File size: 4,121 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import axios from "axios";
import { wrapper } from "axios-cookiejar-support";
import * as cheerio from "cheerio";
import { CookieJar } from "tough-cookie";
import { TorrentSearchResult } from "./search.js";
import { isImdbId } from "../utils/imdb.js";

const NCORE_USER = process.env.NCORE_USER;
const NCORE_PASSWORD = process.env.NCORE_PASSWORD;

export enum NcoreCategory {
  Film_SD_HU = "xvid_hun",
  Film_SD_EN = "xvid",
  Film_HD_HU = "hd_hun",
  Film_HD_EN = "hd",
  Sorozat_SD_HU = "xvidser_hun",
  Sorozat_SD_EN = "xvidser",
  Sorozat_HD_HU = "hdser_hun",
  Sorozat_HD_EN = "hdser",
}

export const searchNcore = async (
  searchQuery: string,
  categories: NcoreCategory[],
  ncoreUser?: string,
  ncorePassword?: string
): Promise<TorrentSearchResult[]> => {
  try {
    const user = ncoreUser || NCORE_USER;
    const password = ncorePassword || NCORE_PASSWORD;

    if (!user || !password) return [];

    const jar = new CookieJar();
    // @ts-ignore
    const client = wrapper(axios.create({ jar, baseURL: "https://ncore.pro" }));

    const formData = new FormData();
    formData.append("nev", user);
    formData.append("pass", password);
    formData.append("set_lang", "hu");
    formData.append("submitted", "1");
    await client.post("/login.php", formData);

    const torrents: TorrentSearchResult[] = [];

    let page = 0;

    while (page <= 5) {
      try {
        page++;

        let torrentsOnPage = 0;

        let params = new URLSearchParams({
          oldal: page.toString(),
          tipus: "kivalasztottak_kozott",
          kivalasztott_tipus: categories.join(","),
          mire: searchQuery,
          miben: isImdbId(searchQuery) ? "imdb" : "name",
          miszerint: "ctime",
          hogyan: "DESC",
        });

        const link = `/torrents.php?${params.toString()}}`;
        const torrentsPage = await client.get(link);
        const $ = cheerio.load(torrentsPage.data);

        const rssUrl = $("link[rel=alternate]").attr("href");
        const downloadKey = rssUrl?.split("=")[1];
        if (!downloadKey) return torrents;

        for (const el of $("div.box_torrent")) {
          torrentsOnPage++;

          const name = $(el).find("div.torrent_txt > a").attr("title");

          const categoryHref = $(el)
            .find("a > img.categ_link")
            .parent()
            .attr("href");

          const tracker = "nCore";
          const category = parseCategory(categoryHref?.split("=")[1]);
          const size = parseSize($(el).find("div.box_meret2").text());
          const seeds = Number($(el).find("div.box_s2").text());
          const peers = Number($(el).find("div.box_l2").text());
          const torrentId = $(el).next().next().attr("id");
          const torrent = `https://ncore.pro/torrents.php?action=download&id=${torrentId}&key=${downloadKey}`;

          if (!name || !torrentId) continue;

          torrents.push({
            name,
            tracker,
            category,
            size,
            seeds,
            peers,
            torrent,
          });
        }

        if (torrentsOnPage < 50) break;
      } catch {
        continue;
      }
    }

    return torrents;
  } catch (error) {
    return [];
  }
};

const parseCategory = (category: string | undefined) => {
  const categories: Record<NcoreCategory, string> = {
    [NcoreCategory.Film_SD_HU]: "Movies/SD/HU",
    [NcoreCategory.Film_SD_EN]: "Movies/SD/EN",
    [NcoreCategory.Film_HD_HU]: "Movies/HD/HU",
    [NcoreCategory.Film_HD_EN]: "Movies/HD/EN",
    [NcoreCategory.Sorozat_SD_HU]: "TV/SD/HU",
    [NcoreCategory.Sorozat_SD_EN]: "TV/SD/EN",
    [NcoreCategory.Sorozat_HD_HU]: "TV/HD/HU",
    [NcoreCategory.Sorozat_HD_EN]: "TV/HD/EN",
  };

  return categories[category as NcoreCategory];
};

const parseSize = (size: string) => {
  const units: Record<string, number> = {
    TiB: 1024 ** 4,
    GiB: 1024 ** 3,
    MiB: 1024 ** 2,
    KiB: 1024,
    B: 1,
  };

  const [sizeStr, unit] = size.split(" ");
  const sizeNum = Number(sizeStr);

  if (!sizeNum || !units[unit]) return 0;

  return Math.ceil(sizeNum * units[unit]);
};