-
Notifications
You must be signed in to change notification settings - Fork 3
/
populate_download_history.js
106 lines (84 loc) · 2.47 KB
/
populate_download_history.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
98
99
100
101
102
103
104
105
106
/*
Re-populates get_iplayer download history from existing files
e.g. if you move computers and fail to bring the download history across
*/
var fs = require('fs');
var rr = require('recursive-readdir');
var giUtils = require('./giUtils');
var download_history = [];
var entries = [];
//_______________________________________________________________
function check(pathspec,pid_pref) {
var result = false;
filename = pathspec.split('\\');
filename = filename[filename.length-1];
if (filename.indexOf(pid_pref)>=0) {
result = true;
name = filename.split(pid_pref);
desc = name[0].split('_-_');
series = desc[0];
episode = desc[1];
if ((episode) && (episode.charAt(episode.length-1)=='_')) {
episode = episode.slice(0,episode.length-1);
}
if (!episode) {
episode = series;
}
name = name[1].split('.');
pid = name[0].split('_')[0];
pid = pid_pref+pid;
if (giUtils.binarySearch(download_history,pid)<0) {
suffix = name[1];
if ((suffix=='mp4') || (suffix=='mov')) {
type = 'tv';
}
else {
type = 'radio';
}
stat = fs.statSync(pathspec);
var time = Math.floor(stat.mtime.getTime()/1000);
entry = pid+'|'+series+'|'+episode+'|'+type+'|'+time+
'|hlsaacstd1|'+pathspec+'|default,original|1800||||||||||';
console.log(pid+'|'+series+'|'+episode+'|'+type+'|'+time);
download_history.push(pid); //tail end is no longer sorted
entries.push(entry);
}
else {
//console.log('Already in history '+pid+' '+pathspec+' at index '+download_history.indexOf(pid));
}
}
return result;
}
//_____________________________________________________________________________
var dlh_locn;
if (process.argv.length>=3) {
path = process.argv[2];
var config = require('./config.json');
var dlh_locn = config.download_history;
download_history = giUtils.downloadHistory(dlh_locn);
console.log('There are '+download_history.length+' entries in the download_history');
rr(path, function (err, files) {
for (var i in files) {
if (!check(files[i],'b0')) { // Red Bee
if (!check(files[i],'p0')) { // PIPs
check(files[i],'w0'); // world service
}
}
}
});
}
else {
console.log('Usage: '+process.argv[1]+' pathspec');
}
process.on('exit',function(code){
if (entries.length>0) {
console.log('Writing '+entries.length+' new entries');
entry_str = '';
for (var i in entries) {
entry_str += entries[i]+'\n';
}
dlh_fh = fs.openSync(dlh_locn,'a');
fs.appendFileSync(dlh_locn, entry_str, 'utf8');
fs.closeSync(dlh_fh);
}
});