Release v1.0.0 🎉

This commit is contained in:
Christian Kellner
2018-01-20 20:23:27 +01:00
commit c6cffe029d
33 changed files with 2168 additions and 0 deletions

42
lib/services/scraper.js Executable file
View File

@@ -0,0 +1,42 @@
const makeDriver = require('request-x-ray');
const config = require('../../conf/config.json');
const Xray = require('x-ray');
class Scraper {
constructor() {
const filters = {
removeNewline: this._removeNewline,
trim: this._trim,
int: this._int
};
const driver = makeDriver({
headers: {
'User-Agent': config.userAgent
}
});
const xray = Xray({ filters });
xray.driver(driver);
this.xray = xray;
}
get x() {
return this.xray;
}
_removeNewline(value) {
return typeof value === 'string' ? value.replace(/\\n/g, '') : value;
}
_trim(value) {
return typeof value === 'string' ? value.replace(/\s+/g, ' ').trim() : value;
}
_int(value) {
return typeof value === 'string' ? parseInt(value, 10) : value;
}
}
module.exports = new Scraper().x;

25
lib/services/stats.js Normal file
View File

@@ -0,0 +1,25 @@
const config = require('../../conf/config.json');
let lastScrape = {};
if (config.enableStats) {
const http = require('http');
http
.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
config,
lastScrape
})
);
})
.listen(config.statsPort, '127.0.0.1');
}
exports.setLastScrape = (serviceName, numberFound) => {
lastScrape[serviceName] = lastScrape[serviceName] || [];
lastScrape[serviceName].push({
scapeTime: new Date().toString(),
numberFound: numberFound
});
};

36
lib/services/store.js Executable file
View File

@@ -0,0 +1,36 @@
const path = require('path');
const DB_PATH = path.dirname(require.main.filename) + '/conf/store.json';
const FileAsync = require('lowdb/adapters/FileAsync');
const adapter = new FileAsync(DB_PATH);
const low = require('lowdb');
const lowdb = low(adapter);
class Store {
constructor(name) {
this._name = name;
this._db = null;
}
get warmup() {
return new Promise(resolve => {
lowdb.then(db => {
this._db = db;
resolve();
});
});
}
set knownListings(value) {
if (!Array.isArray(value)) throw Error('Not a valid array');
return this._db.set(this._name, value).write();
}
get knownListings() {
return this._db.get(this._name).value() || [];
}
}
module.exports = Store;