handle shareable links
This commit is contained in:
parent
a3f1adfc88
commit
a39167b8ab
@ -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 => {
|
||||
// If it's already just a number, return it
|
||||
if (/^\d+$/.test(input)) {
|
||||
@ -288,7 +308,12 @@ export const Collection = async (
|
||||
}
|
||||
): Promise<TiktokCollectionResponse> => {
|
||||
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",
|
||||
|
||||
@ -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}`)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user