Skip to content

Commit

Permalink
Merge pull request #118 from fewieden/develop
Browse files Browse the repository at this point in the history
v2.5.0
  • Loading branch information
fewieden committed Mar 10, 2024
2 parents 5a818da + bfc563c commit eafb711
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 101 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,21 @@
# MMM-Fuel Changelog

## [2.5.0]

Thanks to [marcx28](https://github.com/marcx28) for their contributions.

### Fixed

* [Sorting by price](https://github.com/fewieden/MMM-Fuel/issues/110)

### Added

* Dependency: `lodash`

### Changed

* [Allow custom header text](https://github.com/fewieden/MMM-Fuel/pull/102)

## [2.4.0]

### Added
Expand Down
1 change: 1 addition & 0 deletions MMM-Fuel.js
Expand Up @@ -161,6 +161,7 @@ Module.register('MMM-Fuel', {

return {
config: this.config,
header: this.data.header,
priceList: this.priceList,
sortByPrice: this.sortByPrice,
gasStations
Expand Down
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -13,8 +13,9 @@ Gas Station Price Module for MagicMirror<sup>2</sup>
* An installation of [MagicMirror<sup>2</sup>](https://github.com/MichMich/MagicMirror)
* OPTIONAL: [Voice Control](https://github.com/fewieden/MMM-voice) and [MMM-Modal](https://github.com/fewieden/MMM-Modal)
* npm
* [node-fetch](https://www.npmjs.com/package/node-fetch)
* [lodash](https://www.npmjs.com/package/lodash)
* [moment](https://www.npmjs.com/package/moment)
* [node-fetch](https://www.npmjs.com/package/node-fetch)
* [node-html-parser](https://www.npmjs.com/package/node-html-parser)

## Installation
Expand Down Expand Up @@ -60,7 +61,7 @@ Gas Station Price Module for MagicMirror<sup>2</sup>
| `showOpenOnly` | `false` | Boolean to show only open gas stations or all. |
| `showDistance` | `true` | Boolean to show the distance to your specified position. |
| `showBrand` | `false` | Boolean to show the brand instead of the name. |
| `iconHeader` | `true` | Boolean to display the car icon in the header. |
| `iconHeader` | `true` | Boolean to display the car icon in the header (works only if no custom header is set). |
| `rotate` | `true` | Boolean to enable/disable rotation between sort by price and distance. |
| `rotateInterval` | `60000` (1 min) | How fast the sorting should be switched between byPrice and byDistance. |
| `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** |
Expand Down
15 changes: 9 additions & 6 deletions apis/autoblog.js
Expand Up @@ -7,6 +7,12 @@
* @see https://github.com/fewieden/MMM-Fuel
*/

/**
* @external lodash
* @see https://www.npmjs.com/package/lodash
*/
const _ = require('lodash');

/**
* @external node-fetch
* @see https://www.npmjs.com/package/node-fetch
Expand All @@ -19,7 +25,7 @@ const fetch = require('node-fetch');
*/
const { parse } = require('node-html-parser');

const { fillMissingPrices, sortByDistance, sortByPrice, mergePrices } = require('./utils');
const { fillMissingPrices, mergePrices, sortByPrice } = require('./utils');

const BASE_URL = 'https://www.autoblog.com';
const MAX_PAGE = 2;
Expand Down Expand Up @@ -156,15 +162,12 @@ async function getData() {

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

const stationsSortedByDistance = filteredStations.sort(sortByDistance);
const stationsSortedByPrice = [...stationsSortedByDistance].sort(sortByPrice.bind(null, config));

return {
types: ['regular', 'premium', 'mid-grade', 'diesel'],
unit: 'mile',
currency: 'USD',
byPrice: stationsSortedByPrice,
byDistance: stationsSortedByDistance
byPrice: _.sortBy(filteredStations, sortByPrice.bind(null, config)),
byDistance: _.sortBy(filteredStations, 'distance')
};
}

Expand Down
8 changes: 7 additions & 1 deletion apis/gasbuddy.js
Expand Up @@ -7,6 +7,12 @@
* @see https://github.com/fewieden/MMM-Fuel
*/

/**
* @external lodash
* @see https://www.npmjs.com/package/lodash
*/
const _ = require('lodash');

/**
* @external node-fetch
* @see https://www.npmjs.com/package/node-fetch
Expand Down Expand Up @@ -160,7 +166,7 @@ async function getData() {
stations.forEach(station => fillMissingPrices(config, station, maxPricesByType));

// Webpage doesn't support distance (only zip code).
const stationsSortedByPrice = stations.sort(sortByPrice.bind(null, config));
const stationsSortedByPrice = _.sortBy(stations, sortByPrice.bind(null, config));
const stationsSortedByDistance = stationsSortedByPrice;

return {
Expand Down
13 changes: 8 additions & 5 deletions apis/nsw.js
Expand Up @@ -7,6 +7,12 @@
* @see https://github.com/fewieden/MMM-Fuel
*/

/**
* @external lodash
* @see https://www.npmjs.com/package/lodash
*/
const _ = require('lodash');

/**
* @external node-fetch
* @see https://www.npmjs.com/package/node-fetch
Expand All @@ -25,7 +31,7 @@ const moment = require('moment');
*/
const Log = require('logger');

const { filterStations, sortByDistance } = require('./utils');
const { filterStations } = require('./utils');

const SECOND = 1000;
const MINUTE = 60 * SECOND;
Expand Down Expand Up @@ -126,15 +132,12 @@ async function getData() {

stations = stations.filter(filterStations);

const distance = stations.slice(0);
distance.sort(sortByDistance);

return {
types: ['diesel', 'e5'],
unit: 'kilometer',
currency: 'AUD',
byPrice: stations,
byDistance: distance
byDistance: _.sortBy(stations, 'distance')
};
}

Expand Down
13 changes: 8 additions & 5 deletions apis/spritpreisrechner.js
Expand Up @@ -7,13 +7,19 @@
* @see https://github.com/fewieden/MMM-Fuel
*/

/**
* @external lodash
* @see https://www.npmjs.com/package/lodash
*/
const _ = require('lodash');

/**
* @external node-fetch
* @see https://www.npmjs.com/package/node-fetch
*/
const fetch = require('node-fetch');

const { filterStations, sortByDistance } = require('./utils');
const { filterStations } = require('./utils');

const BASE_URL = 'https://api.e-control.at/sprit/1.0';
const TYPES = {
Expand Down Expand Up @@ -176,15 +182,12 @@ async function getData() {

stations = stations.filter(filterStations);

const distance = stations.slice(0);
distance.sort(sortByDistance);

return {
types: ['diesel', 'e5', 'gas'],
unit: 'kilometer',
currency: 'EUR',
byPrice: stations,
byDistance: distance
byDistance: _.sortBy(stations, 'distance')
};
}

Expand Down
56 changes: 10 additions & 46 deletions apis/tankerkoenig.js
Expand Up @@ -7,6 +7,12 @@
* @see https://github.com/fewieden/MMM-Fuel
*/

/**
* @external lodash
* @see https://www.npmjs.com/package/lodash
*/
const _ = require('lodash');

/**
* @external node-fetch
* @see https://www.npmjs.com/package/node-fetch
Expand All @@ -19,6 +25,8 @@ const fetch = require('node-fetch');
*/
const Log = require('logger');

const { sortByPrice } = require('./utils');

const BASE_URL = 'https://creativecommons.tankerkoenig.de/json';

let config;
Expand Down Expand Up @@ -59,44 +67,6 @@ function generateStationInfoUrl(id) {
return `${BASE_URL}/detail.php?id=${id}&apikey=${config.api_key}`;
}

/**
* @function sortByPrice
* @description Helper function to sort gas stations by price.
*
* @param {Object} a - Gas Station
* @param {Object} b - Gas Station
*
* @returns {number} Sorting weight.
*/
function sortByPrice(a, b) {
if (b[config.sortBy] === 0) {
return Number.MIN_SAFE_INTEGER;
} else if (a[config.sortBy] === 0) {
return Number.MAX_SAFE_INTEGER;
}

return a[config.sortBy] - b[config.sortBy];
}

/**
* @function sortByDistance
* @description Helper function to sort gas stations by distance.
*
* @param {Object} a - Gas Station
* @param {Object} b - Gas Station
*
* @returns {number} Sorting weight.
*/
function sortByDistance(a, b) {
if (b.dist === 0) {
return Number.MIN_SAFE_INTEGER;
} else if (a.dist === 0) {
return Number.MAX_SAFE_INTEGER;
}

return a.dist - b.dist;
}

/**
* @function filterStations
* @description Helper function to filter gas stations.
Expand Down Expand Up @@ -323,18 +293,12 @@ async function getData() {
const stationsFiltered = stations.filter(filterStations);
stationsFiltered.forEach(normalizeStations);

const distance = stationsFiltered.slice(0);
distance.sort(sortByDistance);

const price = stationsFiltered.slice(0);
price.sort(sortByPrice);

return {
types: ['diesel', 'e5', 'e10'],
unit: 'kilometer',
currency: 'EUR',
byPrice: price,
byDistance: distance
byPrice: _.sortBy(stationsFiltered, sortByPrice.bind(null, config)),
byDistance: _.sortBy(stationsFiltered, 'dist')
};
}

Expand Down
39 changes: 15 additions & 24 deletions apis/utils/index.js
Expand Up @@ -7,6 +7,12 @@
* @see https://github.com/fewieden/MMM-Fuel
*/

/**
* @external lodash
* @see https://www.npmjs.com/package/lodash
*/
const _ = require('lodash');

/**
* @function filterStations
* @description Helper function to filter gas stations.
Expand All @@ -16,22 +22,9 @@
* @returns {boolean} To keep or filter the station.
*/
function filterStations(station) {
const prices = Object.keys(station.prices);
const prices = _.keys(station.prices);

return !prices.every(type => station.prices[type] === -1);
}

/**
* @function sortByDistance
* @description Helper function to sort gas stations by distance.
*
* @param {Object} a - Gas Station
* @param {Object} b - Gas Station
*
* @returns {number} Sorting weight.
*/
function sortByDistance(a, b) {
return a.distance - b.distance;
return !_.every(prices, type => _.get(station, `prices.${type}`) === -1);
}

/**
Expand All @@ -45,8 +38,8 @@ function sortByDistance(a, b) {
*/
function fillMissingPrices(config, station, maxPricesByType) {
for (const type of config.types) {
if (!station.prices[type]) {
station.prices[type] = `>${maxPricesByType[type]}`;
if (!_.get(station, `prices.${type}`)) {
_.set(station, `prices.${type}`, `>${maxPricesByType[type]}`);
}
}
}
Expand All @@ -60,15 +53,14 @@ function fillMissingPrices(config, station, maxPricesByType) {
*
* @returns {number} Sorting weight.
*/
function sortByPrice(config, a, b) {
const aPrice = a.prices[config.sortBy];
const bPrice = b.prices[config.sortBy];
function sortByPrice(config, station) {
const price = _.get(station, `prices.${config.sortBy}`);

if (!isNaN(aPrice) || !isNaN(bPrice)) {
return isNaN(aPrice) ? 1 : -1;
if (_.isNumber(price)) {
return price;
}

return 0;
return Number.POSITIVE_INFINITY;
}

/**
Expand Down Expand Up @@ -108,6 +100,5 @@ module.exports = {
fillMissingPrices,
filterStations,
mergePrices,
sortByDistance,
sortByPrice
};
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "mmm-fuel",
"version": "2.4.0",
"version": "2.5.0",
"description": "Gas Station price Module for MagicMirror2",
"scripts": {
"lint": "eslint . && stylelint **/*.css",
Expand Down Expand Up @@ -29,6 +29,7 @@
"stylelint-config-standard": "^27.0.0"
},
"dependencies": {
"lodash": "^4.17.21",
"moment": "^2.29.4",
"node-fetch": "^2.6.7",
"node-html-parser": "^5.4.1"
Expand Down

0 comments on commit eafb711

Please sign in to comment.