Spaces:
Paused
Paused
import axios from "axios"; | |
import { wrapper } from "axios-cookiejar-support"; | |
import * as cheerio from "cheerio"; | |
import { CookieJar } from "tough-cookie"; | |
import { isImdbId } from "../utils/imdb.js"; | |
const NCORE_USER = process.env.NCORE_USER; | |
const NCORE_PASSWORD = process.env.NCORE_PASSWORD; | |
export var NcoreCategory; | |
(function (NcoreCategory) { | |
NcoreCategory["Film_SD_HU"] = "xvid_hun"; | |
NcoreCategory["Film_SD_EN"] = "xvid"; | |
NcoreCategory["Film_HD_HU"] = "hd_hun"; | |
NcoreCategory["Film_HD_EN"] = "hd"; | |
NcoreCategory["Sorozat_SD_HU"] = "xvidser_hun"; | |
NcoreCategory["Sorozat_SD_EN"] = "xvidser"; | |
NcoreCategory["Sorozat_HD_HU"] = "hdser_hun"; | |
NcoreCategory["Sorozat_HD_EN"] = "hdser"; | |
})(NcoreCategory || (NcoreCategory = {})); | |
export const searchNcore = async (searchQuery, categories, ncoreUser, ncorePassword) => { | |
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 = []; | |
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) => { | |
const categories = { | |
[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]; | |
}; | |
const parseSize = (size) => { | |
const units = { | |
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]); | |
}; | |
//# sourceMappingURL=ncore.js.map |