File size: 3,875 Bytes
762fa11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import axios from "axios";
import * as cheerio from "cheerio";
export var ItorrentCategory;
(function (ItorrentCategory) {
    ItorrentCategory[ItorrentCategory["Film"] = 3] = "Film";
    ItorrentCategory[ItorrentCategory["Sorozat"] = 4] = "Sorozat";
})(ItorrentCategory || (ItorrentCategory = {}));
export var ItorrentQuality;
(function (ItorrentQuality) {
    ItorrentQuality["SD"] = "sd";
    ItorrentQuality["HD"] = "hd";
    ItorrentQuality["CAM"] = "cam";
})(ItorrentQuality || (ItorrentQuality = {}));
export const searchItorrent = async (searchQuery, categories, qualities) => {
    const torrents = [];
    const quality = qualities.join(",");
    await Promise.all(categories.map(async (category) => {
        let page = 0;
        while (page <= 5) {
            try {
                page++;
                let torrentsOnPage = 0;
                const link = `https://itorrent.ws/torrentek/category/${category}/title/${searchQuery}/qualities[]/${quality}/page/${page}/`;
                const torrentsPage = await axios.get(link);
                const $ = cheerio.load(torrentsPage.data);
                await Promise.all([...$("tr.gradeX")].map(async (el) => {
                    torrentsOnPage++;
                    const tracker = "iTorrent";
                    const torrentAnchor = $(el).find("td.ellipse > a");
                    const torrentHref = torrentAnchor.attr("href");
                    const name = torrentAnchor.text().trim();
                    if (!torrentHref || !name)
                        return;
                    const category = parseCategory($(el).find("i.zqf").attr("title"));
                    const size = parseSize($(el).find("td:nth-child(5)").text().trim());
                    const seeds = Number($(el).find("td:nth-child(7)").text());
                    const peers = Number($(el).find("td:nth-child(8)").text());
                    const torrentPageLink = `https://itorrent.ws${torrentHref}`;
                    let torrent;
                    let magnet;
                    try {
                        const torrentPage = await axios.get(torrentPageLink);
                        const $ = cheerio.load(torrentPage.data);
                        const torrentFileHref = $("a.btn-primary.seed-warning").attr("href");
                        if (torrentFileHref)
                            torrent = `https://itorrent.ws${torrentFileHref}`;
                        magnet = $("a.btn-success.seed-warning").attr("href");
                        if (!torrent && !magnet)
                            return;
                    }
                    catch {
                        return;
                    }
                    torrents.push({
                        name,
                        tracker,
                        category,
                        size,
                        seeds,
                        peers,
                        torrent,
                        magnet,
                    });
                }));
                if (torrentsOnPage < 48)
                    break;
            }
            catch {
                continue;
            }
        }
    }));
    return torrents;
};
const parseCategory = (category) => {
    const categories = {
        "Film/HU/CAM": "Movies/CAM/HU",
        "Film/HU/SD": "Movies/SD/HU",
        "Film/HU/HD": "Movies/HD/HU",
        "Sorozat/HU/SD": "TV/SD/HU",
        "Sorozat/HU/HD": "TV/HD/HU",
    };
    return categories[category];
};
const parseSize = (size) => {
    const units = {
        TB: 1024 ** 4,
        GB: 1024 ** 3,
        MB: 1024 ** 2,
        KB: 1024,
        B: 1,
    };
    const [sizeStr, unit] = size.split(" ");
    const sizeNum = Number(sizeStr);
    if (!sizeNum || !units[unit])
        return 0;
    return Math.ceil(sizeNum * units[unit]);
};
//# sourceMappingURL=itorrent.js.map