-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildCaches.js
97 lines (89 loc) · 2.7 KB
/
buildCaches.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'use strict';
var util = require('util');
var path = require('path');
var fs = require('fs');
var readfiles = require('node-readfiles');
var radio = {service:{type:'Radio'},items:{}};
var tv = {service:{type:'TV'},items:{}};
var tvIndex = 1;
var radioIndex = 30000;
var tvProgs = 0;
var radioProgs = 0;
const now = Math.floor(new Date()/1000);
const aMonth = 30*24*60*60;
var heading = '#index|type|name|pid|available|expires|episode|seriesnum|episodenum|versions|duration|desc|channel|categories|thumbnail|timeadded|guidance|web';
function output(obj,filename) {
var s = heading + '\n';
for (var pid in obj.items) {
var item = obj.items[pid];
if (obj.service.type == 'TV') {
s += (tvIndex++)+'|tv|';
tvProgs++;
}
else {
s += (radioIndex++) + '|radio|';
radioProgs++;
}
s += (item.brand.title ? item.brand.title : item.episode.title.split(' - ')[0].split(':')[0]) + '|';
s += item.episode.id + '|';
s += item.published_time.end + '|';
s += (new Date(item.published_time.end)/1000)+aMonth + '|';
s += item.episode.title + '|';
s += '1|';
s += '1|';
s += 'original|';
s += '0|';
s += '|';
s += item.service.id + '|';
s += '|';
s += '|';
s += now + '|';
s += '|';
s += 'http://bbc.co.uk/programmes/'+item.episode.id + '|';
s += '\n';
}
fs.writeFileSync(filename,s,'utf8');
}
function processSchedule(filename) {
var scheduleTxt = fs.readFileSync(filename,'utf8');
var schedule = JSON.parse(scheduleTxt);
if (schedule.items && schedule.items.length) {
for (let item of schedule.items) {
var target = (schedule.service.type == 'TV' ? tv : radio);
if (target.items[item.episode.id]) {
if (new Date(item.published_time.start) < new Date(target.items[item.episode.id].published_time.start)) {
target.items[item.episode.id].published_time.start = item.published_time.start;
}
if (new Date(item.published_time.end) > new Date(target.items[item.episode.id].published_time.end)) {
target.items[item.episode.id].published_time.end = item.published_time.end;
}
}
else {
target.items[item.episode.id] = item;
}
}
}
}
var pathspec = path.resolve('./');
readfiles(pathspec, {readContents: false, filenameFormat: readfiles.FULL_PATH}, function (err) {
if (err) console.log(util.inspect(err));
})
.then(files => {
files = files.sort(function(a,b){
if (a<b) return +1;
if (a>b) return -1;
return 0;
});
for (var file of files) {
if ((file.indexOf('.json')>0) && (file.indexOf('package.json')<0) && (file.indexOf('node_modules')<0)) {
processSchedule(file);
}
}
output(radio,'./radio.cache');
output(tv,'./tv.cache');
console.log('Radio programmes '+radioProgs);
console.log('TV programmes '+tvProgs);
})
.catch(err => {
console.log(util.inspect(err));
});