Skip to content

Commit

Permalink
fix: tiktok api urls not working
Browse files Browse the repository at this point in the history
  • Loading branch information
okdargy committed Mar 29, 2024
1 parent b3bc687 commit a6f715a
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 109 deletions.
88 changes: 59 additions & 29 deletions src/services/tiktok.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,63 @@
import { TikTokAPIResponse, AwemeList } from "../types/Services"
import { TikTokAPIResponse, AwemeList } from "../types/Services";

export async function grabAwemeId(videoId: string): Promise<String | Error> {
// https://vm.tiktok.com/ZMJmVWVpL/
const res = await fetch('https://vm.tiktok.com/' + videoId)
const url = new URL(res.url)

const awemeIdPattern = /\/@[\w\d_.]+\/(video|photo)\/(\d{19})/
const match = url.pathname.match(awemeIdPattern)

if (match) {
return match[2]
} else {
throw new Error('Could not find awemeId')
}
// https://vm.tiktok.com/ZMJmVWVpL/
const res = await fetch("https://vm.tiktok.com/" + videoId);
const url = new URL(res.url);

const awemeIdPattern = /\/@[\w\d_.]+\/(video|photo)\/(\d{19})/;
const match = url.pathname.match(awemeIdPattern);

if (match) {
return match[2];
} else {
throw new Error("Could not find awemeId");
}
}

export async function getVideoInfo(awemeId: String): Promise<AwemeList | Error> {
const res: Response = await fetch('https://api19-normal-c-useast2a.tiktokv.com/aweme/v1/feed/?aweme_id=' + awemeId, {
cf: {
cacheEverything: true,
cacheTtlByStatus: { "200-299": 86400, 404: 1, "500-599": 0 },
},
})
const json: TikTokAPIResponse = await res.json()
const videoInfo: AwemeList | undefined = json.aweme_list.find((aweme) => aweme.aweme_id === awemeId)

if (videoInfo) {
return videoInfo
} else {
return new Error('Could not find video info')
}
}
export async function getVideoInfo(
awemeId: String,
): Promise<AwemeList | Error> {
const apiUrl = new URL(
"https://api22-normal-c-alisg.tiktokv.com/aweme/v1/feed/?region=US&carrier_region=US",
);

const params = {
aweme_id: awemeId,
iid: "7318518857994389254",
device_id: "7318517321748022790",
channel: "googleplay",
app_name: "musical_ly",
version_code: "300904",
device_platform: "android",
device_type: "ASUS_Z01QD",
os_version: "9",
};

Object.keys(params).forEach((key) =>
apiUrl.searchParams.append(key, params[key]),
);

console.log(apiUrl.toString());

const res: Response = await fetch(apiUrl.toString(), {
headers: {
"User-Agent":
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Mobile Safari/537.36",
},
cf: {
cacheEverything: true,
cacheTtlByStatus: { "200-299": 86400, 404: 1, "500-599": 0 },
},
});
const json: TikTokAPIResponse = await res.json();
const videoInfo: AwemeList | undefined = json.aweme_list.find(
(aweme) => aweme.aweme_id === awemeId,
);

if (videoInfo) {
return videoInfo;
} else {
return new Error("Could not find video info");
}
}
169 changes: 89 additions & 80 deletions src/templates/pages/VideoResponse.tsx
Original file line number Diff line number Diff line change
@@ -1,86 +1,95 @@
import { AwemeList } from '../../types/Services';
import MetaHelper from '../../util/MetaHelper';
import { AwemeList } from "../../types/Services";
import MetaHelper from "../../util/MetaHelper";

