UNPKG

@riqtu/tiktok-scraper

Version:

Modified TikTok Scraper with proxy support

61 lines (60 loc) 1.76 kB
import apiClient from "./api.js"; const getAwemeId = async (url) => { const REGEXP = /(?:video|photo|user)\/(\d+)/; const valid = url.match(REGEXP); if (valid) { return valid[1]; } try { const { data, headers } = await apiClient .get(url, { maxRedirects: 0, }) .catch((e) => e.response); const match = data.match(REGEXP)?.[1] || headers.location.match(REGEXP)?.[1]; if (!match) { throw new Error(data); } return match; } catch (error) { console.error("Failed to fetch the aweme ID:", error); return null; } }; /** * Fetches the list of Aweme objects from the specified URL. * * @param url - The URL to fetch the Aweme list from. * @returns A promise that resolves to a tuple containing the Aweme ID and the list of Aweme objects, or null if the Aweme ID is not found or the data is invalid. */ const fetchAwemeList = async (url, agent) => { const aweme_id = await getAwemeId(url); if (!aweme_id) { return null; } function device_idGenerator() { return Array(19) .fill(0) .map(() => Math.floor(Math.random() * 10)) .join(""); } const { data } = await apiClient.request({ url: "/aweme/v1/feed/", method: "OPTIONS", params: { iid: device_idGenerator(), device_id: device_idGenerator(), version_code: "300904", aweme_id, }, httpAgent: agent, httpsAgent: agent, }); if (!data || !data.aweme_list) { return null; } const { aweme_list } = data; return [aweme_id, aweme_list]; }; export default fetchAwemeList;