-
Notifications
You must be signed in to change notification settings - Fork 4
/
MMM-next-episode.js
147 lines (128 loc) · 5.66 KB
/
MMM-next-episode.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
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
139
140
141
142
143
144
145
146
147
/* global Module Log */
Module.register("MMM-next-episode", {
defaults: {
id: "",
hash_key: "",
displaySeasonAndEpisode: false,
displayShowingOn: false,
displayEpisodeName: false,
maxdays: 7,
ShowThumbnail: true,
ThumbnailSize: "medium"
},
getStyles () {
return ["MMM-next-episode.css"];
},
start () {
Log.log("MMM-next-episode started!");
this.shows = [];
this.qrCode = null;
this.sendSocketNotification("GET_DATA", this.config);
},
socketNotificationReceived (notification, payload) {
Log.debug("next-episode, Received socket notification: ", notification, " with payload: ", payload);
if (notification === "DATA") {
Log.log("next-episode, Received DATA notification with payload: ", payload);
this.shows = this.processData(payload);
this.updateDom();
} else if (notification === "QR_CODE") {
Log.log("next-episode, Received QR_CODE notification with payload: ", payload);
this.qrCode = payload;
this.updateDom();
}
},
processData (data) {
Log.log("next-episode, Processing data: ", data);
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(data, "text/xml");
const processedData = [];
const sizeMapping = {
small: "little",
medium: "thumb",
large: "big"
};
const results = xmlDoc.getElementsByTagName("result");
for (let i = 0; i < results.length; i++) {
try {
const result = results[i];
const showData = {
id: result.getElementsByTagName("showid")[0].textContent,
time: result.getElementsByTagName("hour")[0].textContent,
season: result.getElementsByTagName("seasonNumber")[0].textContent,
episode: result.getElementsByTagName("episodeNumber")[0].textContent,
episodeName: result.getElementsByTagName("episodeName")[0].textContent.replace('<!--//--><![CDATA[//>', '').replace('<!--', '').replace('//--><!]]>', ''),
showingOn: result.getElementsByTagName("channel_name")[0].textContent.replace('<!--//--><![CDATA[//>', '').replace('<!--', '').replace('//--><!]]>', ''),
showName: result.getElementsByTagName("title")[0].textContent,
thumbnail: `https://static.next-episode.net/tv-shows-images/${sizeMapping[this.config.ThumbnailSize]}/${result.getElementsByTagName("imageUrl")[0].textContent.split("/").pop().split("?")[0].replace(".jpg", "")}.jpg`,
airDate: result.getElementsByTagName("countdown")[0].textContent
};
Log.log("next-episode, Processed show data: ", showData);
processedData.push(showData);
} catch (error) {
Log.error("next-episode, Error occurred when processing data: ", error);
}
}
Log.log("next-episode, Final processed data: ", processedData);
return processedData;
},
getDom () {
Log.log("next-episode, Creating DOM elements");
const wrapper = document.createElement("div");
wrapper.className = "MMM-next-episode";
if (this.qrCode) {
const img = document.createElement("img");
img.src = this.qrCode;
wrapper.appendChild(img);
} else {
this.shows.forEach((show) => {
const airDateDays = parseInt(show.airDate.split(" ").filter((word) => !isNaN(word))[0], 10);
Log.log(`next-episode, airDateDays: ${airDateDays}`);
if (isNaN(airDateDays) || airDateDays <= this.config.maxdays) {
Log.log("next-episode, Creating DOM element for show: ", show.showName, " with season and episode: S", show.season, "E", show.episode, " and air date: ", show.airDate);
const showElement = document.createElement("div");
showElement.className = "show-element";
if (this.config.ShowThumbnail) {
const img = document.createElement("img");
img.src = show.thumbnail;
img.className = "show-thumbnail";
showElement.appendChild(img);
}
const capitalizedShowName = show.showName.charAt(0).toUpperCase() + show.showName.slice(1);
const showDetails = document.createElement("div");
showDetails.className = this.config.ShowThumbnail
? "show-details thumbnail-enabled"
: "show-details thumbnail-disabled";
const showNameElement = document.createElement("div");
showNameElement.className = "show-name";
showNameElement.innerHTML = capitalizedShowName;
showDetails.appendChild(showNameElement);
if (this.config.displaySeasonAndEpisode) {
const episodeElement = document.createElement("div");
episodeElement.className = "show-episode";
episodeElement.innerHTML = `S${show.season}E${show.episode}`;
showDetails.appendChild(episodeElement);
}
if (this.config.displayEpisodeName && show.episodeName != "") {
const episodeNameElement = document.createElement("div");
episodeNameElement.className = "show-episode";
episodeNameElement.innerHTML = show.episodeName;
showDetails.appendChild(episodeNameElement);
}
const airDateElement = document.createElement("div");
var airDateString = "";
if (this.config.displayShowingOn && show.showingOn != "") {
airDateString = `On ${show.showingOn} `;
}
airDateElement.className = "show-airdate";
airDateString = airDateString + show.airDate;
airDateElement.innerHTML = airDateString;
showDetails.appendChild(airDateElement);
showElement.appendChild(showDetails);
wrapper.appendChild(showElement);
}
});
}
return wrapper;
}
});
// @Created By Pierre Gode