UNPKG

zyscr

Version:
119 lines (118 loc) 3.67 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) { if (!query) { return { error: true, message: "what you want to search?", }; } try { const { data } = await Utils_1.Axios.request({ baseURL: Constant_1.nineAppsBaseUrl, url: "/search/tag-" + query + "-1/", }).catch((e) => e?.response); if (!data) { throw new Error("No data found!"); } const $ = (0, Utils_1.Cheerio)(data); const _temp = []; $('div.content > ul > li[class=""]').each((i, e) => { const title = $(e).find(".name").text().trim(); let url = $(e).find("a.app-item").attr("href"); if (!url.startsWith("http")) { url = Constant_1.nineAppsBaseUrl + url; } const thumbnail = $(e).find(".pic > img").attr("src"); const size = $(e).find("p.other > .size").text(); const version = $(e).find("p.other > .version").text(); _temp.push({ title, url, thumbnail, size, version, }); }); if (!_temp.length) { throw new Error(`Empty results for ${query}`); } return _temp; } catch (e) { return { error: true, message: String(e), }; } } exports.search = search; async function findDownloaUrl(opts) { const { data } = await Utils_1.Axios.request({ ...opts, }).catch((e) => e?.response); const $ = (0, Utils_1.Cheerio)(data); const downloadUrl = $(".downloading-info").find("a.js_test").attr("href") || false; return downloadUrl; } async function detail(url) { if (!url.startsWith("http")) { return { error: true, message: "No url provided", }; } try { const { data, headers } = await Utils_1.Axios.request({ url, }).catch((e) => e?.response); if (!data) { throw new Error("No data found!"); } const $ = (0, Utils_1.Cheerio)(data); const title = $("h1.name").text().trim(); if (!title) { throw new Error(`Cannot find title sources!`); } const version = $(".details-sdk > span[itemprop='version']") .text() .trim(); const rating = $(".rating-info > .rating").text().trim(); const size = $("a.download-app") .text() .trim() .match(/(\d+(\.\d+)?([KMGTPE]B))/i)[0] || ""; const publisher = $(".details-author > p[itemprop='publisher']").text(); const _downloadUrl = $("a.download-app").attr("href"); if (!_downloadUrl) { throw new Error("Cannot find base download url sources!"); } const downloadUrl = await findDownloaUrl({ url: Constant_1.nineAppsBaseUrl + _downloadUrl, headers: { cookie: headers["set-cookie"], }, }); if (!downloadUrl) { throw new Error("Cannot find direct download url sources!"); } return { title, version, rating, size, publisher, downloadUrl, }; } catch (e) { return { error: true, message: String(e), }; } } exports.detail = detail;