-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddon.js
292 lines (255 loc) · 10.6 KB
/
addon.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
require('dotenv').config();
const { addonBuilder, serveHTTP } = require("stremio-addon-sdk");
const { Client } = require('discord-rpc');
const axios = require('axios');
const fs = require('fs');
const clientId = process.env.DISCORD_CLIENT_ID;
const tmdbApiKey = process.env.TMDB_API_KEY;
const tmdbAccessToken = process.env.TMDB_ACCESS_TOKEN;
const runningLocally = process.env.RUNNING_LOCALLY === 'true';
let rpc;
let startTimestamp = Math.floor(Date.now() / 1000); // Store the start time when Stremio was opened
let resetActivityTimeout;
// Fun randomized phrases for browsing
const browsingPhrases = [
'Exploring the vast world of cinema...',
'Hunting for the next big binge...',
'Diving into the archives...',
'On the quest for entertainment...',
'Scrolling through endless possibilities...'
];
// Fun randomized phrases for watching details
const watchingPhrases = [
'Captivated by',
'Engrossed in',
'Immersed in',
'Glued to the screen with',
'Watching the masterpiece:'
];
// Discord RPC connection only if running locally
if (runningLocally) {
rpc = new Client({ transport: 'ipc' });
rpc.on('ready', () => {
console.log('Connected to Discord!');
setDefaultActivity(); // Set the default activity when the addon starts
});
rpc.login({ clientId }).catch((error) => {
console.error('Error connecting to Discord:', error);
});
}
const manifest = {
id: "org.stremio.discordpresence",
version: "1.0.0",
name: "Discord Rich Presence Addon",
description: "Addon that updates Discord status with the current stream from Stremio.",
resources: ["meta", "subtitles"],
types: ["movie", "series", "channel"],
logo: "/assets/discord-logo.png",
catalogs: [],
idPrefixes: ["tt", "yt"]
};
const builder = new addonBuilder(manifest);
// Function to fetch image from TMDB using IMDb ID or from other sources for YouTube
async function fetchImage(imdbId) {
if (imdbId.startsWith('yt')) {
return 'https://img.youtube.com/vi/' + imdbId.split(':')[1] + '/hqdefault.jpg'; // Use YouTube thumbnail
} else {
try {
const tmdbUrl = `https://api.themoviedb.org/3/find/${imdbId}?external_source=imdb_id&api_key=${tmdbApiKey}`;
const response = await axios.get(tmdbUrl, {
headers: {
Authorization: `Bearer ${tmdbAccessToken}`,
accept: 'application/json'
}
});
const movie = response.data.movie_results[0];
if (movie) {
const posterPath = movie.poster_path;
return `https://image.tmdb.org/t/p/w500${posterPath}`;
} else {
return null;
}
} catch (error) {
console.error(`Error fetching image for ${imdbId}:`, error);
return null;
}
}
}
// Function to fetch series episode image and name from TMDB
async function fetchSeriesData(imdbId, season, episode) {
try {
const tmdbUrl = `https://api.themoviedb.org/3/find/${imdbId}?external_source=imdb_id&api_key=${tmdbApiKey}`;
const response = await axios.get(tmdbUrl, {
headers: {
Authorization: `Bearer ${tmdbAccessToken}`,
accept: 'application/json'
}
});
if (response.data.tv_results.length > 0) {
const series = response.data.tv_results[0];
const episodeUrl = `https://api.themoviedb.org/3/tv/${series.id}/season/${season}/episode/${episode}?api_key=${tmdbApiKey}`;
const episodeResponse = await axios.get(episodeUrl, {
headers: {
Authorization: `Bearer ${tmdbAccessToken}`,
accept: 'application/json'
}
});
const episodeData = episodeResponse.data;
return {
name: `${series.name} - ${episodeData.name}`,
poster: episodeData.still_path ? `https://image.tmdb.org/t/p/w500${episodeData.still_path}` : `https://image.tmdb.org/t/p/w500${series.poster_path}`,
};
}
} catch (error) {
console.error(`Error fetching series data for ${imdbId}:`, error);
return null;
}
}
// Function to fetch YouTube video details
async function fetchYouTubeDetails(videoId) {
try {
const response = await axios.get(`https://www.googleapis.com/youtube/v3/videos?part=snippet&id=${videoId}&key=${process.env.YOUTUBE_API_KEY}`);
if (response.data.items.length > 0) {
const video = response.data.items[0].snippet;
return {
title: video.title,
creator: video.channelTitle,
thumbnail: video.thumbnails.high.url
};
}
return null;
} catch (error) {
console.error(`Error fetching YouTube video details for ${videoId}:`, error);
return null;
}
}
// Function to set the default activity
function setDefaultActivity() {
const randomBrowsingPhrase = browsingPhrases[Math.floor(Math.random() * browsingPhrases.length)];
if (runningLocally && rpc) {
rpc.setActivity({
details: 'Stremio is open',
state: randomBrowsingPhrase,
largeImageKey: 'https://play-lh.googleusercontent.com/k3489BQdgNeGZKMV8HIOMVZlMyY2EXkiWB0MN6yTl5omfd3_MCSCU75UTXqwh-7j-Qs', // Stremio image provided
largeImageText: 'Stremio',
startTimestamp: startTimestamp, // Start time when Stremio was opened
instance: false,
});
console.log(`Discord Rich Presence updated: ${randomBrowsingPhrase}`);
}
}
// Function to update Discord Rich Presence when playing content
async function updatePlayActivity(id, stream) {
clearTimeout(resetActivityTimeout); // Clear any existing timeout when playing starts
let largeImageKey = 'default_image_key'; // Default image key
let name = stream.name;
let stateText = '';
if (stream.type === 'series') {
const seriesData = await fetchSeriesData(id.split(':')[0], stream.season, stream.episode);
if (seriesData) {
largeImageKey = seriesData.poster || largeImageKey;
name = seriesData.name;
}
} else if (stream.type === 'channel' && id.startsWith('yt')) {
const videoId = id.split(':')[1];
const youtubeDetails = await fetchYouTubeDetails(videoId);
if (youtubeDetails) {
largeImageKey = youtubeDetails.thumbnail;
name = youtubeDetails.title;
stateText = `by ${youtubeDetails.creator}`;
}
} else {
largeImageKey = await fetchImage(id) || largeImageKey; // Fetch image for movies or channels
}
const randomWatchingPhrase = watchingPhrases[Math.floor(Math.random() * watchingPhrases.length)];
if (runningLocally && rpc) {
rpc.setActivity({
details: `${randomWatchingPhrase} ${name}`,
state: stateText || (stream.type === 'series' ? `S${stream.season}:E${stream.episode}` : undefined), // Show season/episode for series or creator for YouTube
startTimestamp: startTimestamp, // Show the total duration since Stremio was opened
largeImageKey: largeImageKey,
largeImageText: name,
smallImageKey: 'stremio_logo',
instance: false,
});
console.log(`Discord Rich Presence updated: ${randomWatchingPhrase} ${name} (${stream.type}) ${stateText || ''}`);
// Set a timeout to reset the activity after 30 seconds of inactivity
resetActivityTimeout = setTimeout(() => {
setDefaultActivity();
console.log(`No activity detected for 30 seconds, resetting DCRP to default.`);
}, 30000); // 30 seconds timeout
}
}
// Subtitle handler to detect when a stream is actually being watched
builder.defineSubtitlesHandler(async function(args) {
console.log(`Subtitle request received for: ${args.type} with id: ${args.id}`);
const meta = await fetchMetadata(args.id);
if (meta) {
await updatePlayActivity(args.id, meta);
}
return Promise.resolve({ subtitles: [] });
});
// Meta handler to fetch metadata when the user starts watching something
builder.defineMetaHandler(async function(args) {
console.log(`Meta request received for: ${args.type} with id: ${args.id}`);
const meta = await fetchMetadata(args.id);
if (meta) {
await updatePlayActivity(args.id, meta);
}
return Promise.resolve({ meta });
});
// Function to fetch metadata
async function fetchMetadata(id) {
if (id.startsWith('yt')) {
const videoId = id.split(':')[1];
const youtubeDetails = await fetchYouTubeDetails(videoId);
if (youtubeDetails) {
return {
id: id,
name: youtubeDetails.title,
type: 'channel', // Treat it as a channel type
creator: youtubeDetails.creator,
thumbnail: youtubeDetails.thumbnail
};
}
} else {
const [imdbId, season, episode] = id.split(':');
const tmdbUrl = `https://api.themoviedb.org/3/find/${imdbId}?external_source=imdb_id&api_key=${tmdbApiKey}`;
try {
const response = await axios.get(tmdbUrl, {
headers: {
Authorization: `Bearer ${tmdbAccessToken}`,
accept: 'application/json'
}
});
if (response.data.tv_results.length > 0 && season && episode) {
// Handle TV series episodes
const series = response.data.tv_results[0];
return {
id: id,
name: series.name,
type: "series",
season: season,
episode: episode,
poster: series.poster_path ? `https://image.tmdb.org/t/p/w500${series.poster_path}` : null
};
} else if (response.data.movie_results.length > 0) {
// Handle movies
const movie = response.data.movie_results[0];
return {
id: id,
name: movie.title,
type: "movie",
poster: movie.poster_path ? `https://image.tmdb.org/t/p/w500${movie.poster_path}` : null
};
}
} catch (error) {
console.error(`Error fetching metadata for ${id}:`, error);
return null;
}
}
return null;
}
module.exports = builder.getInterface();
serveHTTP(builder.getInterface(), { port: process.env.PORT || 4000 });
console.log(`Stremio Discord Presence Addon is running on port ${process.env.PORT || 4000}`);