Skip to content

Commit

Permalink
yo who up es'ing they lint fr
Browse files Browse the repository at this point in the history
  • Loading branch information
KodingDev committed Sep 24, 2022
1 parent e768bab commit d282ddb
Show file tree
Hide file tree
Showing 5 changed files with 895 additions and 45 deletions.
36 changes: 36 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"root": true,
"env": {
"node": true,
"es6": true
},
"ignorePatterns": [
"node_modules/*",
"src/generated/*"
],
"extends": [
"eslint:recommended"
],
"overrides": [
{
"files": [
"**/*.ts"
],
"parser": "@typescript-eslint/parser",
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"rules": {
"prettier/prettier": [
"error",
{},
{
"usePrettierrc": true
}
]
}
}
]
}
7 changes: 7 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi": true,
"trailingComma": "all",
"singleQuote": false,
"tabWidth": 2,
"useTabs": false
}
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@
"license": "AGPL-3.0-or-later",
"scripts": {
"start": "wrangler dev",
"deploy": "wrangler publish"
"deploy": "wrangler publish",
"lint:fix": "yarn run --silent lint:prettier:fix && yarn run --silent lint:eslint:fix",
"lint:prettier:fix": "prettier -w \"src/**/*.{js,jsx,ts,tsx}\" -u",
"lint:eslint:fix": "eslint --fix \"src/**/*.{js,jsx,ts,tsx}\""
},
"devDependencies": {
"@cloudflare/workers-types": "^3.16.0",
"@typescript-eslint/eslint-plugin": "^5.38.0",
"@typescript-eslint/parser": "^5.38.0",
"eslint": "^8.24.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"prettier": "^2.7.1",
"typescript": "^4.8.3",
"wrangler": "2.1.6"
},
Expand Down
86 changes: 46 additions & 40 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,61 +19,67 @@
// inshallah this works
// 🦀 crab in the code

const TT_API = "https://t.tiktok.com/api/item/detail/?itemId="
const USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0"
const TT_API = "https://t.tiktok.com/api/item/detail/?itemId=";
const USER_AGENT =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0";

async function get(url: string) { // Wrapper for fetch, pretend to be a human.
return await fetch(url, {
headers: {
"User-Agent": USER_AGENT
},
})
async function get(url: string) {
// Wrapper for fetch, pretend to be a human.
return await fetch(url, {
headers: {
"User-Agent": USER_AGENT,
},
});
}

async function getVideoURL(videoID: string): Promise<string | undefined> {
let res = await get(TT_API + videoID)
let json: any = await res.json() // TODO: type
let videoURL = undefined
const res = await get(TT_API + videoID);
const json: any = await res.json(); // TODO: type
let videoURL = undefined;

try {
videoURL = json?.itemInfo?.itemStruct?.video?.downloadAddr
} catch (e) { /* TODO: Maybe do something here... an image, perhaps? */
}
try {
videoURL = json?.itemInfo?.itemStruct?.video?.downloadAddr;
} catch (e) {
/* TODO: Maybe do something here... an image, perhaps? */
}

return videoURL
return videoURL;
}

async function idFromTikTokURL(url: string) {
let videoID = new URL(url).pathname.split("/").pop()
if (!videoID) return undefined
const videoID = new URL(url).pathname.split("/").pop();
if (!videoID) return undefined;

return await getVideoURL(videoID)
return await getVideoURL(videoID);
}

export default {
async fetch(request: Request): Promise<Response> {
let url = new URL(request.url)
// If the URL is like /@username/video/1234567891234567891
if (url.pathname.match(/\/@[^\/]+\/video\/\d+/)) {
let videoURL = await idFromTikTokURL(request.url)
if (videoURL) return Response.redirect(videoURL, 302)
}
async fetch(request: Request): Promise<Response> {
const url = new URL(request.url);
// If the URL is like /@username/video/1234567891234567891
if (url.pathname.match(/\/@[^/]+\/video\/\d+/)) {
const videoURL = await idFromTikTokURL(request.url);
if (videoURL) return Response.redirect(videoURL, 302);
}

// If the URL is like https://www.tiktok.com/t/ZTRav7308
if (url.pathname.match(/\/t\/\w+/)) {
let videoID = url.pathname.replace("/t/", "").replace(/\/$/, "")
if (!videoID) return new Response("Failed to parse Video ID from t URL", {status: 400})
// If the URL is like https://www.tiktok.com/t/ZTRav7308
if (url.pathname.match(/\/t\/\w+/)) {
const videoID = url.pathname.replace("/t/", "").replace(/\/$/, "");
if (!videoID)
return new Response("Failed to parse Video ID from t URL", {
status: 400,
});

// We actually need to go to that page to get a URL we like :)
let res = await get("https://www.tiktok.com/t/" + videoID)
// We actually need to go to that page to get a URL we like :)
const res = await get("https://www.tiktok.com/t/" + videoID);

let videoURL = await idFromTikTokURL(res.url)
if (videoURL) return Response.redirect(videoURL, 302)
}
const videoURL = await idFromTikTokURL(res.url);
if (videoURL) return Response.redirect(videoURL, 302);
}

// There's a third one, vm.tiktok.com/VIDEO_ID
// But, I don't know how to host the subdomain thing in a Worker right now.
// There's a third one, vm.tiktok.com/VIDEO_ID
// But, I don't know how to host the subdomain thing in a Worker right now.

return Response.redirect("https://britmoji.org/")
}
}
return Response.redirect("https://britmoji.org/");
},
};
Loading

0 comments on commit d282ddb

Please sign in to comment.