-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloopItems.js
263 lines (228 loc) · 8.66 KB
/
loopItems.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
const axios = require("axios");
// const { controlData } = require("./controlData");
const { getAllocineInfo } = require("./content/getAllocineInfo");
const { getMetacriticRating } = require("./content/getMetacriticRating");
const { getNodeVarsValues } = require("./utils/getNodeVarsValues");
const { upsertToDatabase } = require("./upsertToDatabase");
const compareUsersRating = require("./compareUsersRating");
const createJSON = require("./createJSON");
const generateURLs = require("./generateURLs");
/**
* Loop through items in a collection, perform various operations on each item, and return an object containing the number of new or updated items.
*
* @param {Object} collectionData - The collection data object.
* @param {Object} config - The configuration object.
* @param {boolean} force - Flag indicating whether to force the operations.
* @param {number} index_to_start - The index to start looping from.
* @param {string} item_type - The type of item being looped.
* @param {Array} jsonArray - The array of JSON objects to loop through.
* @param {Array} mojoBoxOfficeArray - The array of Mojo Box Office data to be included in the operations.
* @param {string} check_data - Flag indicating whether we check the data or not.
* @returns {Object} Returns an object containing the number of new or updated items.
*/
const loopItems = async (
collectionData,
config,
force,
index_to_start,
item_type,
jsonArray,
mojoBoxOfficeArray,
check_data,
max_index,
) => {
let createJsonCounter = (itemCounter = 0);
// Loop through jsonArray with the given start index
for (let index = index_to_start; index < jsonArray.length; index++) {
try {
if (max_index && index === max_index) {
console.log(`Maximum index ${max_index} processed, aborting.`);
process.exit(0);
}
const json = jsonArray[index];
// Log the progress in terms of percentage
console.timeLog(
"Duration",
`- ${parseInt(index) + 1} / ${jsonArray.length} (${(((parseInt(index) + 1) * 100) / jsonArray.length).toFixed(1)}%)`,
);
// Generate URLs based on the current JSON item
const urls = generateURLs(item_type, config, json);
/* Handle AlloCiné related data */
const allocineId = urls.allocine.id;
const allocineURL = urls.allocine.lastPartUrl;
const allocineHomepage = urls.allocine.homepage;
const allocineCriticsDetails = urls.allocine.criticsDetails;
if (check_data === "check_data") {
// await controlData(allocineHomepage, config.keysToCheck, isDocumentHasInfo, isDocumentExisting[0], item_type);
}
/* Handle IMDb related data */
const imdbId = urls.imdb.id;
const imdbHomepage = urls.imdb.homepage;
/* Handle BetaSeries related data */
const betaseriesId = urls.betaseries.id;
const betaseriesHomepage = urls.betaseries.homepage;
/* Handle Metacritic related data */
const metacriticId = urls.metacritic.id;
const metacriticHomepage = urls.metacritic.homepage;
/* Handle Rotten Tomatoes related data */
const rottenTomatoesId = urls.rotten_tomatoes.id;
const rottenTomatoesHomepage = urls.rotten_tomatoes.homepage;
/* Handle Letterboxd related data */
const letterboxdId = urls.letterboxd.id;
const letterboxdHomepage = urls.letterboxd.homepage;
/* Handle SensCritique related data */
const sensCritiqueId = urls.senscritique.id;
const sensCritiqueHomepage = urls.senscritique.homepage;
/* Handle TMDB related data */
const tmdbId = urls.tmdb.id;
const tmdbHomepage = urls.tmdb.homepage;
/* Handle Trakt related data */
const traktId = urls.trakt.id;
const traktHomepage = urls.trakt.homepage;
/* Handle TV Time related data */
const tvtimeId = urls.tv_time.id;
const tvtimeHomepage = urls.tv_time.homepage;
/* Handle TheTVDB related data */
const theTvdbId = urls.thetvdb.id;
const theTvdbHomepage = urls.thetvdb.homepage;
// Determine if the URL is active
const isActive = urls.is_active;
const checkDate = getNodeVarsValues.check_date;
if (parseInt(checkDate) >= 0) {
const item_type_api = item_type === "movie" ? "movie" : "tvshow";
const apiCall = `${config.baseURLRemote}/${item_type_api}/${tmdbId}?api_key=${config.internalApiKey}`;
try {
const response = await axios.get(apiCall);
if (response && response.data && response.data.updated_at) {
const { updated_at } = response.data;
const updatedAtDate = new Date(updated_at);
const dateValue = new Date();
dateValue.setDate(dateValue.getDate() - parseInt(checkDate));
if (updatedAtDate >= dateValue) {
console.log(
`Skipping because updated less than ${parseInt(checkDate)} days ago.`,
);
continue;
}
}
} catch (error) {
const status = error.response.status;
if (status === 404) {
console.log(
`Item called on ${apiCall} not found in database, continuing...`,
);
} else if (status === 503) {
throw new Error(
"Render service has been suspended. Please re-enable it.",
);
} else {
throw new Error(`Error fetching data: ${error}`);
}
}
}
if (getNodeVarsValues.check_id) {
if (imdbId !== getNodeVarsValues.check_id) continue;
}
// Check if page is existing before upsert to DB
const { error } = await getAllocineInfo(
allocineHomepage,
betaseriesHomepage,
tmdbId,
true,
);
let errorMetacritic = false;
try {
const { error: errorMetacritic } = await getMetacriticRating(
metacriticHomepage,
metacriticId,
);
if (errorMetacritic.includes("403")) {
console.log(errorMetacritic);
errorMetacritic = true;
}
} catch (error) {}
// Determine if user ratings are equal and fetch the data
if (!error) {
const getIsEqualValue = !force
? await compareUsersRating(
allocineHomepage,
allocineURL,
betaseriesHomepage,
imdbHomepage,
imdbId,
isActive,
item_type,
mojoBoxOfficeArray,
tmdbId,
true,
)
: false;
if (!getIsEqualValue) getIsEqualValue.isEqual = false;
const data =
(!force && getIsEqualValue.isEqual) || errorMetacritic
? getIsEqualValue.data
: (createJsonCounter++,
await createJSON(
allocineCriticsDetails,
allocineURL,
allocineHomepage,
allocineId,
betaseriesHomepage,
betaseriesId,
imdbHomepage,
imdbId,
isActive,
item_type,
metacriticHomepage,
metacriticId,
rottenTomatoesHomepage,
rottenTomatoesId,
letterboxdHomepage,
letterboxdId,
sensCritiqueHomepage,
sensCritiqueId,
traktHomepage,
traktId,
mojoBoxOfficeArray,
tmdbId,
tmdbHomepage,
tvtimeHomepage,
tvtimeId,
theTvdbHomepage,
theTvdbId,
));
if (!errorMetacritic && !getIsEqualValue.isEqual) {
/*
* Update `updated_at` date only when
* - There is no Metacritic error
* - And data is not fetched from the database
*/
data.updated_at = new Date().toISOString();
} else {
console.log(
`The 'updated_at' date was not modified because 'errorMetacritic' is: ${errorMetacritic} and 'isEqual' is: ${getIsEqualValue.isEqual}`,
);
}
// Perform upsert operation on the database with the fetched data
await upsertToDatabase(data, collectionData, getIsEqualValue.isEqual);
itemCounter++;
if (
itemCounter === config.circleLimitPerInstance &&
getNodeVarsValues.environment === "circleci"
) {
process.exit(0);
}
} else {
console.error(error);
}
} catch (error) {
throw new Error(
`Error processing item at index ${index}: ${error.message}`,
);
}
}
return {
newOrUpdatedItems: createJsonCounter,
};
};
module.exports = loopItems;