-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
212 lines (198 loc) · 5.93 KB
/
main.ts
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
import {
assertEquals,
assertExists,
import {
DOMParser,
HTMLDocument,
} from "https://deno.land/x/deno_dom/deno-dom-wasm.ts";
const TRACKERS: string[] = ["ThePirateBay", "YggTorrent", "EZTV"];
enum URL {
thepiratebay = "https://thepiratebay.org",
yggtorrent = "https://www4.yggtorrent.li",
eztv = "https://eztv.re",
}
interface Torrent {
category: string | null;
name: string | null;
date: string | null;
magnet: string | null;
torrent: string | null;
size: string | null;
seeders: number | null;
leechers: number | null;
user: string | null;
downloads: number | null;
comments: number | null;
}
async function fetchThePirateBay(search: string): Promise<string> {
const browser = await puppeteer.launch({
executablePath: "/usr/bin/chromium-browser",
headless: false,
});
const page = await browser.newPage();
await page.setViewport({ width: 1920, height: 1080 });
const response = await page.goto(
`${URL.thepiratebay}/search.php?q=${search}`,
);
assertEquals(response.status(), 200);
await page.click('label[title="Order by Seeders"]');
const HTML: string = await page.content();
browser.close();
return HTML;
}
async function fetchYggTorrent(search: string): Promise<string> {
const response = await fetch(
`${URL.yggtorrent}/engine/search?name=${search}&do=search&sort=seeders`,
);
assertEquals(response.status, 200);
return await response.text();
}
async function fetchEZTV(search: string): Promise<string> {
const response = await fetch(`${URL.eztv}/search/${search}`);
assertEquals(response.status, 200);
return await response.text();
}
async function fetchTracker(
tracker: string,
search: string,
): Promise<HTMLDocument> {
let HTML: string | null = null;
switch (tracker) {
case TRACKERS[0]: {
HTML = await fetchThePirateBay(search);
break;
}
case TRACKERS[1]: {
HTML = await fetchYggTorrent(search);
break;
}
case TRACKERS[2]: {
HTML = await fetchEZTV(search);
break;
}
default: {
console.error("ERROR");
Deno.exit(1);
}
}
assertExists(HTML);
const result: HTMLDocument | null = new DOMParser().parseFromString(
HTML,
"text/html",
);
assertExists(result);
return result;
}
function scrapThePirateBay(HTML: HTMLDocument): Torrent[] {
const row = HTML.querySelectorAll("li.list-entry");
const results: Torrent[] = [];
for (const element of row) {
const torrent: Torrent = {
category: element.children[0]?.textContent,
name: element.children[1]?.textContent,
date: element.children[2]?.textContent,
magnet: element.children[3]?.children[0]?.getAttribute("href"),
torrent: null,
size: element.children[4]?.textContent,
seeders: parseInt(element.children[5]?.textContent),
leechers: parseInt(element.children[6]?.textContent),
user: element.children[7]?.textContent,
downloads: null,
comments: null,
};
results.push(torrent);
}
return results;
}
function scrapYggTorrent(HTML: HTMLDocument): Torrent[] {
const row = HTML.querySelectorAll("table.table tr");
const torrentLink = "https://www3.yggtorrent.nz/engine/download_torrent?id=";
const results: Torrent[] = [];
for (const element of row) {
const torrent: Torrent = {
category: element.children[0]?.textContent,
name: element.children[1]?.textContent.replace(/[\n\r]/g, ""), // remove \n
comments: parseInt(element.children[3].textContent),
date: element.children[4]?.textContent.split(" ")[0],
size: element.children[5]?.textContent,
downloads: parseInt(element.children[6]?.textContent),
seeders: parseInt(element.children[7]?.textContent),
leechers: parseInt(element.children[8]?.textContent),
magnet: null,
torrent: torrentLink +
element.children[2]?.children[0]?.getAttribute("target"),
user: null,
};
results.push(torrent);
}
results.splice(0, 1);
return results;
}
function scrapEZTV(HTML: HTMLDocument): Torrent[] {
const row = HTML.querySelectorAll("table.forum_header_border tr");
const results: Torrent[] = [];
for (let i = 0; i < row.length; i++) {
if (i > 8) {
const torrent: Torrent = {
category: row[i].children[0]?.children[0]?.getAttribute("href"),
name: row[i].children[1]?.textContent.replace(/[\n\r]/g, ""),
comments: null,
date: row[i].children[4]?.textContent,
size: row[i].children[3]?.textContent,
downloads: null,
seeders: parseInt(row[i].children[5]?.textContent),
leechers: null,
magnet: row[i].children[2]?.children[0]?.getAttribute("href"),
torrent: row[i].children[2]?.children[1]?.getAttribute("href"),
user: null,
};
results.push(torrent);
}
}
return results;
}
function scrapTorrent(tracker: string, HTML: HTMLDocument): Torrent[] {
let results: Torrent[] = [];
switch (tracker) {
case TRACKERS[0]: {
results = scrapThePirateBay(HTML);
break;
}
case TRACKERS[1]: {
results = scrapYggTorrent(HTML);
break;
}
case TRACKERS[2]: {
results = scrapEZTV(HTML);
break;
}
default: {
console.error("ERROR");
Deno.exit(1);
}
}
assertExists(results);
return results;
}
const argv = yargs(Deno.args)
.option("search", {
alias: "s",
demandOption: true,
describe: "Input your search string",
type: "string",
})
.option("tracker", {
alias: "t",
demandOption: true,
describe: "Choose a tracker to fetch",
choices: TRACKERS,
type: "string",
})
.help()
.argv;
const HTML: HTMLDocument = await fetchTracker(argv.tracker, argv.search);
const results: Torrent[] = await scrapTorrent(argv.tracker, HTML);
console.log(JSON.stringify(results, null, 2));