-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerateURLs.js
116 lines (100 loc) · 4.51 KB
/
generateURLs.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
/**
* Generates URLs for various movie or series related websites based on the provided item type, configuration, and JSON data.
* @param {string} item_type - The type of item (movie or series).
* @param {object} config - The configuration object containing base URLs for different websites.
* @param {object} json - The JSON data containing information about the item.
* @returns {object} - An object containing URLs for AlloCiné, BetaSeries, IMDb, Letterboxd, Metacritic, RottenTomatoes, SensCritique, TheMovieDatabase, TheTVDB, Trakt, TVTime, and an isActive flag.
* @throws {Error} - If an invalid AlloCiné URL is provided or if The Movie Database ID is not found.
*/
const generateURLs = (item_type, config, json) => {
const baseURLType =
item_type === "movie" ? config.baseURLTypeFilms : config.baseURLTypeSeries;
const baseURLCriticDetails =
item_type === "movie"
? config.baseURLCriticDetailsFilms
: config.baseURLCriticDetailsSeries;
const allocineURL = json.URL;
const allocineIdMatch = allocineURL.match(/=(.*)\./);
if (!allocineIdMatch) {
throw new Error(`Invalid AlloCiné URL: ${allocineURL}`);
}
const allocineId = parseInt(allocineIdMatch.pop());
const allocineHomepage = `${config.baseURLAllocine}${baseURLType}${allocineId}.html`;
const allocineCriticsDetails = `${config.baseURLAllocine}${baseURLCriticDetails}${allocineId}${config.endURLCriticDetails}`;
const imdbId = json.IMDB_ID;
const imdbHomepage = `${config.baseURLIMDB}${imdbId}/`;
let betaseriesId = json.BETASERIES_ID;
const betaseriesHomepage =
item_type === "movie"
? `${config.baseURLBetaseriesFilm}${betaseriesId}`
: `${config.baseURLBetaseriesSerie}${betaseriesId}`;
if (betaseriesId.startsWith("serie/")) {
const betaseriesIdNew = betaseriesId.split("/");
betaseriesId = betaseriesIdNew[1];
}
const metacriticId = json.METACRITIC_ID;
const metacriticHomepage =
item_type === "movie"
? `${config.baseURLMetacriticFilm}${metacriticId}`
: `${config.baseURLMetacriticSerie}${metacriticId}`;
const rottenTomatoesId = json.ROTTEN_TOMATOES_ID;
const rottenTomatoesHomepage =
item_type === "movie"
? `${config.baseURLRottenTomatoesFilm}${rottenTomatoesId}`
: `${config.baseURLRottenTomatoesSerie}${rottenTomatoesId}`;
const letterboxdId = item_type === "movie" ? json.LETTERBOXD_ID : null;
const letterboxdHomepage =
item_type === "movie"
? `${config.baseURLLetterboxdFilm}${letterboxdId}`
: null;
const sensCritiqueId = parseInt(json.SENSCRITIQUE_ID);
const sensCritiqueHomepage =
item_type === "movie"
? `${config.baseURLSensCritiqueFilm}${sensCritiqueId}`
: `${config.baseURLSensCritiqueSerie}${sensCritiqueId}`;
const tmdbId = parseInt(json.THEMOVIEDB_ID);
const tmdbHomepage =
item_type === "movie"
? `${config.baseURLTMDBFilm}${tmdbId}`
: `${config.baseURLTMDBSerie}${tmdbId}`;
const traktId = json.TRAKT_ID;
const traktHomepage =
item_type === "movie"
? `${config.baseURLTraktFilm}${traktId}`
: `${config.baseURLTraktSerie}${traktId}`;
const theTvdbId = parseInt(json.THETVDB_ID);
const theTvdbHomepage =
item_type === "movie"
? `${config.baseURLTheTVDBFilm}`
: `${config.baseURLTheTVDBSerie}`;
const tvtimeId = item_type === "tvshow" ? theTvdbId : null;
const tvtimeHomepage =
item_type === "tvshow" ? `${config.baseURLTVTimeSerie}${theTvdbId}` : null;
const isActive = json.IS_ACTIVE === "TRUE";
// If the Movie Database ID is not found, log an error and exit.
if (isNaN(tmdbId)) {
throw new Error(
`Something went wrong, The Movie Database ID has not been found for ${allocineHomepage}!`,
);
}
return {
allocine: {
id: allocineId,
lastPartUrl: allocineURL,
homepage: allocineHomepage,
criticsDetails: allocineCriticsDetails,
},
imdb: { id: imdbId, homepage: imdbHomepage },
betaseries: { id: betaseriesId, homepage: betaseriesHomepage },
metacritic: { id: metacriticId, homepage: metacriticHomepage },
rotten_tomatoes: { id: rottenTomatoesId, homepage: rottenTomatoesHomepage },
letterboxd: { id: letterboxdId, homepage: letterboxdHomepage },
senscritique: { id: sensCritiqueId, homepage: sensCritiqueHomepage },
tmdb: { id: tmdbId, homepage: tmdbHomepage },
trakt: { id: traktId, homepage: traktHomepage },
tv_time: { id: tvtimeId, homepage: tvtimeHomepage },
thetvdb: { id: theTvdbId, homepage: theTvdbHomepage },
is_active: isActive,
};
};
module.exports = generateURLs;