export function VideoResponse(data: AwemeList): JSX.Element {
let videoUrl = 'https://fxtiktok-rewrite.dargy.workers.dev/generate/video/' + data.aweme_id
let videoUrl =
"https://fxtiktok-rewrite.dargy.workers.dev/generate/video/" +
data.aweme_id;

if(data.video.duration > 0) {
const awemeVideo = data.video.bit_rate[0].play_addr.url_list.find((url) => url.includes('/aweme/v1/play'))
if (awemeVideo) {
const url = new URL(awemeVideo)
const videoId = url.searchParams.get('video_id')
const fileId = url.searchParams.get('file_id')
if (data.video.duration > 0) {
const awemeVideo =
data.video.play_addr.url_list.find((url) =>
url.includes("/aweme/v1/play"),
) ||
data.video.bit_rate[0].play_addr.url_list.find((url) =>
url.includes("/aweme/v1/play"),
);

videoUrl = `https://${url.hostname}/aweme/v1/play/?video_id=${videoId}&file_id=${fileId}&item_id=${data.aweme_id}`
}
if (awemeVideo) {
const url = new URL(awemeVideo);

const videoId = url.searchParams.get("video_id");
const fileId = url.searchParams.get("file_id");

videoUrl = `https://${url.hostname}/aweme/v1/play/?video_id=${videoId}&file_id=${fileId}&item_id=${data.aweme_id}`;
}
}

return (
<>
{
MetaHelper([
{
name: 'og:title',
content: `${data.author.nickname} (@${data.author.unique_id})` // Nickname (@username)
},
{
name: 'theme-color',
content: '#ff0050' // TikTok's theme color
},
{
name: 'twitter:card',
content: `${data.video.duration !== 0 ? 'player' : 'summary_large_image'}`
},
{
name: 'twitter:site',
content: `@${data.author.unique_id}` // @username
},
{
name: 'twitter:creator',
content: `@${data.author.unique_id}` // @username
},
{
name: 'twitter:title',
content: `${data.author.nickname} (@${data.author.unique_id})` // Nickname (@username)
},
{
name: 'og:url',
content: data.share_url
},
{
name: 'og:description',
content: data.video.duration !== 0 ? data.desc : null
},
{
name: `og:${data.video.duration !== 0 ? 'video' : 'image'}`,
content: `${data.video.duration !== 0 ? videoUrl : 'https://fxtiktok-rewrite.dargy.workers.dev/generate/image/' + data.aweme_id}`
},
{
name: 'og:type',
content: `${data.video.duration !== 0 ? 'video.other' : 'image.other'}`
},
{
name: `og:${data.video.duration !== 0 ? 'video' : 'image'}:type`,
content: `${data.video.duration !== 0 ? 'video/mp4' : 'image/jpeg'}`
},
{
name: `og:${data.video.duration !== 0 ? 'video' : 'image'}:width`,
content: `${data.video.duration !== 0 ? data.video.width : data.video.cover.width}`
},
{
name: `og:${data.video.duration !== 0 ? 'video' : 'image'}:height`,
content: `${data.video.duration !== 0 ? data.video.height : data.video.cover.height}`
},
], {
likes: data.statistics.digg_count,
comments: data.statistics.comment_count,
shares: data.statistics.share_count,
unique_id: data.author.unique_id,
images: data.image_post_info ? data.image_post_info.images.length : 0
})
}
</>
)
}
return (
<>
{MetaHelper(
[
{
name: "og:title",
content: `${data.author.nickname} (@${data.author.unique_id})`, // Nickname (@username)
},
{
name: "theme-color",
content: "#ff0050", // TikTok's theme color
},
{
name: "twitter:card",
content: `${data.video.duration !== 0 ? "player" : "summary_large_image"}`,
},
{
name: "twitter:site",
content: `@${data.author.unique_id}`, // @username
},
{
name: "twitter:creator",
content: `@${data.author.unique_id}`, // @username
},
{
name: "twitter:title",
content: `${data.author.nickname} (@${data.author.unique_id})`, // Nickname (@username)
},
{
name: "og:url",
content: data.share_url,
},
{
name: "og:description",
content: data.video.duration !== 0 ? data.desc : null,
},
{
name: `og:${data.video.duration !== 0 ? "video" : "image"}`,
content: `${data.video.duration !== 0 ? videoUrl : "https://fxtiktok-rewrite.dargy.workers.dev/generate/image/" + data.aweme_id}`,
},
{
name: "og:type",
content: `${data.video.duration !== 0 ? "video.other" : "image.other"}`,
},
{
name: `og:${data.video.duration !== 0 ? "video" : "image"}:type`,
content: `${data.video.duration !== 0 ? "video/mp4" : "image/jpeg"}`,
},
{
name: `og:${data.video.duration !== 0 ? "video" : "image"}:width`,
content: `${data.video.duration !== 0 ? data.video.width : data.video.cover.width}`,
},
{
name: `og:${data.video.duration !== 0 ? "video" : "image"}:height`,
content: `${data.video.duration !== 0 ? data.video.height : data.video.cover.height}`,
},
],
{
likes: data.statistics.digg_count,
comments: data.statistics.comment_count,
shares: data.statistics.share_count,
unique_id: data.author.unique_id,
images: data.image_post_info ? data.image_post_info.images.length : 0,
},
)}
</>
);
}

0 comments on commit a6f715a

Please sign in to comment.