Spaces:
Paused
Paused
File size: 12,310 Bytes
c0b3afa | 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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | const axios = require('axios');
const { pool } = require('../helpers/db');
const log = require('../helpers/logger');
const { addToQueueGET, addToQueuePOST } = require('../helpers/bottleneck_trakt');
const TRAKT_BASE_URL = 'https://api.trakt.tv';
const TRAKT_API_VERSION = '2';
const TRAKT_API_KEY = process.env.TRAKT_CLIENT_ID;
const TRAKT_CLIENT_SECRET = process.env.TRAKT_CLIENT_SECRET;
const TRAKT_REDIRECT_URI = `${process.env.BASE_URL}/callback`;
const makeGetRequest = (url, accessToken = null) => {
const headers = {
'trakt-api-version': TRAKT_API_VERSION,
'trakt-api-key': TRAKT_API_KEY,
};
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`;
} else {
log.debug(`No access token provided, making unauthenticated request.`);
}
return new Promise((resolve, reject) => {
addToQueueGET({
fn: () => axios.get(url, { headers })
.then(response => {
log.debug(`API GET request successful for URL: ${url}`);
resolve(response.data);
})
.catch(error => {
if (error.response && error.response.status === 401) {
log.warn(`Unauthorized request (401) during API GET request for URL: ${url} - ${error.message}`);
} else {
log.error(`Error during API GET request for URL: ${url} - ${error.message}`);
}
reject(error);
})
});
});
};
const makePostRequest = (url, data, accessToken = null) => {
const headers = {
'trakt-api-version': TRAKT_API_VERSION,
'trakt-api-key': TRAKT_API_KEY,
'Content-Type': 'application/json',
};
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`;
}
return new Promise((resolve, reject) => {
addToQueuePOST({
fn: () => axios.post(url, data, { headers })
.then(response => {
log.debug(`API POST request successful for URL: ${url}`);
resolve(response.data);
})
.catch(error => {
log.error(`Error during API POST request for URL: ${url} - ${error.message}`);
reject(error);
})
});
});
};
const exchangeCodeForToken = async (code) => {
try {
const response = await makePostRequest(`${TRAKT_BASE_URL}/oauth/token`, {
code: code,
client_id: TRAKT_API_KEY,
client_secret: TRAKT_CLIENT_SECRET,
redirect_uri: TRAKT_REDIRECT_URI,
grant_type: 'authorization_code',
});
return response;
} catch (error) {
log.error(`Error exchanging authorization code for token: ${error.message}`);
throw error;
}
};
const fetchData = async (endpoint, params = {}, accessToken = null) => {
const queryString = new URLSearchParams(params).toString();
const url = `${TRAKT_BASE_URL}${endpoint}?${queryString}`;
try {
const data = await makeGetRequest(url, accessToken);
log.debug(`Data successfully retrieved from URL: ${url}`);
return data;
} catch (error) {
throw error;
}
};
const refreshTraktToken = async (refreshToken) => {
const payload = {
refresh_token: refreshToken,
client_id: TRAKT_API_KEY,
client_secret: TRAKT_CLIENT_SECRET,
redirect_uri: TRAKT_REDIRECT_URI,
grant_type: 'refresh_token'
};
try {
const response = await axios.post('https://api.trakt.tv/oauth/token', payload, {
headers: { 'Content-Type': 'application/json' }
});
return response.data;
} catch (error) {
if (error.response) {
log.error(`Failed to refresh token: ${JSON.stringify(error.response.data)}`);
} else {
log.error(`Failed to refresh token: ${error.message}`);
}
throw error;
}
};
const updateTokensInDb = async (username, newAccessToken, newRefreshToken) => {
await pool.query(
'UPDATE trakt_tokens SET access_token = $1, refresh_token = $2 WHERE username = $3',
[newAccessToken, newRefreshToken, username]
);
};
const fetchUserHistory = async (username, type, accessToken) => {
const endpoint = `/users/${username}/watched/${type}`;
try {
return await fetchData(endpoint, {}, accessToken);
} catch (error) {
if (error.response && error.response.status === 401) {
throw new Error('token_expired');
} else {
throw error;
}
}
};
async function handleTraktHistory(parsedConfig, filteredResults, type) {
const traktUsername = parsedConfig.traktUsername;
const watchedEmoji = parsedConfig.watchedEmoji || '✔️';
const fetchInterval = process.env.TRAKT_HISTORY_FETCH_INTERVAL || '24h';
const dbType = type === 'movies' ? 'movie' : type === 'series' ? 'show' : type;
const intervalInMs = (() => {
const intervalValue = parseInt(fetchInterval.slice(0, -1), 10);
const intervalUnit = fetchInterval.slice(-1);
switch (intervalUnit) {
case 'h':
return intervalValue * 60 * 60 * 1000;
case 'd':
return intervalValue * 24 * 60 * 60 * 1000;
default:
throw new Error(`Invalid time unit in TRAKT_HISTORY_FETCH_INTERVAL: ${fetchInterval}`);
}
})();
const result = await pool.query(
`SELECT last_fetched_at FROM trakt_tokens WHERE username = $1`,
[traktUsername]
);
const lastFetchedRow = result.rows[0];
const lastFetchedAt = lastFetchedRow ? new Date(lastFetchedRow.last_fetched_at) : null;
const now = new Date();
if (!lastFetchedAt || (now - lastFetchedAt) >= intervalInMs) {
try {
const tokensResult = await pool.query(
`SELECT access_token, refresh_token FROM trakt_tokens WHERE username = $1`,
[traktUsername]
);
const tokensRow = tokensResult.rows[0];
if (tokensRow) {
let { access_token, refresh_token } = tokensRow;
try {
const [movieHistory, showHistory] = await Promise.all([
fetchUserHistory(traktUsername, 'movies', access_token),
fetchUserHistory(traktUsername, 'shows', access_token)
]);
await Promise.all([
saveUserWatchedHistory(traktUsername, movieHistory),
saveUserWatchedHistory(traktUsername, showHistory)
]);
} catch (error) {
if (error.message === 'token_expired') {
log.warn(`Token expired for user ${traktUsername}, refreshing token...`);
const newTokens = await refreshTraktToken(refresh_token);
access_token = newTokens.access_token;
refresh_token = newTokens.refresh_token;
await updateTokensInDb(traktUsername, newTokens.access_token, newTokens.refresh_token);
const [movieHistory, showHistory] = await Promise.all([
fetchUserHistory(traktUsername, 'movies', newTokens.access_token),
fetchUserHistory(traktUsername, 'shows', newTokens.access_token)
]);
await Promise.all([
saveUserWatchedHistory(traktUsername, movieHistory),
saveUserWatchedHistory(traktUsername, showHistory)
]);
} else {
throw error;
}
}
await pool.query(
`UPDATE trakt_tokens SET last_fetched_at = $1 WHERE username = $2`,
[now.toISOString(), traktUsername]
);
}
} catch (error) {
log.error(`Error fetching Trakt history for user ${traktUsername}: ${error.message}`);
}
}
const traktIdsResult = await pool.query(
`SELECT tmdb_id FROM trakt_history WHERE username = $1 AND type = $2 AND tmdb_id IS NOT NULL`,
[traktUsername, dbType]
);
log.debug(`Fetching Trakt history for user ${traktUsername} with type ${dbType}. Result: ${traktIdsResult.rows.length} items found.`);
const traktIds = traktIdsResult.rows.map(row => `tmdb:${row.tmdb_id}`);
return filteredResults.map(content => {
const contentId = `tmdb:${content.id}`;
if (traktIds.includes(contentId)) {
content.title = `${watchedEmoji} ${content.title || content.name}`;
}
return content;
});
}
const saveUserWatchedHistory = async (username, history) => {
if (!history || history.length === 0) {
log.warn(`No history to save for user ${username}.`);
return;
}
const client = await pool.connect();
try {
await client.query('BEGIN');
for (const item of history) {
const media = item.movie || item.show;
const mediaId = media.ids.imdb || media.ids.tmdb;
const mediaType = item.movie ? 'movie' : 'show';
const watchedAt = item.last_watched_at;
const title = media.title;
const historyResult = await client.query(
`SELECT id FROM trakt_history WHERE username = $1 AND imdb_id = $2`,
[username, media.ids.imdb]
);
if (historyResult.rows.length > 0) {
await client.query(
`UPDATE trakt_history
SET watched_at = $1, title = $2, tmdb_id = $3, type = $4
WHERE id = $5`,
[watchedAt, title, media.ids.tmdb, mediaType, historyResult.rows[0].id]
);
} else {
await client.query(
`INSERT INTO trakt_history (username, imdb_id, tmdb_id, type, watched_at, title)
VALUES ($1, $2, $3, $4, $5, $6)`,
[username, media.ids.imdb, media.ids.tmdb, mediaType, watchedAt, title]
);
}
}
await client.query('COMMIT');
log.info(`History saved for user ${username}`);
} catch (err) {
await client.query('ROLLBACK');
log.error(`Error committing transaction for user ${username}: ${err.message}`);
throw err;
} finally {
client.release();
}
};
const fetchUserProfile = async (accessToken) => {
const endpoint = '/users/me';
return await fetchData(endpoint, {}, accessToken);
};
async function lookupTraktId(tmdbId, type, accessToken) {
const url = `${TRAKT_BASE_URL}/search/tmdb/${tmdbId}?type=${type}`;
try {
const response = await makeGetRequest(url, accessToken);
if (response.length > 0 && response[0].type === type && response[0][type]) {
const traktId = response[0][type].ids.trakt;
return traktId;
} else {
throw new Error(`No Trakt ID found for TMDB ID ${tmdbId}`);
}
} catch (error) {
log.error(`Error fetching Trakt ID for TMDB ID ${tmdbId}: ${error.message}`);
throw error;
}
}
const markContentAsWatched = async (access_token, type, id, watched_at) => {
const url = `${TRAKT_BASE_URL}/sync/history`;
let data = {};
if (type === 'movies') {
data = {
movies: [{ ids: { trakt: id }, watched_at }]
};
} else if (type === 'series') {
data = {
shows: [{ ids: { trakt: id }, watched_at }]
};
}
try {
const response = await makePostRequest(url, data, access_token);
return response;
} catch (error) {
log.error(`Error marking content as watched: ${error.message}`);
throw error;
}
};
module.exports = { makeGetRequest, makePostRequest, fetchUserHistory, fetchUserProfile, exchangeCodeForToken, handleTraktHistory, markContentAsWatched, lookupTraktId, saveUserWatchedHistory };
|