-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-episodes.js
114 lines (98 loc) · 2.73 KB
/
get-episodes.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
const https = require("https");
const fs = require("fs");
const path = require("path");
// const M = process.argv[2];
const ANIME_ID = process.argv[2];
const NAME = process.argv[3];
const FROM_EPISODE = +process.argv[4] || 1;
const TO_EPISODE = +process.argv[5] || Number.MAX_SAFE_INTEGER;
(() => {
error = false;
if (FROM_EPISODE < 0) {
console.log("FROM_EPSIODE has to be greater than 0");
error = true;
} else if (TO_EPISODE && TO_EPISODE < FROM_EPISODE) {
console.log("TO_EPISODE can't be greater than FROM_EPISODE");
error = true;
}
if (error) {
console.log("Will now exit!!!");
process.exit(0);
}
})();
// The promise which make the api request to the server
// No Library used here :)
const getUrls = (url) => {
return new Promise((resolve, reject) => {
https
.request(url, (res) => {
let responseData = "";
res.on("data", (d) => (responseData += d));
res.on("end", () => {
const { next_page_url, data } = JSON.parse(responseData);
resolve({
next_page_url,
episodes: data,
});
});
})
.on("error", (e) => reject(e))
.end();
});
};
// keep creating the next page of urls
const apiURL = (page) =>
`https://animepahe.com/api?m=release&sort=episode_desc&id=${ANIME_ID}&page=${page}`;
// The actual function
// Made IIFE so that can be run as async
(async () => {
const allEpisodes = [];
let currentPage = 1;
while (true) {
const { episodes, next_page_url } = await getUrls(apiURL(currentPage));
console.log(
"Page",
currentPage,
"Fetched",
episodes.length,
"episodes from",
episodes[0].episode,
"to",
episodes[29].episode
);
for (val of episodes) {
const { episode, session } = val;
if (episode >= FROM_EPISODE && episode <= TO_EPISODE) {
val.url = `https://animepahe.com/play/${ANIME_ID}/${session}`;
allEpisodes.push(val);
}
}
if (next_page_url == null) {
console.log("Reached the end of page.");
break;
}
if (episodes[29].episode < FROM_EPISODE) {
console.log("Reached the starting episode");
break;
}
currentPage++;
}
console.log(
"Total episodes fetched",
allEpisodes.length,
"from",
allEpisodes[0].episode,
"to",
allEpisodes[allEpisodes.length - 1].episode
);
const dirPath = path.join(path.resolve("reward", NAME));
fs.mkdirSync(dirPath, { recursive: true });
const stream = fs.createWriteStream(path.join(dirPath, "episode-links.txt"), {
flags: "wx",
});
for (val of allEpisodes) {
stream.write(val.url + "\n");
}
stream.end();
console.log(`Total ${allEpisodes.length} fetched and written to file`);
})();