refactor: add type and move API to api folder

This commit is contained in:
TobyG74 2023-10-12 18:33:25 +07:00
parent 8bccf19644
commit e649860c78
2 changed files with 12 additions and 9 deletions

View File

@ -1,2 +1,4 @@
export const _tiktokurl: string = "https://www.tiktok.com"
export const _tiktokapi = (id: string): string => `https://api.tiktokv.com/aweme/v1/feed/?aweme_id=${id}`
export const _ssstikapi: string = "https://ssstik.io/abc?url=dl"
export const _ssstikurl: string = "https://ssstik.io"

View File

@ -1,10 +1,11 @@
import Axios from "axios"
import { load } from "cheerio"
import { Author, Statistics, SSSTikFetchTT, SSSTikResult } from "../types/ssstik"
import { _ssstikapi, _ssstikurl } from "../api"
const fetchTT = () =>
new Promise<SSSTikFetchTT>(async (resolve, reject) => {
Axios.get("https://ssstik.io", {
Axios.get(_ssstikurl, {
headers: {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0"
}
@ -26,19 +27,19 @@ export const SSSTik = (url: string) =>
new Promise<SSSTikResult>(async (resolve, reject) => {
const tt: SSSTikFetchTT = await fetchTT()
if (tt.status !== "success") return resolve({ status: "error", message: tt.message })
Axios("https://ssstik.io/abc?url=dl", {
Axios(_ssstikapi, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
Origin: "https://ssstik.io",
Referer: "https://ssstik.io/en",
Origin: _ssstikurl,
Referer: _ssstikurl + "/en",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0"
},
data: new URLSearchParams(
Object.entries({
id: url,
locale: "en",
tt: tt.result
tt: tt.result as string
})
)
})
@ -48,7 +49,7 @@ export const SSSTik = (url: string) =>
// Result
const desc = $("p.maintext").text().trim()
const author: Author = {
avatar: $("img.result_author").attr("src"),
avatar: $("img.result_author").attr("src") as string,
nickname: $("h2").text().trim()
}
const statistics: Statistics = {
@ -62,7 +63,7 @@ export const SSSTik = (url: string) =>
$("ul.splide__list > li")
.get()
.map((img) => {
images.push($(img).find("img").attr("src"))
images.push($(img).find("img").attr("src") as string)
})
if (images.length !== 0) {
@ -75,7 +76,7 @@ export const SSSTik = (url: string) =>
author,
statistics,
images,
music: $("a.music").attr("href")
music: $("a.music").attr("href") as string
}
})
} else {
@ -88,7 +89,7 @@ export const SSSTik = (url: string) =>
author,
statistics,
video: $("a.without_watermark").attr("href"),
music: $("a.music").attr("href")
music: $("a.music").attr("href") as string
}
})
}