-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
76 lines (64 loc) · 2.78 KB
/
app.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
'use strict';
const request = require('request-promise');
const cheerio = require('cheerio');
const Homey = require('homey');
const BROADCAST_TITLES = [
'NOS Journaal', 'Nieuwsuur', 'NOS Jeugdjournaal', 'NOS het vragenuurtje', 'NOS Sportjournaal', 'NOS Studio Sport Eredivisie', 'NOS Studio Sport'
];
class NOS extends Homey.App {
async onInit() {
this.log(`${this.manifest.id} is running...`);
this.programMap = {};
for (let name of BROADCAST_TITLES) {
this.programMap[name] = {
token: new Homey.FlowToken(name, { type: 'string', title: name }),
};
this.programMap[name].token.register()
.catch(err => this.error(`failed to register token ${name}`, err));
}
await this.updateBroadcasts();
// Update broadcast urls every 30 minutes
setInterval(() => {
this.updateBroadcasts();
}, 30 * 60 * 1000);
}
updateBroadcasts() {
this.log('updateBroadcasts()');
return request.get('https://nos.nl/uitzendingen/')
.then(res => {
const $ = cheerio.load(res);
$('.broadcast-link').each((i, data) => {
request.get(`https://nos.nl${data.attribs.href}`)
.then(videoRes => {
let $ = cheerio.load(videoRes);
$('source').each((j, test) => {
//this.log(test);
if (test.attribs['data-label'] === 'Hoog - 720p') {
// Parse url, name and date from HTML
const name = $(data.children[0].children[1]).text().trim();
//this.log(name);
const date = new Date(($(data.children[0].children[2]).attr('datetime').split('+')[0]) + 'Z');
//this.log(date);
const url = test.attribs.src.replace('https://', '');
//this.log(url);
if (!BROADCAST_TITLES.includes(name)) return;
// If newer broadcast available, add it
if (typeof this.programMap[name].date === 'undefined' || this.programMap[name].date < date) {
this.programMap[name].url = url;
this.programMap[name].name = name;
this.programMap[name].date = date;
this.programMap[name].token.setValue(url);
}
return;
}
})
});
});
return true;
})
.catch(err => {
this.error('failed to update broadcasts', err);
});
}
}
module.exports = NOS;