-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetPodcast.js
More file actions
executable file
·77 lines (71 loc) · 2.26 KB
/
Copy pathgetPodcast.js
File metadata and controls
executable file
·77 lines (71 loc) · 2.26 KB
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
69
70
71
72
73
74
75
76
77
#!/usr/bin/env node
//Usage ./getPodcast.js pid
//would generate pid.json from pid.txt
const fs = require('fs');
const pidJSONpath = pid => `./${pid}.json`;
const split = require('./lib/split.js');
//const Pid = '287';
const Pid = process.argv[2];
const ProgOf = require('./lib/getProgOf.js')();
const Programme = {
name: ProgOf[Pid],
latest: '1900-01-01',
pages: [],
years: []
};
const lastLatest = Programme['latest'];
const updateProgramme = (date, titleCaptions, audio) => {
const [title, caption] = split(date, titleCaptions, Pid, ProgOf[Pid]);
const getPageIndex = title => {
for (let i = 0; i < Programme['pages'].length; i++) {
if (Programme['pages'][i]['title'] === title) return i;
}
const page = {
broadcast_date: '',
title: title,
episodes: 0,
podcasts: []
};
Programme['pages'].unshift(page);
return 0;
};
const pageIndex = getPageIndex(title);
const episode = {caption: caption, url: audio};
Programme['pages'][pageIndex]['broadcast_date'] = date;
Programme['pages'][pageIndex]['podcasts'].unshift(episode);
Programme['pages'][pageIndex]['episodes']++;
};
let hasNewEpisode = false;
const dtm = fs.readFileSync(`./${Pid}.txt`, {encoding:'utf8', flag:'r'}).replace(/\n+$/, "").split('\n');
dtm.forEach(e => {
[date, titleCaption, audio] = e.split(' ');
if (!hasNewEpisode) hasNewEpisode = true;
if (Programme['latest'] === lastLatest) Programme['latest'] = date;
updateProgramme(date, titleCaption.replace(/_/g, ' '), audio);
});
if (hasNewEpisode) {
Programme['pages'].sort((a, b) => {
const A = a.broadcast_date, B = b.broadcast_date;
return (A < B) ? -1 : ((A > B) ? 1 : 0);
});
let prgYear = {
name: '',
podcasts: []
};
Programme['pages'].forEach(page => {
const podcast = {
title: page['title'],
episodes: page['episodes']
};
if (prgYear['name'] !== page['broadcast_date'].substr(0,4)) {
if (prgYear['name'].length > 0) {
Programme['years'].push(prgYear);
}
prgYear = {name: page['broadcast_date'].substr(0,4), podcasts: [ podcast ]};
} else {
prgYear['podcasts'].push(podcast);
}
});
Programme['years'].push(prgYear);
fs.writeFileSync(pidJSONpath(Pid), JSON.stringify(Programme));
}