Skip to content

Commit 5a818da

Browse files
authored
Merge pull request #98 from fewieden/develop
2.4.0
2 parents 2323336 + 1411d72 commit 5a818da

12 files changed

+482
-403
lines changed

CHANGELOG.md

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# MMM-Fuel Changelog
22

3+
## [2.4.0]
4+
5+
### Added
6+
7+
* [Provider: `gasbuddy`](https://github.com/fewieden/MMM-Fuel/pull/97)
8+
* [Config option: `fade`](https://github.com/fewieden/MMM-Fuel/issues/7)
9+
* [Config option: `showAddressCity`](https://github.com/fewieden/MMM-Fuel/pull/64)
10+
11+
### Changed
12+
13+
* [Migrated provider `nsw` from API v1 to v2 in order to support new region Tasmania (Australia)](https://github.com/fewieden/MMM-Fuel/issues/68#issuecomment-916505199)
14+
* Dependency update
15+
316
## [2.3.1]
417

518
### Fixed

MMM-Fuel.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ Module.register('MMM-Fuel', {
4949
* @property {boolean} colored - Flag to render map in colour or greyscale.
5050
* @property {boolean} open - Flag to render column to indicate if the gas stations are open or closed.
5151
* @property {boolean|int} shortenText - Max characters to be shown for name and address.
52-
* @property {boolean} showAddress - Flag to show the gas stations address.
52+
* @property {boolean} showAddress - Flag to show the gas station's address.
53+
* @property {boolean} showAddressCity - Flag to show the gas station's city.
5354
* @property {boolean} showOpenOnly - Flag to show only open gas stations or all.
5455
* @property {boolean} showDistance - Flag to show the distance to your specified position.
5556
* @property {boolean} showBrand - Flag to show the brand instead of the name.
@@ -61,6 +62,7 @@ Module.register('MMM-Fuel', {
6162
* @property {int} updateInterval - Speed of update.
6263
* @property {string} provider - API provider of the data.
6364
* @property {boolean} toFixed - Flag to show price with only 2 decimals.
65+
* @property {boolean} fade - Fade the list of gas stations.
6466
*/
6567
defaults: {
6668
radius: 5,
@@ -73,6 +75,7 @@ Module.register('MMM-Fuel', {
7375
open: false,
7476
shortenText: false,
7577
showAddress: true,
78+
showAddressCity: true,
7679
showOpenOnly: false,
7780
showDistance: true,
7881
showBrand: false,
@@ -85,7 +88,8 @@ Module.register('MMM-Fuel', {
8588
provider: 'tankerkoenig',
8689
toFixed: false,
8790
stationIds: [],
88-
excludeStationIds: []
91+
excludeStationIds: [],
92+
fade: true
8993
},
9094

9195
/**
@@ -430,5 +434,14 @@ Module.register('MMM-Fuel', {
430434
unit: this.priceList.unit,
431435
maximumFractionDigits: 1
432436
}).format(distance));
437+
this.nunjucksEnvironment().addFilter('fade', (index, total) => {
438+
if (this.config.fade) {
439+
const percentage = (1 - 1 / total * index).toFixed(2);
440+
441+
return `opacity: ${percentage}`;
442+
}
443+
444+
return '';
445+
});
433446
}
434447
});

README.md

+22-2
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ Gas Station Price Module for MagicMirror<sup>2</sup>
5656
| `width` | `600` | Width of the map in pixel. |
5757
| `colored` | `false` | Boolean to show the gas stations map colored or not. |
5858
| `shortenText` | `false` | Integer of characters to be shown for name and address. Default shows all. |
59-
| `showAddress` | `true` | Boolean to show the gas stations address. |
59+
| `showAddress` | `true` | Boolean to show the gas station's address. |
6060
| `showOpenOnly` | `false` | Boolean to show only open gas stations or all. |
6161
| `showDistance` | `true` | Boolean to show the distance to your specified position. |
6262
| `showBrand` | `false` | Boolean to show the brand instead of the name. |
6363
| `iconHeader` | `true` | Boolean to display the car icon in the header. |
6464
| `rotate` | `true` | Boolean to enable/disable rotation between sort by price and distance. |
6565
| `rotateInterval` | `60000` (1 min) | How fast the sorting should be switched between byPrice and byDistance. |
6666
| `updateInterval` | `900000` (15 mins) | How often should the data be fetched. **If your value is to small, you risk to get banned from the API provider. I suggest a minimum of 15mins** |
67+
| `fade` | `true` | Boolean to fade out the list of gas stations. |
6768

6869
## Global config
6970

@@ -85,6 +86,7 @@ or your API access will be suspended.
8586
| `api_key` | REQUIRED | Get an API key for free access to the data of [tankerkoenig.de](https://creativecommons.tankerkoenig.de/#register). |
8687
| `types` | `["diesel"]` | Valid options are `diesel`, `e5` and `e10`. |
8788
| `radius` | `5` | Valid range is 0-25. Set to 0 to disable. Not required if `stationIds` are provided. |
89+
| `showAddressCity` | `true` | Boolean to show the gas station's city. |
8890
| `stationIds` | `[]` | Optional array of fuel station ids to fetch instead of the radius. You can only specify a maximum of 10 and you can find the ids [here](https://creativecommons.tankerkoenig.de/TankstellenFinder/index.html). Using radius and station ids in parallel will result in more API calls. If you run into issues increase the `updateInterval`. |
8991
| `excludeStationIds` | `[]` | Optional array of fuel station ids to exclude from the radius. This is useful e.g. if you got a non public or truck exclusive station in the radius. You can find the ids [here](https://creativecommons.tankerkoenig.de/TankstellenFinder/index.html).|
9092

@@ -98,6 +100,7 @@ No API key required.
98100
| `types` | `["diesel"]` | Valid options are `diesel`, `e5` and `gas`. |
99101
| `radius` | `5` | Valid range not tested yet. |
100102
| `max` | `5` | The API provider returns maximum of 5 valid datasets. |
103+
| `showAddressCity` | `true` | Boolean to show the gas station's city. |
101104
| `showBrand` | `false` | The API provider does not return brand information. |
102105

103106
### autoblog (USA only)
@@ -116,7 +119,24 @@ No API key required. The displayed distance is not based on your coordinates but
116119
| `showOpenOnly` | `false` | Not supported |
117120
| `showBrand` | `false` | Not supported |
118121

119-
### nsw (Australia NSW only)
122+
### gasbuddy (USA and Canada only)
123+
124+
No API key required. The displayed distance is not based on your coordinates but on the zip code.
125+
126+
| **Option** | **Default** | **Description** |
127+
| --- | --- | --- |
128+
| `provider` | `"tankerkoenig"` | Make sure you set it to `"gasbuddy"`. |
129+
| `zip` | REQUIRED | The zip code of your address, e.g. `"12345"` |
130+
| `types` | `["regular"]` | Valid options are `regular`, `midgrade`, `premium`, `diesel`, `e85`, and `unl88`. |
131+
| `showDistance` | `false` | Not supported |
132+
| `radius` | `5` | Not supported |
133+
| `lat` | `undefined` | Not supported |
134+
| `lng` | `undefined` | Not supported |
135+
| `open` | `false` | Not supported |
136+
| `showOpenOnly` | `false` | Not supported |
137+
| `showBrand` | `false` | Not supported |
138+
139+
### nsw (Australia NSW and TAS only)
120140

121141
This provider gives no information if the gas stations are open or closed.
122142
Config options should be set accordingly `open`: false and `showOpenOnly`: false.

apis/autoblog.js

+14-63
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const fetch = require('node-fetch');
1919
*/
2020
const { parse } = require('node-html-parser');
2121

22+
const { fillMissingPrices, sortByDistance, sortByPrice, mergePrices } = require('./utils');
23+
2224
const BASE_URL = 'https://www.autoblog.com';
2325
const MAX_PAGE = 2;
2426

@@ -59,43 +61,6 @@ function mapGasStation(htmlGasStation, type) {
5961
};
6062
}
6163

62-
/**
63-
* @function fillMissingPrices
64-
* @description Replaces missing price information with max price for type.
65-
*
66-
* @param {Object} station - Gas Station
67-
* @param {Object} maxPricesByType - Maximum price per fuel type.
68-
*
69-
* @returns {void}
70-
*/
71-
function fillMissingPrices(station, maxPricesByType) {
72-
for (const type of config.types) {
73-
if (!station.prices[type]) {
74-
station.prices[type] = `>${maxPricesByType[type]}`;
75-
}
76-
}
77-
}
78-
79-
/**
80-
* @function sortByPrice
81-
* @description Helper function to sort gas stations by price.
82-
*
83-
* @param {Object} a - Gas Station
84-
* @param {Object} b - Gas Station
85-
*
86-
* @returns {number} Sorting weight.
87-
*/
88-
function sortByPrice(a, b) {
89-
const aPrice = a.prices[config.sortBy];
90-
const bPrice = b.prices[config.sortBy];
91-
92-
if (!isNaN(aPrice) || !isNaN(bPrice)) {
93-
return isNaN(aPrice) ? 1 : -1;
94-
}
95-
96-
return 0;
97-
}
98-
9964
/**
10065
* @function fetchPaginatedStations
10166
* @description Paginated API requests for specified type.
@@ -160,31 +125,17 @@ async function getAllStations() {
160125
}
161126

162127
/**
163-
* @function mergePrices
164-
* @description Merges fuel prices of different types of gas station
128+
* @function getStationKey
129+
* @description Helper to retrieve unique station key.
130+
*
131+
* @param {Object} station - Station
165132
*
166-
* @param {Object[]} responses - List of gas stations with prices of single fuel type.
133+
* @returns {string} Returns unique station key.
167134
*
168-
* @returns {Object} Returns gas stations with merged prices and max prices per fuel type.
135+
* @see apis/README.md
169136
*/
170-
function mergePrices(responses) {
171-
const { indexedStations, maxPricesByType } = responses.reduce(({ indexedStations, maxPricesByType }, station) => {
172-
const stationKey = `${station.name}-${station.address}`;
173-
174-
if (!indexedStations[stationKey]) {
175-
indexedStations[stationKey] = station;
176-
} else {
177-
indexedStations[stationKey].prices[station.fuelType] = station.prices[station.fuelType];
178-
}
179-
180-
if (!maxPricesByType[station.fuelType] || maxPricesByType[station.fuelType] < station.prices[station.fuelType]) {
181-
maxPricesByType[station.fuelType] = station.prices[station.fuelType];
182-
}
183-
184-
return { indexedStations, maxPricesByType };
185-
}, { indexedStations: {}, maxPricesByType: {} });
186-
187-
return { stations: Object.values(indexedStations), maxPricesByType };
137+
function getStationKey(station) {
138+
return `${station.name}-${station.address}`;
188139
}
189140

190141
/**
@@ -199,14 +150,14 @@ function mergePrices(responses) {
199150
async function getData() {
200151
const responses = await getAllStations();
201152

202-
const { stations, maxPricesByType } = mergePrices(responses);
153+
const { stations, maxPricesByType } = mergePrices(responses, getStationKey);
203154

204-
stations.forEach(station => fillMissingPrices(station, maxPricesByType));
155+
stations.forEach(station => fillMissingPrices(config, station, maxPricesByType));
205156

206157
const filteredStations = stations.filter(station => station.distance <= config.radius);
207158

208-
const stationsSortedByDistance = filteredStations.sort((a, b) => a.distance - b.distance);
209-
const stationsSortedByPrice = [...stationsSortedByDistance].sort(sortByPrice);
159+
const stationsSortedByDistance = filteredStations.sort(sortByDistance);
160+
const stationsSortedByPrice = [...stationsSortedByDistance].sort(sortByPrice.bind(null, config));
210161

211162
return {
212163
types: ['regular', 'premium', 'mid-grade', 'diesel'],

0 commit comments

Comments
 (0)