-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (37 loc) · 1.22 KB
/
index.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
const desicionMaker = require('./lib/desicion-maker.js');
const logger = require('./lib/logger');
const { fetchFeed, parseFeed } = require('./lib/feed-handler');
const { fetchFile, storeFile } = require('./lib/file-storage');
const firebase = require('./lib/firebase');
const logHelpers = require('./utils/logHelpers')
const { decideAndDownloadError } = require('./lib/error-handler');
const endScript = () => {
firebase.admin.database().goOffline();
logger.log('info','Script finished running')
logger.close()
}
const downloadDecideAndStore = async (posts) => {
const shows = await firebase.getShowTitles();
for(let post of posts) {
let download = false;
try {
download = await desicionMaker.decide(post, shows);
if(download) {
const file = await fetchFile(post);
const success = await storeFile(file, post);
logHelpers.logFileDownloaded(post, success);
}
} catch (e) {
decideAndDownloadError(e, post)
}
}
}
const initiateFetch = async (feedUrl) => {
const feed = await fetchFeed(feedUrl);
const posts = await parseFeed(feed);
await downloadDecideAndStore(posts);
endScript();
}
firebase.getStaticUrl(async feedUrl => {
initiateFetch(feedUrl);
});