forked from Fannovel16/fancaps-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovie.js
51 lines (46 loc) · 2.08 KB
/
movie.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
const axios = require("./createAxios")()
const { JSDOM } = require("jsdom")
const { getImageId } = require("./image")
async function getMovieData(movieUrl, { skipNLastPages, numOfPromises }) {
movieUrl = new URL(movieUrl)
let i = 1
let imageUrls2d = []
while (true) {
let currImageUrls2dPromises = []
for (let j = 0; j < numOfPromises; j++) {
movieUrl.searchParams.set("page", i + j)
currImageUrls2dPromises.push(getCurrPageImageUrls(movieUrl.toString()))
}
let currImageUrls2d = (await Promise.allSettled(currImageUrls2dPromises)).map(el => el.value)
const errI = currImageUrls2d.findIndex(el => !el)
if (errI >= 0) {
imageUrls2d.push(currImageUrls2d.slice(0, errI).flat())
break
}
imageUrls2d.push(currImageUrls2d.flat())
i+= numOfPromises
}
//if (skipNLastPages) imageUrls2d = imageUrls2d.slice(0, -skipNLastPages)
movieUrl.searchParams.delete("page")
return {
movieTitle: new JSDOM((await axios(movieUrl.toString())).data)
.window.document
.querySelector(".post_title ").textContent //Cringe css selector lol
.trim()
.replace("Images from ", '')
.replace(': ', ' - ') //File system don't like colons which usually appear in movies' title
.replace('/', '-'), //F*** you Fate series
movieUrl,
imageUrls: imageUrls2d.flat()
}
}
async function getCurrPageImageUrls(movieUrl) {
const { data: pageHtml } = await axios(movieUrl)
const { document } = (new JSDOM(pageHtml)).window
const imagesContainerEl = document.querySelector(".post_title ").nextElementSibling
if (Number(document.querySelector("li.active").textContent.trim()) !== Number(new URL(movieUrl).searchParams.get("page"))) {
throw new Error("Page number invalid")
}
return [...imagesContainerEl.querySelectorAll("img.imageFade")].map(el => `https://cdni.fancaps.net/file/fancaps-movieimages/${getImageId(el.src)}.jpg`)
}
module.exports = { getMovieData, getCurrPageImageUrls }