UNPKG

zyscr

Version:
119 lines (118 loc) 3.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.detail = exports.search = void 0; const Utils_1 = require("../Utils"); const Constant_1 = require("../Constant"); async function search(query) { try { const { data } = await Utils_1.Axios.request({ baseURL: Constant_1.XvideosBaseUrl, url: "/", method: "GET", params: { k: query, }, }).catch((e) => e?.response); if (!data) { throw new Error(`No data response from ${Constant_1.XvideosBaseUrl}`); } const $ = (0, Utils_1.Cheerio)(data); const _temp = []; $("#content > div > div").each((i, e) => { const title = $(e).find("p.title > a").attr("title"); let url = $(e).find("p.title > a").attr("href"); if (!url.startsWith("http")) { url = Constant_1.XvideosBaseUrl + url; } const quality = $(e).find(".video-hd-mark").text() || "low"; const views = $(e) .find(".bg > span > span") .text() .replace(/[^0-9\.K|M]/gi, ""); const duration = $(e).find("a > .duration").text(); const thumbnail = $(e).find(".thumb > a > img").attr("data-src"); _temp.push({ title, quality, views, duration, url, thumbnail, }); }); if (!(Array.isArray(_temp) && _temp.length)) { throw new Error(`Empty results for '${query}'`); } return _temp; } catch (e) { return { error: true, message: String(e), }; } } exports.search = search; async function detail(url) { try { const { data } = await Utils_1.Axios.request({ url, method: "GET", }).catch((e) => e?.response); if (!data) { throw new Error("No data found for the provided url."); } const $ = (0, Utils_1.Cheerio)(data); const duration = $("h2.page-title > .duration").text(); const quality = $("h2.page-title > .video-hd-mark").text() || "low"; if (!duration) { throw new Error("is that correct xvideos url?"); } let scripts = ""; $("script").each((i, e) => { scripts += $(e).text(); }); const titleRegExp = /setVideoTitle\('(.+?)'\)/; const lowRegExp = /setVideoUrlLow\('(.+?)'\)/; const highRegExp = /setVideoUrlHigh\('(.+?)'\)/; const hlsRegExp = /setVideoHLS\('(.+?)'\)/; let results = { title: "", quality: "", duration: "", urls: {}, }; const title = scripts.match(titleRegExp); const low = scripts.match(lowRegExp); const high = scripts.match(highRegExp); const hls = scripts.match(hlsRegExp); if (title && title[1]) { results["title"] = title[1]; } else { throw new Error("cannot find title sources!"); } results["quality"] = quality; results["duration"] = duration; if (low && low[1]) { results["urls"]["low"] = low[1]; } if (high && high[1]) { results["urls"]["high"] = high[1]; } if (hls && hls[1]) { results["urls"]["hls"] = hls[1]; } if (!(results["urls"]["low"] || results["urls"]["high"])) { throw new Error("cannot find video sources!"); } return results; } catch (e) { return { error: true, message: String(e), }; } } exports.detail = detail;