epl-fixtures
Version:
Fixtures for Premier League clubs
51 lines (44 loc) • 1.1 kB
JavaScript
;
const fs = require('fs');
const path = require('path');
const htmlParser = require('./htmlParser');
/**
* Load cached file
* @param allOrClubId
* @param callback
*/
exports.loadFromCache = function (allOrClubId, callback) {
const html = fs.readFileSync(this.cachePath(allOrClubId)).toString();
htmlParser(html, callback);
}
/**
* Check whether or not a file exists in cache
* @param allOrClubId
* @returns {boolean}
*/
exports.isCached = function (allOrClubId) {
try {
const cachedFile = fs.statSync(this.cachePath(allOrClubId));
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
return cachedFile.mtime > yesterday;
} catch (e) {
return false;
}
}
/**
* Save HTML to cache
* @param html
* @param allOrClubId
*/
exports.saveToCache = function (html, allOrClubId) {
fs.writeFile(this.cachePath(allOrClubId), html);
}
/**
* Path to cached file
* @param allOrClubId
* @returns {Promise.<*>}
*/
exports.cachePath = function (allOrClubId) {
return path.resolve(__dirname, '../cache/' + allOrClubId + '.html');
}