forked from dev-palwar/Anime-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
183 lines (156 loc) · 6.71 KB
/
index.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
const puppeteer = require("puppeteer");
const axios = require("axios");
const fs = require("fs");
const path = require("path");
const readline = require("readline");
const { SingleBar, Presets } = require("cli-progress");
const downloadFolder = path.join(require("os").homedir(), "Desktop");
// Check if the download folder exists; if not, create it
if (!fs.existsSync(downloadFolder)) {
fs.mkdirSync(downloadFolder);
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let episodesArr = [];
console.log("\x1b[33mPress ctrl+c to exit\x1b[0m");
console.log("\x1b[33mAnd please use a clear name\x1b[0m");
rl.question("\x1b[36mName your anime: \x1b[0m", (anime) => {
searchFor(anime);
});
async function searchFor(searchQuery) {
try {
const browser = await puppeteer.launch({ headless: "new" });
const page = await browser.newPage();
page.setViewport({
width: 1280,
height: 600,
isMobile: false,
});
console.log(`\x1b[36m😈 Alright!\x1b[0m`);
await page.goto("https://animepahe.ru/", { waitUntil: "networkidle2" });
console.log(`\x1b[35m🔍 Searching...\x1b[0m`);
await page.waitForSelector('input[name="q"]');
await page.type('input[name="q"]', searchQuery, { delay: 100 });
await page.waitForTimeout(1000);
await page.waitForSelector('li[data-index="0"] a');
const totalTitles = await page.evaluate(() => {
const anchors = Array.from(document.querySelectorAll("li[data-index] a"));
return anchors.map((anchor) => anchor.textContent);
});
console.log("\x1b[32m📂 Files found\x1b[0m");
for (let i = 0; i < totalTitles.length; i++) {
console.log(`\x1b[32mPress ${i + 1} for ${totalTitles[i]}\x1b[0m`);
}
rl.question(
`${"\x1b[36m🤔 "}So which one you lookin for?\n\x1b[0m`,
async (number) => {
console.log("\x1b[36m👍 Got it\x1b[0m");
for (let index = 0; index < number; index++) {
await page.keyboard.press("ArrowDown");
}
await page.keyboard.press("Enter");
await page.waitForSelector('a[class="play"]');
episodesArr = await page.evaluate(() => {
const anchors = Array.from(
document.querySelectorAll('a[class="play"]')
);
return anchors.map((anchor) => anchor.href);
});
console.log(
`\x1b[34m🔗 There ${episodesArr.length > 1 ? "are" : "is"} ${
episodesArr.length
} episode${episodesArr.length > 1 ? "s" : ""}\x1b[0m`
);
rl.question(
"\x1b[36mWhich one do you want to download? \x1b[0m",
async (episodeNumber) => {
console.log("\x1b[36m👍 Got it\x1b[0m");
console.log("\x1b[36m⏳ Hold on...\x1b[0m");
const page2 = await browser.newPage();
await page2.goto(episodesArr[episodeNumber - 1]);
// Clicks on a download button and returns download links for all the sizes available (Pahe Shortner)
await page2.waitForSelector('a[id="downloadMenu"]');
const downloadLinks = await page2.evaluate(() => {
document.querySelector('a[id="downloadMenu"]').click();
const anchors = Array.from(
document.querySelectorAll("#pickDownload a")
);
return anchors.map((anchor) => anchor.textContent);
});
console.log("\x1b[32m💾 Available sizes\x1b[0m");
for (let i = 0; i < downloadLinks.length; i++) {
console.log(
`\x1b[32mPress ${i + 1} for ${downloadLinks[i]}\x1b[0m`
);
}
rl.question("\x1b[36mWhich size? \x1b[0m", async (size) => {
console.log(`\x1b[35m🧲 Fetching...\x1b[0m`);
const downloadLink = await page2.evaluate((size) => {
document.querySelector('a[id="downloadMenu"]').click();
return document.querySelectorAll("#pickDownload a")[size].href;
}, size);
const page3 = await browser.newPage();
await page3.goto(downloadLink);
await page3.waitForSelector('a[rel="nofollow"]');
await page3.waitForTimeout(6000);
const kwiklink = await page3.evaluate(() => {
return document.querySelector('a[rel="nofollow"]').href;
});
console.log(`\x1b[36m🫡 Preparing for download...\x1b[0m`);
const page4 = await browser.newPage();
await page4.goto(kwiklink, {
waitUntil: "networkidle2",
});
await page4.waitForSelector('button[type="submit"]');
const fileName = await page4.evaluate(() => {
document.querySelector('button[type="submit"]').click();
// Using trim() to remove whitespaces
return document.querySelector(".title").textContent.trim();
});
// Displaying downloading progress
const progressBar = new SingleBar({}, Presets.shades_grey);
let totalLength = 0;
let currentLength = 0;
page4.on("request", async (interceptedRequest) => {
const url = interceptedRequest.url();
if (url.includes("files.nextcdn.org/get")) {
console.log(`\x1b[35m✌️ ${" "} Downloading...\x1b[0m`);
const response = await axios({
url,
method: "GET",
responseType: "stream",
});
// Constructing the file path with the updated filename
const filePath = path.join(downloadFolder, fileName);
const writer = fs.createWriteStream(filePath);
response.data.on("data", (chunk) => {
currentLength += chunk.length;
if (!totalLength) {
totalLength = +response.headers["content-length"];
progressBar.start(totalLength, currentLength);
}
progressBar.update(currentLength);
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", () => {
console.log("\x1b[36m🎉 Downloading finished\x1b[0m");
browser.close();
progressBar.stop();
resolve();
});
writer.on("error", reject);
});
}
});
});
}
);
}
);
} catch (error) {
console.error("\x1b[31m❌ An error occurred:\x1b[0m", error);
}
}