-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
106 lines (88 loc) · 3.09 KB
/
node_helper.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
const NodeHelper = require("node_helper");
const https = require("https");
const http = require("http");
const url = require("url");
module.exports = NodeHelper.create({
start: function() {
console.log("Starting node helper for: " + this.name);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "START_SONARR") {
this.config = payload;
this.getUpcoming();
this.getHistory();
this.scheduleUpdate();
}
},
scheduleUpdate: function() {
setInterval(() => {
this.getUpcoming();
this.getHistory();
}, this.config.updateInterval);
},
getUpcoming: function() {
const apiUrl = `${this.config.baseUrl}/api/v3/calendar`;
const currentDate = new Date();
const futureDate = new Date(currentDate.getTime() + 24 * 24 * 60 * 60 * 1000); // 24 days from now
const params = new URLSearchParams({
start: currentDate.toISOString(),
end: futureDate.toISOString(),
includeSeries: "true",
includeEpisodeFile: "false"
});
this.sendRequest(apiUrl, params, (response) => {
if (!Array.isArray(response)) {
console.error("Unexpected response format from Sonarr API");
return;
}
this.sendSocketNotification("SONARR_UPCOMING", response);
});
},
getHistory: function() {
const apiUrl = `${this.config.baseUrl}/api/v3/history`;
const params = new URLSearchParams({
page: 1,
pageSize: 50, // Fetch more items and limit in MMM-Sonarr.js
sortKey: "date",
sortDirection: "descending",
includeSeries: true,
includeEpisode: true,
eventType: 1,
});
this.sendRequest(apiUrl, params, (response) => {
this.sendSocketNotification("SONARR_HISTORY", response.records);
});
},
sendRequest: function(apiUrl, params, callback) {
const fullUrl = `${apiUrl}?${params.toString()}`;
const parsedUrl = url.parse(fullUrl);
const options = {
hostname: parsedUrl.hostname,
port: parsedUrl.port,
path: parsedUrl.path,
method: 'GET',
headers: {
"X-Api-Key": this.config.apiKey,
},
};
const protocol = parsedUrl.protocol === 'https:' ? https : http;
const req = protocol.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
const jsonData = JSON.parse(data);
callback(jsonData);
} catch (error) {
console.error("Error parsing JSON:", error);
}
});
});
req.on('error', (error) => {
console.error("Error fetching Sonarr data:", error);
});
req.end();
},
});