-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetAllocineItemsNumber.js
63 lines (53 loc) · 1.6 KB
/
getAllocineItemsNumber.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
const { config } = require("./config");
const { generateUserAgent } = require("./utils/generateUserAgent");
const { getCheerioContent } = require("./utils/getCheerioContent");
const { getNodeVarsValues } = require("./utils/getNodeVarsValues");
const { logErrors } = require("./utils/logErrors");
const fetchAndCheckItemCount = async (index) => {
const baseURLAllocineType =
getNodeVarsValues.item_type === "movie"
? config.baseURLAllocineFilms
: config.baseURLAllocineSeries;
const baseURLType =
getNodeVarsValues.item_type === "movie"
? config.baseURLTypeFilms
: config.baseURLTypeSeries;
const countItems = await getAllocineItemsNumber(
baseURLAllocineType,
baseURLType,
index,
);
if (countItems < 15) {
console.error(
`Found ${countItems} items. Something is wrong on AlloCiné, aborting.`,
);
process.exit(1);
}
};
const getAllocineItemsNumber = async (
baseURLAllocineType,
baseURLType,
index,
) => {
let countItems = 0;
try {
const options = {
headers: {
"User-Agent": generateUserAgent(),
},
};
const url = `${baseURLAllocineType}${index > 1 ? "?page=" + index : ""}`;
$ = await getCheerioContent(`${url}`, options, "getAllocineItemsNumber");
const links = $("a.meta-title-link");
links.each((_i, link) => {
const href = $(link).attr("href");
if (href.includes(baseURLType)) {
countItems++;
}
});
} catch (error) {
logErrors(error, baseURLAllocineType, "getAllocineItemsNumber");
}
return countItems;
};
module.exports = { fetchAndCheckItemCount };