handle shareable links

This commit is contained in:
Tino Laomahei 2025-05-25 15:56:24 +12:00
parent a3f1adfc88
commit a39167b8ab
2 changed files with 32 additions and 6 deletions

View File

@ -197,6 +197,26 @@ const createVideoResponse = (
} }
}) })
const handleRedirect = async (url: string, proxy?: string): Promise<string> => {
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 => { export const extractCollectionId = (input: string): string | null => {
// If it's already just a number, return it // If it's already just a number, return it
if (/^\d+$/.test(input)) { if (/^\d+$/.test(input)) {
@ -288,7 +308,12 @@ export const Collection = async (
} }
): Promise<TiktokCollectionResponse> => { ): Promise<TiktokCollectionResponse> => {
try { 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) { if (!collectionId) {
return { return {
status: "error", status: "error",

View File

@ -3,11 +3,13 @@ import { Collection } from "../src/utils/downloader/tiktokApi"
async function testCollection() { async function testCollection() {
try { try {
// You can use either a collection ID or URL // 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...") console.log("Testing Collection method...")
const result = await Collection(collectionIdOrUrl, { const result = await Collection(collectionId, {
page: 2, page: 1,
count: 5, // Optional: Number of items to fetch count: 5, // Optional: Number of items to fetch
proxy: undefined // Optional: Add your proxy if needed proxy: undefined // Optional: Add your proxy if needed
}) })
@ -17,7 +19,6 @@ async function testCollection() {
console.log("========================") console.log("========================")
console.log("Collection Overview:") console.log("Collection Overview:")
console.log("========================") console.log("========================")
console.log(`Collection ID: ${collectionIdOrUrl}`)
console.log(`Total items fetched: ${result.result.itemList.length}`) console.log(`Total items fetched: ${result.result.itemList.length}`)
console.log(`Has more items: ${result.result.hasMore}`) console.log(`Has more items: ${result.result.hasMore}`)
@ -29,7 +30,7 @@ async function testCollection() {
console.log(`Description: ${item.desc}`) console.log(`Description: ${item.desc}`)
console.log(`Author: ${item.author.nickname}`) console.log(`Author: ${item.author.nickname}`)
console.log(`Created: ${new Date(item.createTime * 1000).toLocaleString()}`) console.log(`Created: ${new Date(item.createTime * 1000).toLocaleString()}`)
// Log video URL // Log video URL
if (item.video?.playAddr?.[0]) { if (item.video?.playAddr?.[0]) {
console.log(`Video URL: ${item.video.playAddr[0]}`) console.log(`Video URL: ${item.video.playAddr[0]}`)