-
Notifications
You must be signed in to change notification settings - Fork 3
/
giUtils.js
51 lines (42 loc) · 988 Bytes
/
giUtils.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
/*
get_iplayer related functions
*/
var fs = require('fs');
module.exports = {
downloadHistory: function (dlh_locn) {
var download_history = [];
var dlh_str = fs.readFileSync(dlh_locn,'utf8');
var dlh = dlh_str.split('\n');
for (var i in dlh) {
temppid = dlh[i].split('|')[0];
if (temppid.indexOf('vpid/')>=0) {
temppid = temppid.split('vpid/')[1];
temppid = temppid.split('.')[0];
}
download_history.push(temppid);
}
download_history.sort(); // so we can binary search it later
console.log('There are '+download_history.length+' entries in the download_history');
return download_history;
},
binarySearch: function (list, item) {
var min = 0;
var max = list.length - 1;
var guess;
while (min <= max) {
guess = Math.floor((min + max) / 2);
if (list[guess] === item) {
return guess;
}
else {
if (list[guess] < item) {
min = guess + 1;
}
else {
max = guess - 1;
}
}
}
return -1;
}
};