-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMMM-Podcast.js
94 lines (94 loc) · 3.13 KB
/
MMM-Podcast.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
Module.register("MMM-Podcast",{
// Default module config.
defaults: {
//download time for video
updateVideoHours: 4,
updateVideoMinutes: 0,
//add your static server url here (if needed)
videoUrl: '',
//add your feed url here:
//sadly there are not so many English Podcasts:
//feedUrl: 'http://podcastfeeds.nbcnews.com/audio/podcast/MSNBC-MADDOW-NETCAST-M4V.xml',
//German news in 100 seconds:
feedUrl: 'http://www.tagesschau.de/export/video-podcast/webxl/tagesschau-in-100-sekunden/',
},
getScripts: function() {
return [
'https://code.jquery.com/jquery-2.2.3.min.js', // this file will be loaded from the jquery servers.
]
},
start: function() {
Log.info("Starting module: " + this.name);
this.timedUpdate();
},
timedUpdate: function(){
if(this.config.feedUrl !== ''){
this.loadVideo();
}
else{
self.sendSocketNotification('VIDEO_DOWNLOAD', self.config);
}
var self = this;
//calculate Timeout
var now = new Date();
var millisTillDownload = new Date(now.getFullYear(), now.getMonth(), now.getDate(), this.config.updateVideoHours, this.config.updateVideoMinutes, 0, 0) - now;
if (millisTillDownload < 0) {
millisTillDownload += 86400000; // it's after the needed time, try time tomorrow.
}
setTimeout(function(){self.timedUpdate},millisTillDownload);
},
playVideo: function(){
this.sendSocketNotification('VIDEO_CHANGED', this.config);
},
loadVideo: function(){
var yqlURL = 'http://query.yahooapis.com/v1/public/yql'; //yql
var yqlQS = '?format=json&callback=?&q=select%20*%20from%20rss%20where%20url%3D'; //yql query string
var cachebuster = Math.floor((new Date().getTime()) / 1200 / 1000); //yql caches feeds, so we change the feed url every 20min
var url = yqlURL + yqlQS + "'" + encodeURIComponent(this.config.feedUrl) + "'" + "&_nocache=" + cachebuster;
var self = this;
var html = $.getJSON(url, function(data){
//rss feed is loaded so get url
data = data.query.results;
if(data.item[0]){
self.config.videoUrl = data.item[0].enclosure.url;
}
else{
self.config.videoUrl = data.item.enclosure.url;
}
self.sendSocketNotification('VIDEO_DOWNLOAD', self.config);
});
},
socketNotificationReceived: function(notification, payload) {
if (notification === "VIDEO_LOADED"){
this.sendNotification("VIDEO_LOADED", payload)
}
},
notificationReceived: function(notification, payload, sender) {
if(notification === "ALL_MODULES_STARTED"){
this.sendNotification("REGISTER_VOICE_MODULE", {
mode: "PODCAST",
sentences: [
"SHOW PODCAST",
"HIDE PODCAST"
]
});
}
//register MMM-Button click
else if(notification == "BUTTON_PRESSED") {
Log.log(this.name + " received a system notification: " + notification);
this.playVideo();
}
else if(notification === "VOICE_PODCAST" && sender.name === "MMM-voice"){
this.checkCommands(payload);
}
},
// test for your commands
checkCommands: function(data){
if(/(SHOW)/g.test(data) && /(PODCAST)/g.test(data)){
this.playVideo();
}
if(/(HIDE)/g.test(data) && /(PODCAST)/g.test(data)){
this.playVideo();
}
}
});