@riqtu/tiktok-scraper
Version:
Modified TikTok Scraper with proxy support
66 lines (65 loc) • 2 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = __importDefault(require("./api"));
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 api_1.default
.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 api_1.default.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];
};
exports.default = fetchAwemeList;