From a39167b8ab52a39013d14b43e43157454bc50717 Mon Sep 17 00:00:00 2001 From: Tino Laomahei Date: Sun, 25 May 2025 15:56:24 +1200 Subject: [PATCH] handle shareable links --- src/utils/downloader/tiktokApi.ts | 27 ++++++++++++++++++++++++++- test/collection-test.ts | 11 ++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/utils/downloader/tiktokApi.ts b/src/utils/downloader/tiktokApi.ts index 53507df..5bbea07 100644 --- a/src/utils/downloader/tiktokApi.ts +++ b/src/utils/downloader/tiktokApi.ts @@ -197,6 +197,26 @@ const createVideoResponse = ( } }) +const handleRedirect = async (url: string, proxy?: string): Promise => { + try { + const response = await Axios(url, { + method: 'HEAD', + maxRedirects: 5, + validateStatus: (status) => status >= 200 && status < 400, + ...createProxyAgent(proxy) + }) + + // Get the final URL after all redirects + const finalUrl = response.request.res.responseUrl + + // Remove query parameters + return finalUrl.split('?')[0] + } catch (error) { + console.error('Error handling redirect:', error) + return url + } +} + export const extractCollectionId = (input: string): string | null => { // If it's already just a number, return it if (/^\d+$/.test(input)) { @@ -288,7 +308,12 @@ export const Collection = async ( } ): Promise => { try { - const collectionId = extractCollectionId(collectionIdOrUrl) + // Only handle redirects if the input is a URL + const processedUrl = collectionIdOrUrl.startsWith('http') + ? await handleRedirect(collectionIdOrUrl, options?.proxy) + : collectionIdOrUrl + + const collectionId = extractCollectionId(processedUrl) if (!collectionId) { return { status: "error", diff --git a/test/collection-test.ts b/test/collection-test.ts index 056efc1..bc8f832 100644 --- a/test/collection-test.ts +++ b/test/collection-test.ts @@ -3,11 +3,13 @@ import { Collection } from "../src/utils/downloader/tiktokApi" async function testCollection() { try { // You can use either a collection ID or URL - const collectionIdOrUrl = "https://www.tiktok.com/@getrex.co.nz/collection/big%20back-7507916135931218695" + const collectionId = "7507916135931218695" + const collectionUrl = "https://www.tiktok.com/@getrex.co.nz/collection/big%20back-7507916135931218695" + const collectionShareableLink = "https://vt.tiktok.com/ZShvmqNjQ/" console.log("Testing Collection method...") - const result = await Collection(collectionIdOrUrl, { - page: 2, + const result = await Collection(collectionId, { + page: 1, count: 5, // Optional: Number of items to fetch proxy: undefined // Optional: Add your proxy if needed }) @@ -17,7 +19,6 @@ async function testCollection() { console.log("========================") console.log("Collection Overview:") console.log("========================") - console.log(`Collection ID: ${collectionIdOrUrl}`) console.log(`Total items fetched: ${result.result.itemList.length}`) console.log(`Has more items: ${result.result.hasMore}`) @@ -29,7 +30,7 @@ async function testCollection() { console.log(`Description: ${item.desc}`) console.log(`Author: ${item.author.nickname}`) console.log(`Created: ${new Date(item.createTime * 1000).toLocaleString()}`) - + // Log video URL if (item.video?.playAddr?.[0]) { console.log(`Video URL: ${item.video.playAddr[0]}`)