File size: 3,454 Bytes
c237e22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import axios from "axios";
import * as cheerio from "cheerio";
import { TorrentSearchResult } from "./search.js";

export enum ItorrentCategory {
  Film = 3,
  Sorozat = 4,
}

export enum ItorrentQuality {
  SD = "sd",
  HD = "hd",
  CAM = "cam",
}

export const searchItorrent = async (
  searchQuery: string,
  categories: ItorrentCategory[],
  qualities: ItorrentQuality[]
): Promise<TorrentSearchResult[]> => {
  const torrents: TorrentSearchResult[] = [];
  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: string | undefined;
              let magnet: string | undefined;

              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: string | undefined) => {
  const categories: Record<string, string> = {
    "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 as string];
};

const parseSize = (size: string) => {
  const units: Record<string, number> = {
    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]);
};