mirror of
https://github.com/orangecoding/fredy.git
synced 2026-06-16 12:31:07 +00:00
Release v1.0.0 🎉
This commit is contained in:
42
lib/services/scraper.js
Executable file
42
lib/services/scraper.js
Executable 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
25
lib/services/stats.js
Normal 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
36
lib/services/store.js
Executable 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;
|
||||
Reference in New Issue
Block a user