-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get_Sonarr_New_Episodes.js
110 lines (90 loc) · 3.79 KB
/
Get_Sonarr_New_Episodes.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
const API_KEY = 'YOUR API KEY';
const SERVER = 'YOUR SERVER';
// Main function to fetch and display data
async function main() {
// Function to fetch data from given API URL
async function fetchData(apiUrl) {
const req = new Request(apiUrl);
req.headers = {
"Content-Type": "application/json"
};
const json = await req.loadJSON();
return json;
}
// Function to keep a specified number of random elements in the array
function keepRandomElements(array, count) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
array.splice(count);
}
// Define date variables
const currentDate = new Date();
const sixDaysInMilliseconds = 6 * 24 * 60 * 60 * 1000;
const futureDate = new Date(currentDate.getTime() + sixDaysInMilliseconds);
const start = currentDate.toISOString();
const end = futureDate.toISOString();
// Fetch data from APIs
const json = await fetchData(`${SERVER}/api/v3/calendar?apikey=${API_KEY}&start=${start}&end=${end}&includeEpisodeImages=true&includeSeries=true`);
// Randomize and limit the elements if necessary
if (json.length > 3) {
keepRandomElements(json, 3);
}
// Create a widget
let widget = new ListWidget();
widget.backgroundColor = new Color('#222');
widget.addSpacer(20);
// Display data in the widget
if (json.length === 0) {
const hStack = widget.addStack();
hStack.layoutHorizontally();
hStack.addSpacer(20);
const noNewEpisodes = hStack.addText('No New Episodes');
noNewEpisodes.font = Font.boldSystemFont(60);
noNewEpisodes.textColor = Color.white();
} else {
for (const item of json) {
const seriesTitle = item.series.title;
const seasonNumber = item.seasonNumber;
const posterImage = item.series.images.find((image) => image.coverType === 'poster');
const posterUrl = posterImage ? posterImage.url : 'No poster URL found';
const episodeTitle = item.title;
const episodeNumber = item.episodeNumber;
const episodeOverview = item.overview || '';
const hStack = widget.addStack();
hStack.layoutHorizontally();
hStack.addSpacer(20);
const req = new Request(posterUrl);
const image = await req.loadImage();
const poster = hStack.addImage(image);
poster.imageSize = new Size(60, 80);
hStack.addSpacer(10);
const vStack = hStack.addStack();
vStack.layoutVertically();
const title = vStack.addText(seriesTitle);
title.font = Font.boldSystemFont(16);
title.textColor = Color.white();
const episodeInfo = `S${seasonNumber}E${episodeNumber}: ${episodeTitle}`;
const episodeText = vStack.addText(episodeInfo);
episodeText.font = Font.systemFont(9);
episodeText.textColor = Color.gray();
vStack.addSpacer(10);
const episodeTextOverview = vStack.addText(episodeOverview);
episodeTextOverview.font = Font.systemFont(9);
episodeTextOverview.textColor = Color.gray();
vStack.addSpacer(10);
hStack.addSpacer(); // Add this to push to the left size
widget.addSpacer(20);
}
}
// Display the widget
if (config.runsInWidget) {
Script.setWidget(widget);
Script.complete();
} else {
await widget.presentLarge();
}
}
// Call the main function
main();