UNPKG

tiktermux

Version:

Scraper for downloading media in the form of videos, images and audio from Tiktok. Also for stalking Tiktok Users

146 lines (145 loc) 5.74 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MusicalDown = void 0; const axios_1 = __importDefault(require("axios")); const cheerio_1 = require("cheerio"); const api_1 = require("../../constants/api"); const https_proxy_agent_1 = require("https-proxy-agent"); const socks_proxy_agent_1 = require("socks-proxy-agent"); const TiktokURLregex = /https:\/\/(?:m|www|vm|vt|lite)?\.?tiktok\.com\/((?:.*\b(?:(?:usr|v|embed|user|video|photo)\/|\?shareId=|\&item_id=)(\d+))|\w+)/; const getRequest = (url, proxy) => new Promise((resolve) => { if (!TiktokURLregex.test(url)) { return resolve({ status: "error", message: "Invalid Tiktok URL. Make sure your url is correct!" }); } (0, axios_1.default)(api_1._musicaldownurl, { method: "GET", headers: { Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8", "Update-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0" }, httpsAgent: (proxy && (proxy.startsWith("http") || proxy.startsWith("https") ? new https_proxy_agent_1.HttpsProxyAgent(proxy) : proxy.startsWith("socks") ? new socks_proxy_agent_1.SocksProxyAgent(proxy) : undefined)) || undefined }) .then((data) => { const cookie = data.headers["set-cookie"][0].split(";")[0]; const $ = (0, cheerio_1.load)(data.data); const input = $("div > input").map((_, el) => $(el)); const request = { [input.get(0).attr("name")]: url, [input.get(1).attr("name")]: input.get(1).attr("value"), [input.get(2).attr("name")]: input.get(2).attr("value") }; resolve({ status: "success", request, cookie }); }) .catch((e) => resolve({ status: "error", message: "Failed to get the request form!" })); }); const MusicalDown = (url, proxy) => new Promise(async (resolve) => { const request = await getRequest(url); if (request.status !== "success") return resolve({ status: "error", message: request.message }); (0, axios_1.default)(api_1._musicaldownapi, { method: "POST", headers: { cookie: request.cookie, "Content-Type": "application/x-www-form-urlencoded", Origin: "https://musicaldown.com", Referer: "https://musicaldown.com/en", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0" }, data: new URLSearchParams(Object.entries(request.request)), httpsAgent: (proxy && (proxy.startsWith("http") || proxy.startsWith("https") ? new https_proxy_agent_1.HttpsProxyAgent(proxy) : proxy.startsWith("socks") ? new socks_proxy_agent_1.SocksProxyAgent(proxy) : undefined)) || undefined }) .then(async ({ data }) => { const $ = (0, cheerio_1.load)(data); const images = []; $("div.row > div[class='col s12 m3']") .get() .map((v) => { images.push($(v).find("img").attr("src")); }); if (images.length !== 0) { resolve({ status: "success", result: { type: "image", images } }); } else { let i = 1; let videos = {}; $("div.row > div") .map((_, el) => $(el)) .get(1) .find("a") .get() .map((v) => { if ($(v).attr("href") !== "#modal2") { if (!isURL($(v).attr("href"))) return; videos[$(v).attr("data-event").includes("hd") ? "videoHD" : $(v).attr("data-event").includes("mp4") ? "videoSD" : $(v).attr("data-event").includes("watermark") ? "videoWatermark" : $(v).attr("href").includes("type=mp3") && "music"] = $(v).attr("href") != undefined ? $(v).attr("href") : /downloadX\('([^']+)'\)/.exec($(v).attr("onclick"))[1]; i++; } }); if (Object.keys(videos).length === 0) return resolve({ status: "success", message: "There is an error. Can't find download link" }); resolve({ status: "success", result: { type: "video", author: { avatar: $("div.img-area > img").attr("src"), nickname: $("h2.video-author > b").text() }, desc: $("p.video-desc").text(), ...videos } }); } }) .catch((e) => resolve({ status: "error", message: e.message })); }); exports.MusicalDown = MusicalDown; const isURL = (url) => { let status = false; try { new URL(url); status = true; } catch { status = false; } return status; };