-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.js
68 lines (61 loc) · 2.03 KB
/
fetch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"use strict";
require('dotenv').config();
require('app-module-path').addPath('./app');
const fs = require('fs');
const path = require('path');
const fetch = require('fetching/fetch');
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
if (!process.env.MONGO_URL) {
console.log('Mongo URL is not set in env variables.');
process.exit(1);
}
const fetchers = JSON.parse(fs.readFileSync('fetchers.json', 'utf8'));
const fetchTask = function() {
mongoose.connect(process.env.MONGO_URL);
fetch(fetchers, function (err) {
mongoose.disconnect();
if (err) {
console.error(err);
return;
}
console.log(new Date().toISOString() + ' - Schedules were updated.');
});
};
const testTask = function(fetcherName) {
if (fetcherName.match(/[\\/]/)) {
console.log('Please, specify just a fetcher name, not a path.');
process.exit(1);
}
mongoose.connect(process.env.MONGO_URL);
require(path.join('fetching', 'fetchers', fetcherName)).fetch(function(err, rawShowsData) {
mongoose.disconnect();
if (err) {
console.error(err);
return;
}
console.log(
"\n==================================================================================================" +
"\n=================================================================================================="
);
rawShowsData.forEach(function(rawShowData) {
console.log(JSON.stringify(rawShowData, null, 2));
});
});
};
const mode = process.argv[2] || 'cron';
switch (mode) {
case 'test':
testTask(process.argv[3]);
break;
case 'once':
fetchTask();
break;
case 'cron':
const timezone = 'Europe/Kiev';
let CronJob = require('cron').CronJob;
new CronJob(process.env.FETCH_CRON_EXPRESSION || '0 0 * * * *', fetchTask, null, true, timezone);
break;
default:
console.log('Valid modes are: once, cron, test.');
}