-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdateData.js
222 lines (186 loc) · 7.04 KB
/
updateData.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
const csv = require("csvtojson");
const { config } = require("./config");
const { MongoClient, ServerApiVersion } = require("mongodb");
const uri = `mongodb+srv://${config.mongoDbCredentials}${config.mongoDbCredentialsLastPart}`;
const client = new MongoClient(uri, {
serverApi: ServerApiVersion.v1,
});
const { b64Encode } = require("./utils/b64EncodeAndDecode");
const { fetchAndCheckItemCount } = require("./getAllocineItemsNumber");
const { getMojoBoxOffice } = require("./content/getMojoBoxOffice");
const { getNodeVarsValues } = require("./utils/getNodeVarsValues");
const { jsonArrayFiltered } = require("./utils/jsonArrayFiltered");
const { updateIds } = require("./updateIds");
const checkDbIds = require("./checkDbIds");
const isThirdPartyServiceOK = require("./utils/thirdPartyStatus");
const loopItems = require("./loopItems");
async function checkCountryCode() {
try {
const { success, data } = await isThirdPartyServiceOK(config.IPinfo);
if (success) {
if (data.trim() !== "FR") {
console.log("Please disable any VPN first.");
process.exit(1);
} else {
console.log(`Country code is ${data.trim()}, continuing...`);
console.log(
"----------------------------------------------------------------------------------------------------",
);
}
} else {
console.error("Failed to fetch country code. Aborting.");
process.exit(1);
}
} catch (error) {
console.error("Error fetching country code:", error);
process.exit(1);
}
}
async function checkStatus(service) {
const { success } = await isThirdPartyServiceOK(service.url);
if (success) {
console.log(`${service.name}'s status is OK, continuing...`);
} else {
console.error(`${service.name}'s status is not OK. Aborting.`);
process.exit(1);
}
}
(async () => {
if (getNodeVarsValues.environment === "local") {
await checkCountryCode();
}
if (getNodeVarsValues.skip_services !== "skip_services") {
const ids = [1, 2, 3];
for (const id of ids) {
await fetchAndCheckItemCount(id);
}
console.log(
"----------------------------------------------------------------------------------------------------",
);
for (let service of config.services) {
await checkStatus(service);
}
console.log(
"----------------------------------------------------------------------------------------------------",
);
}
if (getNodeVarsValues.get_ids === "update_ids") updateIds();
if (getNodeVarsValues.get_db !== "update_db") process.exit(0);
const database = client.db(config.dbName);
const collectionData = database.collection(config.collectionName);
const idsFilePath =
getNodeVarsValues.item_type === "movie"
? config.filmsIdsFilePath
: config.seriesIdsFilePath;
console.log(`Ids file path to use: ${idsFilePath}`);
const jsonArrayFromCSV = await csv().fromFile(idsFilePath);
const jsonArray =
!getNodeVarsValues.is_not_active ||
getNodeVarsValues.is_not_active === "active"
? jsonArrayFiltered(jsonArrayFromCSV)
: jsonArrayFromCSV;
const jsonArraySortedHighestToLowest = jsonArray.sort(
(a, b) => b.THEMOVIEDB_ID - a.THEMOVIEDB_ID,
);
const allTheMovieDbIds = jsonArraySortedHighestToLowest.map((item) =>
parseInt(item.THEMOVIEDB_ID),
);
if (allTheMovieDbIds.length === 0) {
console.log("Not updating tvshows as the top list is not correct.");
process.exit(0);
} else if (allTheMovieDbIds.length < config.minimumActiveItems) {
console.log("Something went wrong when updating the IDs. Abording.");
process.exit(1);
}
/**
* If we are in the `update_ids` mode, we proceed to reset the `is_active` and `popularity`
* fields for all documents in the collectionData that do not match the currently active items IDs.
* Once the operation is complete, we log the number of documents that have been affected by this operation.
*
* The fields `is_active` and `popularity` for the active items are updated afterwards.
*/
if (getNodeVarsValues.get_ids === "update_ids") {
const resetIsActive = { $set: { is_active: false } };
const resetPopularity = {
$set: {
"allocine.popularity": null,
"imdb.popularity": null,
mojo: null,
},
};
const filterQueryIsActive = {
item_type: getNodeVarsValues.item_type,
id: { $nin: allTheMovieDbIds },
};
await collectionData.updateMany(filterQueryIsActive, resetIsActive);
await collectionData.updateMany(filterQueryIsActive, resetPopularity);
console.log(
`${allTheMovieDbIds.length} documents have been excluded from the is_active and popularity reset.`,
);
}
if (getNodeVarsValues.delete_ids === "delete_ids") {
let itemsToDelete = [];
itemsToDelete = itemsToDelete.map((item) => b64Encode(item));
const filterQueryDelete = {
item_type: getNodeVarsValues.item_type,
_id: { $in: itemsToDelete },
};
if (itemsToDelete.length > 0) {
const deleteResult = await collectionData.deleteMany(filterQueryDelete);
console.log(`${deleteResult.deletedCount} items were deleted.`);
} else {
console.log(
"Enter some items in the itemsToDelete array first. Abording.",
);
process.exit(1);
}
process.exit(0);
}
const index_to_start = getNodeVarsValues.index_to_start || 0;
const max_index = parseInt(getNodeVarsValues.max_index) || null;
console.time("Duration");
try {
/* Printing out the value of each variable in the getNodeVarsValues object. */
for (const [variable, value] of Object.entries(getNodeVarsValues)) {
const variableValue = value ? value : "not set";
console.log(`${variable}: ${variableValue}`);
}
if (getNodeVarsValues.check_db_ids === "check")
await checkDbIds(jsonArrayFromCSV, collectionData);
const force = getNodeVarsValues.force === "force";
const mojoBoxOfficeArray =
getNodeVarsValues.skip_mojo === "skip_mojo"
? []
: await getMojoBoxOffice(getNodeVarsValues.item_type);
const { newOrUpdatedItems } = await loopItems(
collectionData,
config,
force,
index_to_start,
getNodeVarsValues.item_type,
jsonArraySortedHighestToLowest,
mojoBoxOfficeArray,
getNodeVarsValues.check_data,
max_index,
);
console.log(`Number of new or updated items: ${newOrUpdatedItems}`);
const documents = await collectionData.estimatedDocumentCount();
console.log(`Number of documents in the collection: ${documents}`);
const movieCount = await collectionData.countDocuments({
item_type: "movie",
});
console.log(`Number of movie documents in the collection: ${movieCount}`);
const tvShowCount = await collectionData.countDocuments({
item_type: "tvshow",
});
console.log(`Number of tvshow documents in the collection: ${tvShowCount}`);
} catch (error) {
throw new Error(`Global: ${error}`);
} finally {
await client.close();
}
console.timeEnd(
"Duration",
`- ${jsonArraySortedHighestToLowest.length} elements imported.`,
);
})();