├── images
├── openwebif.png
├── tankstellenpreise.png
├── guenstigetankstelle_dark.png
├── guenstigetankstelle_light.png
└── tankstellenpreise-config.png
├── README.md
├── openWebIf.js
├── guenstigsteTankstelleAT.js
├── guenstigsteTankstelle.js
├── tankstellenpreise.js
└── tankstellenpreisev2_beta.js
/images/openwebif.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Necriso/ScriptableWidgets/HEAD/images/openwebif.png
--------------------------------------------------------------------------------
/images/tankstellenpreise.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Necriso/ScriptableWidgets/HEAD/images/tankstellenpreise.png
--------------------------------------------------------------------------------
/images/guenstigetankstelle_dark.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Necriso/ScriptableWidgets/HEAD/images/guenstigetankstelle_dark.png
--------------------------------------------------------------------------------
/images/guenstigetankstelle_light.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Necriso/ScriptableWidgets/HEAD/images/guenstigetankstelle_light.png
--------------------------------------------------------------------------------
/images/tankstellenpreise-config.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Necriso/ScriptableWidgets/HEAD/images/tankstellenpreise-config.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # Scriptable Widgets
3 |
4 |
5 |
6 | Get more widgets at www.scriptables.net
7 |
8 |
9 |
10 | ## Tankstellen Preise
11 |
12 |
13 | - Shows fuel prices, address and station is open or closed from a gas station next to your location
14 | - You need an API-Key from https://creativecommons.tankerkoenig.de/
15 |
16 | ### Configuration
17 |
18 | Configure the widget via widget parameters.
19 |
20 | **Standard use**
21 |
22 | On Homescreen, long tap on widget -> edit widget -> Parameter
23 |
24 | Add your credentials as follow:
25 | `API-Key|Radius|fixedLocation (0 or 1)`
26 |
27 | Example: `my-api-key|1|0`
28 |
29 | **Fixed location**
30 |
31 | If you want a fixed location change the configuration to this:
32 | `API-Key|Radius|fixedLocation|latitude|longitude`
33 |
34 | Example: `my-api-key|1|1|54.322|10.1355`
35 |
36 | **Specify favorite station brand**
37 |
38 | If you want to display only a specific station in choosen radius change the configuration to this:
39 | `API-Key|Radius|fixedLocation|latitude|longitude|brand`
40 |
41 | Example: `my-api-key|1|0|0|0|aral`
42 | or
43 | Example: `my-api-key|1|0|0|0|shell`
44 |
45 | It can be that you need to set a greater radius e.g. 2 or 3 to find a station.
46 |
47 | **Warning**
48 |
49 | Tankerkoenig.de blocks your IP for rest of the day if API is used excessive.
50 |
51 | Configuration example:
52 |
53 |
54 |
55 | ## Günstigste Tankstelle (DE/AT)
56 |
57 |
58 | - Shows open gas stations next to your location with the cheapest price of selected oil type first.
59 |
60 | **German Version**
61 | - You need an API-Key from https://creativecommons.tankerkoenig.de/
62 |
63 | ### Configuration
64 |
65 | Configure the widget via widget parameters.
66 |
67 | On Homescreen, long tap on widget -> edit widget -> Parameter
68 |
69 | Add your credentials as follow:
70 | `API-Key|Radius|fuelType`
71 |
72 | Example: `my-api-key|1|diesel`
73 |
74 | Supported fuel types are:
75 | - diesel
76 | - e5
77 | - e10
78 |
79 | **Austria Version**
80 |
81 | ### Configuration
82 |
83 | Configure the widget via widget parameters.
84 |
85 | On Homescreen, long tap on widget -> edit widget -> Parameter
86 |
87 | Add your credentials as follow:
88 | `fuelType`
89 |
90 | Example: `DIE`
91 |
92 | Supported fuel types are:
93 | - DIE
94 | - SUP
95 | - GAS
96 |
97 | ## OpenWebIf (beta)
98 |
99 |
100 |
101 |
102 | - Connects to OpenWebIf API of your Enigma2 receiver
103 | - Displays current and next program of current viewed channel
104 |
--------------------------------------------------------------------------------
/openWebIf.js:
--------------------------------------------------------------------------------
1 | // Version 1.0.0
2 | // Check www.scriptables.net for more widgets
3 | // Use www.scriptdu.de to keep the widget up-to-date
4 |
5 | let schema = 'http'
6 | let openWebIfUser = 'root'
7 | let openWebIfPassword = 'YOUR-PASSWORD'
8 | let openWebIfUrl = 'YOUR.IP/api'
9 |
10 | let currentChannel = await loadCurrentChannel()
11 | let widget = await createWidget(currentChannel)
12 | Script.setWidget(widget)
13 | Script.complete()
14 |
15 | async function loadCurrentChannel() {
16 | let url = schema + "://" + openWebIfUser + ":" + openWebIfPassword + "@" + openWebIfUrl + "/getcurrent"
17 | let req = new Request(url)
18 | req.allowInsecureRequest = true
19 | try {
20 | let json = await req.loadJSON()
21 | return json
22 | } catch(e) {
23 | return 'Keine Verbindung zum Server'
24 | }
25 | }
26 |
27 | function getTime(timestamp) {
28 | let ts = new Date(timestamp * 1000)
29 | return ts.toLocaleTimeString("de-DE", {timeZone: 'Europe/Berlin', hour: '2-digit', minute:'2-digit'})
30 | }
31 |
32 | async function createWidget(channel) {
33 | let widget = new ListWidget()
34 | widget.setPadding(5, 0, 0, 5)
35 | // set gradient background
36 | let startColor = new Color("#000")
37 | let endColor = new Color("#111")
38 | let gradient = new LinearGradient()
39 | gradient.colors = [startColor, endColor]
40 | gradient.locations = [0, 1]
41 | widget.backgroundColor = new Color("#000")
42 | widget.backgroundGradient = gradient
43 |
44 | if (typeof channel !== "object") {
45 | let nowText = widget.addText(channel)
46 | nowText.font = Font.boldSystemFont(12)
47 | nowText.textColor = Color.white()
48 | nowText.centerAlignText()
49 | nowText.textOpacity = 0.8
50 | } else {
51 | let nowChannelText = widget.addText(channel.now.sname)
52 | nowChannelText.font = Font.boldSystemFont(12)
53 | nowChannelText.textColor = Color.white()
54 | nowChannelText.centerAlignText()
55 | nowChannelText.textOpacity = 0.8
56 |
57 | widget.addSpacer(5)
58 |
59 | let nowText = widget.addText("JETZT")
60 | nowText.font = Font.boldSystemFont(12)
61 | nowText.textColor = Color.white()
62 | nowText.centerAlignText()
63 | nowText.textOpacity = 0.8
64 |
65 | let nowTimeTxt = widget.addText(getTime(channel.now.begin_timestamp) + " Uhr")
66 | nowTimeTxt.font = Font.semiboldSystemFont(12)
67 | nowTimeTxt.textColor = Color.white()
68 | nowTimeTxt.centerAlignText()
69 |
70 | let nowTitleTxt = widget.addText(channel.now.title)
71 | nowTitleTxt.font = Font.semiboldSystemFont(12)
72 | nowTitleTxt.textColor = Color.white()
73 | nowTitleTxt.centerAlignText()
74 |
75 | widget.addSpacer(5)
76 |
77 | let nextText = widget.addText("NÄCHSTE")
78 | nextText.font = Font.boldSystemFont(12)
79 | nextText.textColor = Color.white()
80 | nextText.centerAlignText()
81 | nextText.textOpacity = 0.8
82 |
83 | let nextTimeTxt = widget.addText(getTime(channel.next.begin_timestamp) + " Uhr")
84 | nextTimeTxt.font = Font.semiboldSystemFont(12)
85 | nextTimeTxt.textColor = Color.white()
86 | nextTimeTxt.centerAlignText()
87 |
88 | let nextTitleTxt = widget.addText(channel.next.title)
89 | nextTitleTxt.font = Font.semiboldSystemFont(12)
90 | nextTitleTxt.textColor = Color.white()
91 | nextTitleTxt.centerAlignText()
92 |
93 | widget.addSpacer()
94 | }
95 | return widget
96 | }
97 |
--------------------------------------------------------------------------------
/guenstigsteTankstelleAT.js:
--------------------------------------------------------------------------------
1 | // Version 1.0.0
2 | // Check www.scriptables.net for more widgets
3 | // Use www.scriptdu.de to keep the widget up-to-date
4 | // Usage:
5 | // Add credentials toyour widget parameters:
6 | // fuelType (DIE, SUP, GAS)
7 |
8 |
9 | let fuelType
10 | let widgetInput = args.widgetParameter;
11 |
12 | if (widgetInput !== null) {
13 | fuelType = widgetInput.toString();
14 |
15 | if (!fuelType) {
16 | throw new Error("Invalid parameter. Expected format: fuelType (DIE, SUP, GAS)")
17 | }
18 |
19 | } else {
20 | throw new Error("No Widget paramter set. Expected format: fuelType (DIE, SUP, GAS)")
21 | }
22 |
23 | const backColor = Color.dynamic(new Color('FFFFFF'), new Color('111111'));
24 | const backColor2 = Color.dynamic(new Color('EEEEEE'), new Color('222222'));
25 | const textColor = Color.dynamic(new Color('000000'), new Color('EDEDED'));
26 | const greyTextColor = Color.dynamic(new Color('000000'), new Color('BBBBBB'))
27 | const apiURL = (location, fuelType) => `https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=${location.latitude.toFixed(3)}&longitude=${location.longitude.toFixed(3)}&fuelType=${fuelType.toUpperCase()}&includeClosed=false`
28 |
29 | let station = await loadStation(fuelType)
30 | let widget = await createWidget(station, fuelType)
31 |
32 | if (!config.runsInWidget) {
33 | await widget.presentSmall()
34 | }
35 |
36 | Script.setWidget(widget)
37 | Script.complete()
38 |
39 | async function loadStation(fuelType) {
40 | let location = await Location.current()
41 |
42 | const data = await new Request(apiURL(location, fuelType)).loadJSON()
43 |
44 | if (data.length === 0) {
45 | return { error: 1 }
46 | }
47 | return data
48 | }
49 |
50 | function formatValue(value) {
51 | let lastDigit = '⁹'
52 | let price = value.toString().slice(0, -1)
53 | return price + lastDigit + "€"
54 | }
55 |
56 | function createList(data) {
57 | const list = new ListWidget()
58 | list.setPadding(0, 4, 1, 4)
59 |
60 | const gradient = new LinearGradient()
61 | gradient.locations = [0, 1]
62 | gradient.colors = [
63 | backColor,
64 | backColor2
65 | ]
66 | list.backgroundGradient = gradient
67 |
68 | if (data.error) {
69 | let errorMessage = list.addText('No stations found.')
70 | errorMessage.font = Font.boldSystemFont(12)
71 | errorMessage.textColor = textColor
72 | }
73 | return list
74 | }
75 |
76 | async function createWidget(data, fuelType) {
77 | let list = createList(data);
78 | const stations = data;
79 | const attr = stations.filter(stations => stations.prices.length !== 0);
80 |
81 | attr.sort(function (a, b) {
82 | return a['prices'][0]['amount'] > b['prices'][0]['amount'];
83 | });
84 |
85 | for (let i = 0; i < 3; i++) {
86 | if (attr[i]) {
87 | if (i === 0) {
88 | let stationPrice = list.addText(formatValue(attr[i]['prices'][0]['amount']))
89 | stationPrice.font = new Font('Menlo', 25)
90 | stationPrice.centerAlignText()
91 | stationPrice.textColor = textColor
92 |
93 | let stationName = list.addText(attr[i]['name'])
94 | stationName.font = Font.boldSystemFont(12)
95 | stationName.textColor = textColor
96 | stationName.centerAlignText()
97 |
98 | let station = list.addText(attr[i].location.address)
99 | station.font = Font.lightSystemFont(12)
100 | station.centerAlignText()
101 | station.textColor = textColor
102 |
103 | list.addSpacer(7)
104 | } else if (i === 1) {
105 | let stationStack1 = list.addStack()
106 | let price1 = stationStack1.addText(formatValue(attr[i]['prices'][0]['amount']))
107 | price1.font = new Font('Menlo', 10)
108 | price1.textColor = greyTextColor
109 |
110 | stationStack1.addSpacer(3)
111 | let addressStack1 = stationStack1.addStack()
112 | addressStack1.layoutVertically()
113 |
114 | let station1 = addressStack1.addText(attr[i]['name'])
115 | station1.font = Font.regularSystemFont(10)
116 | station1.textColor = greyTextColor
117 |
118 | let address1 = addressStack1.addText(attr[i].location.address)
119 | address1.font = Font.regularSystemFont(10)
120 | address1.textColor = greyTextColor
121 |
122 | list.addSpacer(2)
123 | } else if (i === 2) {
124 | let stationStack2 = list.addStack()
125 | let price2 = stationStack2.addText(formatValue(attr[i]['prices'][0]['amount']))
126 | price2.font = new Font('Menlo', 10)
127 | price2.textColor = greyTextColor
128 |
129 | stationStack2.addSpacer(3)
130 |
131 | let addressStack2 = stationStack2.addStack()
132 | addressStack2.layoutVertically()
133 |
134 | let station2 = addressStack2.addText(attr[i]['name'])
135 | station2.font = Font.regularSystemFont(10)
136 | station2.textColor = greyTextColor
137 |
138 | let address2 = addressStack2.addText(attr[i].location.address)
139 | address2.font = Font.regularSystemFont(10)
140 | address2.textColor = greyTextColor
141 | }
142 | }
143 | }
144 |
145 | return list
146 | }
147 |
--------------------------------------------------------------------------------
/guenstigsteTankstelle.js:
--------------------------------------------------------------------------------
1 | // Version 1.1.1
2 | // Check www.scriptables.net for more widgets
3 | // Use www.scriptdu.de to keep the widget up-to-date
4 | // Script by Emre Eromay
5 | // Usage:
6 | // Add credentials toyour widget parameters:
7 | // API-Key|radius in km|fuelType (diesel, e5, e10)
8 | // Important: Don't set the radius to big, the tankerkoenig.de endpoint will deliver all stations in the radius which is set,
9 | // so it will take a long time to fetch data.
10 |
11 | let apiKey, radius, fixedLocation, latitude, longitude, myLocation, oilType
12 | let widgetInput = args.widgetParameter;
13 |
14 | if (widgetInput !== null) {
15 | [apiKey, radius, oilType] = widgetInput.toString().split("|");
16 |
17 | if (!apiKey || !radius || !oilType) {
18 | throw new Error("Invalid parameter. Expected format: apiKey|radius (1-20)|fuelType (diesel, e5, e10)")
19 | }
20 | // Set strings to correct types
21 | radius = parseInt(radius)
22 |
23 | } else {
24 | throw new Error("No Widget paramter set. Expected format: apiKey|radius (1-20)|fuelType (diesel, e5, e10)")
25 | }
26 |
27 | const backColor = Color.dynamic(new Color('FFFFFF'), new Color('111111'))
28 | const backColor2 = Color.dynamic(new Color('EEEEEE'), new Color('222222'))
29 | const textColor = Color.dynamic(new Color('000000'), new Color('EDEDED'))
30 | const greyTextColor = Color.dynamic(new Color('000000'), new Color('BBBBBB'))
31 |
32 | const apiURL = (location, radius, apiKey, oilType) => `https://creativecommons.tankerkoenig.de/json/list.php?lat=${location.latitude.toFixed(3)}&lng=${location.longitude.toFixed(3)}&rad=${radius}&sort=price&type=${oilType}&apikey=${apiKey}`
33 |
34 | let station = await loadStation(apiKey, radius, oilType)
35 | let widget = await createWidget(station)
36 |
37 | if (!config.runsInWidget) {
38 | await widget.presentSmall()
39 | }
40 |
41 | Script.setWidget(widget)
42 | Script.complete()
43 |
44 | async function loadStation(apiKey, radius, oilType) {
45 |
46 | let location = await Location.current()
47 |
48 | const data = await new Request(apiURL(location, radius, apiKey, oilType)).loadJSON()
49 |
50 | if (data.stations.length === 0) {
51 | return { error: 1 }
52 | }
53 | return data
54 | }
55 |
56 | function formatValue(value) {
57 | let lastDigit = '⁹'
58 | let price = value.toString().slice(0, -1)
59 | return price + lastDigit + "€"
60 | }
61 |
62 | function createList(data) {
63 | const list = new ListWidget()
64 | list.setPadding(0, 4, 1, 4)
65 |
66 | const gradient = new LinearGradient()
67 | gradient.locations = [0, 1]
68 | gradient.colors = [
69 | backColor,
70 | backColor2
71 | ]
72 | list.backgroundGradient = gradient
73 |
74 | if (data.error) {
75 | let errorMessage = list.addText('No station in selected radius found. Please set a greater radius in widget parameters')
76 | errorMessage.font = Font.boldSystemFont(12)
77 | errorMessage.textColor = textColor
78 | }
79 | return list
80 | }
81 |
82 | async function createWidget(data) {
83 | let list = createList(data);
84 | list.refreshAfterDate = new Date(Date.now() + 300000);
85 |
86 | const attr = data.stations.filter(station => station.isOpen == true)
87 |
88 | for (let i = 0; i < 3; i++) {
89 | if (attr[i]) {
90 | if (i === 0) {
91 | let stationPrice = list.addText(formatValue(attr[i].price))
92 | stationPrice.font = new Font('Menlo', 25)
93 | stationPrice.centerAlignText()
94 | stationPrice.textColor = textColor
95 |
96 | let stationName = list.addText(attr[i]['brand'])
97 | stationName.font = Font.boldSystemFont(12)
98 | stationName.textColor = textColor
99 | stationName.centerAlignText()
100 |
101 | let station = list.addText(attr[i].street + " " + attr[i].houseNumber)
102 | station.font = Font.lightSystemFont(12)
103 | station.centerAlignText()
104 | station.textColor = textColor
105 |
106 | list.addSpacer(7)
107 | } else if (i === 1) {
108 | let stationStack1 = list.addStack()
109 | let price1 = stationStack1.addText(formatValue(attr[i].price))
110 | price1.font = new Font('Menlo', 10)
111 | price1.textColor = greyTextColor
112 |
113 | stationStack1.addSpacer(3)
114 | let addressStack1 = stationStack1.addStack()
115 | addressStack1.layoutVertically()
116 |
117 | let station1 = addressStack1.addText(attr[i]['brand'])
118 | station1.font = Font.regularSystemFont(10)
119 | station1.textColor = greyTextColor
120 |
121 | let address1 = addressStack1.addText(attr[i].street + " " + attr[i].houseNumber)
122 | address1.font = Font.regularSystemFont(10)
123 | address1.textColor = greyTextColor
124 |
125 | list.addSpacer(2)
126 | } else if (i === 2) {
127 | let stationStack2 = list.addStack()
128 | let price2 = stationStack2.addText(formatValue(attr[i].price))
129 | price2.font = new Font('Menlo', 10)
130 | price2.textColor = greyTextColor
131 |
132 | stationStack2.addSpacer(3)
133 |
134 | let addressStack2 = stationStack2.addStack()
135 | addressStack2.layoutVertically()
136 |
137 | let station2 = addressStack2.addText(attr[i]['brand'])
138 | station2.font = Font.regularSystemFont(10)
139 | station2.textColor = greyTextColor
140 |
141 | let address2 = addressStack2.addText(attr[i].street + " " + attr[i].houseNumber)
142 | address2.font = Font.regularSystemFont(10)
143 | address2.textColor = greyTextColor
144 | }
145 | }
146 | }
147 | return list
148 | }
149 |
--------------------------------------------------------------------------------
/tankstellenpreise.js:
--------------------------------------------------------------------------------
1 | // Version 1.1.5
2 | // Check www.scriptables.net for more widgets
3 | // Use www.scriptdu.de to keep the widget up-to-date
4 | // Usage:
5 | // Add credentials toyour widget parameters:
6 | // API-Key|radius in km|fixedLocation (0 or 1) e.g my-api-key|1|0
7 | // If you want to set a fixed location then the settings should look like:
8 | // API-Key|radius in km|fixedLocation|latitude|longitude (0 or 1) e.g my-api-key|1|1|54.322|10.1355
9 | // Important: Don't set the radius to big, the tankerkoenig.de endpoint will deliver all stations in the radius which is set,
10 | // but only one is needed to display, so it will take a long time to fetch data.
11 |
12 | let apiKey, radius, fixedLocation, latitude, longitude, myLocation, brand
13 | let widgetInput = args.widgetParameter;
14 |
15 | if (widgetInput !== null) {
16 | [apiKey, radius, fixedLocation, latitude, longitude, brand] = widgetInput.split("|");
17 |
18 | if (!apiKey || !radius || !fixedLocation) {
19 | throw new Error("Invalid parameter. Expected format: apiKey|radius (1-20)|fixedLocation (0 or 1)")
20 | }
21 | if (fixedLocation === 1 && (!latitude || !longitude)) {
22 | throw new Error("If fixed location is set to 1 you must set latitude and longitude")
23 | }
24 |
25 | // Set strings to correct types
26 | radius = parseInt(radius)
27 | fixedLocation = parseInt(fixedLocation)
28 |
29 | if (!brand) {
30 | brand = false
31 | }
32 | if (fixedLocation == 0) {
33 | myLocation = {
34 | latitude: 0,
35 | longitude: 0
36 | }
37 | } else {
38 | myLocation = {
39 | latitude: parseFloat(latitude),
40 | longitude: parseFloat(longitude)
41 | }
42 | }
43 | } else {
44 | throw new Error("No Widget paramter set. Expected format: apiKey|radius (1-20)|fixedLocation (0 or 1)")
45 | }
46 |
47 | const backColor = Color.dynamic(new Color('FFFFFF'), new Color('111111'));
48 | const backColor2 = Color.dynamic(new Color('EEEEEE'), new Color('222222'));
49 | const textColor = Color.dynamic(new Color('000000'), new Color('EDEDED'));
50 |
51 | const apiURL = (location, radius, apiKey) => `https://creativecommons.tankerkoenig.de/json/list.php?lat=${location.latitude.toFixed(3)}&lng=${location.longitude.toFixed(3)}&rad=${radius}&sort=dist&type=all&apikey=${apiKey}`
52 |
53 | let station = await loadStation(apiKey, radius, fixedLocation, myLocation)
54 | let widget = await createWidget(station, brand)
55 |
56 | if (!config.runsInWidget) {
57 | await widget.presentSmall()
58 | }
59 |
60 | Script.setWidget(widget)
61 | Script.complete()
62 |
63 | async function loadStation(apiKey, radius, fixedLocation, myLocation) {
64 | let location
65 |
66 | if (fixedLocation) {
67 | location = myLocation
68 | } else {
69 | location = await Location.current()
70 | }
71 |
72 | const data = await new Request(apiURL(location, radius, apiKey)).loadJSON()
73 |
74 | if (data.stations.length === 0) {
75 | return { error: 1 }
76 | }
77 | return data
78 | }
79 |
80 | function formatValue(value) {
81 | if (!value) {
82 | return '-'
83 | }
84 | let lastDigit = '⁹'
85 | let price = value.toString().slice(0, -1)
86 | return price + lastDigit + "€"
87 | }
88 |
89 | async function createWidget(data, brand) {
90 |
91 | const list = new ListWidget()
92 | list.setPadding(0, 4, 1, 4)
93 |
94 | const gradient = new LinearGradient()
95 | gradient.locations = [0, 1]
96 | gradient.colors = [
97 | backColor,
98 | backColor2
99 | ]
100 | list.backgroundGradient = gradient
101 |
102 | if (data.error) {
103 | let errorMessage = list.addText('No station in selected radius found. Please set a greater radius in widget parameters')
104 | errorMessage.font = Font.boldSystemFont(12)
105 | errorMessage.textColor = textColor
106 | return list
107 | }
108 |
109 | const stations = data.stations;
110 | let selectedStations
111 |
112 | if (brand) {
113 | selectedStations = stations.filter(stations => stations['brand'].toLowerCase() === brand.toLowerCase());
114 | } else {
115 | selectedStations = stations
116 | }
117 |
118 | const attr = selectedStations[0]
119 |
120 | let open = '🔴'
121 | if (attr.isOpen) {
122 | open = '🟢'
123 | }
124 |
125 | let firstLineStack = list.addStack()
126 |
127 | let stationName = firstLineStack.addText(attr.brand)
128 | stationName.font = Font.boldSystemFont(15)
129 | stationName.textColor = textColor
130 |
131 | firstLineStack.addSpacer()
132 | let stationOpen = firstLineStack.addText(open)
133 | stationOpen.font = Font.mediumSystemFont(10)
134 | stationOpen.rightAlignText()
135 |
136 | list.addSpacer(5)
137 |
138 | let dieselStack = list.addStack()
139 | let dieselLabel = dieselStack.addText("Diesel:")
140 | dieselLabel.font = Font.boldSystemFont(12)
141 | dieselLabel.textColor = textColor
142 |
143 | dieselStack.addSpacer()
144 | let dieselPrice = dieselStack.addText(formatValue(attr.diesel))
145 | dieselPrice.font = new Font('Menlo', 12)
146 | dieselPrice.textColor = textColor
147 |
148 | list.addSpacer(1)
149 |
150 | let e5Stack = list.addStack()
151 | let e5Label = e5Stack.addText("Benzin E5:")
152 | e5Label.font = Font.boldSystemFont(12)
153 | e5Label.textColor = textColor
154 |
155 | e5Stack.addSpacer()
156 | let e5Price = e5Stack.addText(formatValue(attr.e5))
157 | e5Price.font = new Font('Menlo', 12)
158 | e5Price.textColor = textColor
159 |
160 | list.addSpacer(1)
161 |
162 | let e10Stack = list.addStack()
163 | let e10Label = e10Stack.addText("Benzin E10:")
164 | e10Label.font = Font.boldSystemFont(12)
165 | e10Label.textColor = textColor
166 |
167 | e10Stack.addSpacer()
168 | let e10Price = e10Stack.addText(formatValue(attr.e10))
169 | e10Price.font = new Font('Menlo', 12)
170 | e10Price.textColor = textColor
171 |
172 | list.addSpacer(5)
173 | let address = list.addText('Adresse:')
174 | address.font = Font.boldSystemFont(12)
175 | address.textColor = textColor
176 |
177 | let addressStack = list.addStack()
178 |
179 | let station = addressStack.addText(attr.street)
180 | station.font = Font.lightSystemFont(12)
181 | station.textColor = textColor
182 |
183 | let houseNumber = addressStack.addText(" " + attr.houseNumber)
184 | houseNumber.font = Font.lightSystemFont(12)
185 | houseNumber.textColor = textColor
186 |
187 | return list
188 | }
--------------------------------------------------------------------------------
/tankstellenpreisev2_beta.js:
--------------------------------------------------------------------------------
1 | // Version 2.0.3b
2 | // Check www.scriptables.net for more widgets
3 | // Use www.scriptdu.de to keep the widget up-to-date
4 |
5 | const files = FileManager.local()
6 | const configPath = files.joinPath(files.documentsDirectory(), "widget-tankstelle.json")
7 | const configExits = files.fileExists(configPath)
8 |
9 | let configuration = await loadConfig()
10 |
11 | if (!configuration) {
12 | console.log("noconfig")
13 | configuration = {
14 | apiKey: '',
15 | radius: '',
16 | fixedLocation: '',
17 | myLocation: {
18 | latitude: '',
19 | longitude: ''
20 | }
21 | }
22 | }
23 |
24 | console.log(configuration)
25 |
26 | if (config.runsInApp) {
27 | const alert = await createAlert(configuration)
28 | const response = await alert.present()
29 |
30 | if (response === 0) {
31 | console.log('Cancel was pressed... doing nothing')
32 | } else if (response === 1) {
33 | console.log('Submit was pressed')
34 |
35 | let store = {
36 | apiKey: alert.textFieldValue(0),
37 | radius: alert.textFieldValue(1),
38 | fixedLocation: parseInt(alert.textFieldValue(2)),
39 | myLocation: {
40 | latitude: parseFloat(alert.textFieldValue(3)),
41 | longitude: parseFloat(alert.textFieldValue(4))
42 | }
43 | }
44 | await storeConfig(store)
45 | }
46 | }
47 |
48 | const apiURL = (location, radius, apiKey) => `https://creativecommons.tankerkoenig.de/json/list.php?lat=${location.latitude.toFixed(3)}&lng=${location.longitude.toFixed(3)}&rad=${radius}&sort=dist&type=all&apikey=${apiKey}`
49 |
50 | let station = await loadStation()
51 | let widget = await createWidget(station)
52 |
53 | if (!config.runsInWidget) {
54 | await widget.presentSmall()
55 | }
56 | Script.setWidget(widget)
57 | Script.complete()
58 |
59 |
60 | async function createAlert(configuration) {
61 | console.log('Creating alert with data')
62 |
63 | const alert = new Alert()
64 | alert.title = `Settings`
65 |
66 | if (!configuration.fixedLocation) {
67 | configuration.fixedLocation = "0"
68 | }
69 |
70 | if (!configuration.myLocation.latitude) {
71 | configuration.myLocation.latitude = ''
72 | }
73 |
74 | if (!configuration.myLocation.longitude) {
75 | configuration.myLocation.longitude = ''
76 | }
77 |
78 | alert.addTextField('API-Key', configuration.apiKey || '')
79 | alert.addTextField('Radius in km (e.g 1)', configuration.radius.toString())
80 | alert.addTextField('Fixed Location (0 or 1)', configuration.fixedLocation.toString())
81 | alert.addTextField('Fixed Latitude (e.g. 53.511)', configuration.myLocation.latitude.toString())
82 | alert.addTextField('Fixed Longitude (e.g. 9.9937)', configuration.myLocation.longitude.toString())
83 | alert.addAction('Cancel'); // 0
84 | alert.addAction('Submit'); // 1
85 |
86 | return alert;
87 | }
88 |
89 | async function storeConfig(data)
90 | {
91 | try {
92 | files.writeString(configPath, JSON.stringify(data))
93 | } catch (e) {
94 | console.log("Creating Config failed!")
95 | console.log(e)
96 | }
97 | }
98 |
99 | async function loadConfig() {
100 | let data
101 |
102 | if (configExits) {
103 | console.log("Get from file")
104 | data = JSON.parse(files.readString(configPath))
105 | } else {
106 | data = null
107 | }
108 | return data
109 | }
110 |
111 | async function loadStation() {
112 | let location
113 |
114 | let config = await loadConfig()
115 |
116 | if (config.fixedLocation === 1) {
117 | location = config.myLocation
118 | } else {
119 | location = await Location.current()
120 | }
121 | console.log(location)
122 | const data = await new Request(apiURL(location, config.radius, config.apiKey)).loadJSON()
123 |
124 | if (!data) {
125 | const errorList = new ListWidget()
126 | errorList.addText("Keine Ergebnisse für den aktuellen Ort gefunden.")
127 | return errorList
128 | }
129 | return data
130 | }
131 |
132 | function formatValue(value) {
133 | if (!value) {
134 | return '-'
135 | }
136 | let lastDigit = '⁹'
137 | let price = value.toString().slice(0, -1)
138 | return price + lastDigit + "€"
139 | }
140 |
141 | async function createWidget(data) {
142 | const attr = data.stations[0]
143 |
144 | const list = new ListWidget()
145 | list.setPadding(0, 4, 1, 4)
146 |
147 | const gradient = new LinearGradient()
148 | gradient.locations = [0, 1]
149 | gradient.colors = [
150 | new Color("111111"),
151 | new Color("222222")
152 | ]
153 | list.backgroundGradient = gradient
154 |
155 | let open = '🔴'
156 | if (attr.isOpen) {
157 | open = '🟢'
158 | }
159 |
160 | let firstLineStack = list.addStack()
161 |
162 | let stationName = firstLineStack.addText(attr.brand)
163 | stationName.font = Font.boldSystemFont(15)
164 | stationName.textColor = Color.white()
165 |
166 | firstLineStack.addSpacer()
167 | let stationOpen = firstLineStack.addText(open)
168 | stationOpen.font = Font.mediumSystemFont(10)
169 | stationOpen.rightAlignText()
170 |
171 | list.addSpacer(5)
172 |
173 | let dieselStack = list.addStack()
174 | let dieselLabel = dieselStack.addText("Diesel:")
175 | dieselLabel.font = Font.boldSystemFont(12)
176 | dieselLabel.textColor = Color.white()
177 |
178 | dieselStack.addSpacer()
179 | let dieselPrice = dieselStack.addText(formatValue(attr.diesel))
180 | dieselPrice.font = new Font('Menlo', 12)
181 | dieselPrice.textColor = Color.white()
182 |
183 | list.addSpacer(1)
184 |
185 | let e5Stack = list.addStack()
186 | let e5Label = e5Stack.addText("Benzin E5:")
187 | e5Label.font = Font.boldSystemFont(12)
188 | e5Label.textColor = Color.white()
189 |
190 | e5Stack.addSpacer()
191 | let e5Price = e5Stack.addText(formatValue(attr.e5))
192 | e5Price.font = new Font('Menlo', 12)
193 | e5Price.textColor = Color.white()
194 |
195 | list.addSpacer(1)
196 | let e10Stack = list.addStack()
197 | let e10Label = e10Stack.addText("Benzin E10:")
198 | e10Label.font = Font.boldSystemFont(12)
199 | e10Label.textColor = Color.white()
200 |
201 | e10Stack.addSpacer()
202 | let e10Price = e10Stack.addText(formatValue(attr.e10))
203 | e10Price.font = new Font('Menlo', 12)
204 | e10Price.textColor = Color.white()
205 |
206 | list.addSpacer(5)
207 | let address = list.addText('Adresse:')
208 | address.font = Font.boldSystemFont(12)
209 | address.textColor = Color.white()
210 | let station = list.addText(attr.street)
211 | station.font = Font.lightSystemFont(12)
212 | station.textColor = Color.white()
213 |
214 | return list
215 | }
216 |
--------------------------------------------------------------------------------