-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMM-YouTube-Live-Stream.js
More file actions
138 lines (114 loc) · 3.6 KB
/
MMM-YouTube-Live-Stream.js
File metadata and controls
138 lines (114 loc) · 3.6 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* global Module */
/* Magic Mirror
* Module: MMM-YouTube-Live-Stream
*
* By Niek Nijland <ngnijland@gmail.com>
* MIT Licensed.
*/
Module.register('MMM-YouTube-Live-Stream', {
defaults: {
updatesEvery: 5 * 60 * 1000,
notStreamingTimeout: 10 * 1000,
},
start: function () {
Log.info(`Starting module: ${this.name}`);
this.channel = this.config.channel;
this.interval;
this.status = 'loading';
this.videoId;
this.updatesEvery = this.config.updatesEvery;
this.notStreamingTimeout = this.config.notStreamingTimeout;
if (typeof this.channel !== 'string' && this.channel !== '') {
Log.error(
`"${this.channel}" is not a supported value. Please enter a valid channel name`
);
return;
}
if (typeof this.notStreamingTimeout !== 'number') {
Log.error(
`Configuration error: "notStreamingTimeout" should be a number, but is: "${typeof this
.notStreamingTimeout}". Falling back to 10 seconds (${10 * 1000}).`
);
this.updatesEvery = 10 * 1000;
}
if (typeof this.updatesEvery !== 'number') {
Log.error(
`Configuration error: "updatesEvery" should be a number, but is: "${typeof this
.updatesEvery}". Falling back to 5 minutes (${5 * 60 * 1000}).`
);
this.updatesEvery = 5 * 60 * 1000;
}
if (this.notStreamingTimeout > this.updatesEvery) {
Log.error(
'Configuration error: "updatesEvery" should be bigger than the "notStreamingTimeout" config, but is smaller. Please change this in the MagicMirror config.'
);
return;
}
this.startInterval();
},
socketNotificationReceived: function (notification, payload) {
switch (notification) {
case 'CHANNEL_STATUS': {
if (payload.status === 'ERROR') {
Log.error(payload.message);
this.status = 'error';
this.updateDom();
return;
}
if (payload.status === 'DONE') {
if (payload.streaming && this.videoId) {
return;
}
if (payload.streaming) {
this.videoId = payload.videoId;
} else {
this.videoId = '';
}
this.status = 'idle';
this.updateDom();
}
break;
}
default: {
Log.error(
`Socket notivication error: Unknown notification "${notification}" received from node_helper. Please submit an issue in the MMM-YouTube-Live_stream repository.`
);
}
}
},
startInterval: function () {
clearInterval(this.interval);
this.sendSocketNotification('GET_CHANNEL_STATUS', {
channel: this.channel,
timeout: this.notStreamingTimeout,
});
this.interval = setInterval(() => {
this.sendSocketNotification('GET_CHANNEL_STATUS', {
channel: this.channel,
timeout: this.notStreamingTimeout,
});
}, this.updatesEvery);
},
getDom: function () {
const root = document.createElement('div');
if (this.status === 'loading') {
root.innerText = 'Checking channel...';
return root;
}
if (this.status === 'error') {
root.innerText = 'An error occured. Check the console for more info.';
return root;
}
if (!this.videoId) {
root.innerText = 'Channel is not streaming...';
return root;
}
const iframe = document.createElement('iframe');
iframe.className = 'MMM-YouTube-LiveStream__iframe';
iframe.src = `https://www.youtube.com/embed/${this.videoId}?autoplay=1`;
return iframe;
},
getStyles: function () {
return [this.file('css/MMM-YouTube-Live-Stream.css')];
},
});