diff --git a/src/client/cookie.ts b/src/client/cookie.ts new file mode 100644 index 0000000..985b5ef --- /dev/null +++ b/src/client/cookie.ts @@ -0,0 +1,55 @@ +import { TiktokAPI, SSSTik, MusicalDown, TiktokStalk } from "../index" + +export class Client { + private cookie: string[] + + constructor() { + this.cookie = [] + } + + public getCookie(): string { + if (this.cookie.length === 0) throw new Error("Please login your account first!") + return this.cookie[0] + } + + public getAllCookie(): string[] { + if (this.cookie.length === 0) throw new Error("Please login your account first!") + return this.cookie + } + + public setCookie(cookie: string) { + this.cookie.push(cookie) + } + + public deleteCookie(index: number) { + this.cookie.splice(index - 1, 1) + } + + public clearCookie() { + this.cookie = [] + } + + public TiktokDL = (url: string, options: { version: "v1" | "v2" | "v3" }) => + new Promise(async (resolve, reject) => { + switch (options.version) { + case "v1": { + await TiktokAPI(url).then(resolve).catch(reject) + } + case "v2": { + await SSSTik(url).then(resolve).catch(reject) + } + case "v3": { + await MusicalDown(url).then(resolve).catch(reject) + } + default: { + await TiktokAPI(url).then(resolve).catch(reject) + } + } + }) + + public TiktokStalk = (username: string) => + new Promise(async (resolve, reject) => { + const cookie = this.getCookie() + await TiktokStalk(username, { cookie: cookie }).then(resolve).catch(reject) + }) +} diff --git a/src/index.ts b/src/index.ts index 3eeaeaa..475e30b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,4 @@ -export * from "./utils" +export * from "./utils/dl_tiktokapi" +export * from "./utils/dl_ssstik" +export * from "./utils/dl_musicaldown" +export * from "./utils/tiktok_stalker" diff --git a/src/types/ssstik.ts b/src/types/ssstik.ts index b3e8fb3..b3bc25d 100644 --- a/src/types/ssstik.ts +++ b/src/types/ssstik.ts @@ -4,7 +4,7 @@ export interface SSSTikFetchTT { result?: string } -export interface SSSTikResult { +export interface SSSTikResponse { status: "success" | "error" message?: string result?: { diff --git a/src/types/tiktokapi.ts b/src/types/tiktokApi.ts similarity index 96% rename from src/types/tiktokapi.ts rename to src/types/tiktokApi.ts index f07846d..58c8420 100644 --- a/src/types/tiktokapi.ts +++ b/src/types/tiktokApi.ts @@ -1,4 +1,4 @@ -export interface DLResult { +export interface TiktokAPIResponse { status: "success" | "error" message?: string result?: { diff --git a/src/utils/musicaldown.ts b/src/utils/downloader_musicaldown.ts similarity index 100% rename from src/utils/musicaldown.ts rename to src/utils/downloader_musicaldown.ts diff --git a/src/utils/ssstik.ts b/src/utils/downloader_ssstik.ts similarity index 95% rename from src/utils/ssstik.ts rename to src/utils/downloader_ssstik.ts index 2845324..9375dee 100644 --- a/src/utils/ssstik.ts +++ b/src/utils/downloader_ssstik.ts @@ -1,6 +1,6 @@ import Axios from "axios" import { load } from "cheerio" -import { Author, Statistics, SSSTikFetchTT, SSSTikResult } from "../types/ssstik" +import { Author, Statistics, SSSTikFetchTT, SSSTikResponse } from "../types/ssstik" import { _ssstikapi, _ssstikurl } from "../api" /** @@ -29,7 +29,7 @@ const fetchTT = () => }) export const SSSTik = (url: string) => - new Promise(async (resolve, reject) => { + new Promise(async (resolve, reject) => { const tt: SSSTikFetchTT = await fetchTT() if (tt.status !== "success") return resolve({ status: "error", message: tt.message }) Axios(_ssstikapi, { diff --git a/src/utils/tiktokapi.ts b/src/utils/downloader_tiktokApi.ts similarity index 96% rename from src/utils/tiktokapi.ts rename to src/utils/downloader_tiktokApi.ts index 04a0fcd..590bfa8 100644 --- a/src/utils/tiktokapi.ts +++ b/src/utils/downloader_tiktokApi.ts @@ -1,9 +1,9 @@ import axios from "axios" import { _tiktokapi, _tiktokurl } from "../api" -import { Author, DLResult, Statistics, Music } from "../types/tiktokapi" +import { Author, TiktokAPIResponse, Statistics, Music } from "../types/tiktokApi" -export const TiktokAPI = (url: string): Promise => - new Promise((resolve, reject) => { +export const TiktokAPI = (url: string) => + new Promise((resolve, reject) => { url = url.replace("https://vm", "https://vt") axios .head(url) diff --git a/src/utils/index.ts b/src/utils/index.ts deleted file mode 100644 index c1740ab..0000000 --- a/src/utils/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./switch" -export * from "./stalker" diff --git a/src/utils/switch.ts b/src/utils/switch.ts index 9112336..6951407 100644 --- a/src/utils/switch.ts +++ b/src/utils/switch.ts @@ -1,9 +1,12 @@ -import { MusicalDown } from "./musicaldown" -import { SSSTik } from "./ssstik" -import { TiktokAPI } from "./tiktokapi" +import { MusicalDownResponse } from "../types/musicaldown" +import { SSSTikResponse } from "../types/ssstik" +import { TiktokAPIResponse } from "../types/tiktokApi" +import { MusicalDown } from "./downloader_musicaldown" +import { SSSTik } from "./downloader_ssstik" +import { TiktokAPI } from "./downloader_tiktokApi" export const TiktokDL = (url: string, options: { version: "v1" | "v2" | "v3" }) => - new Promise(async (resolve, reject) => { + new Promise(async (resolve, reject) => { switch (options.version) { case "v1": { await TiktokAPI(url).then(resolve).catch(reject) diff --git a/src/utils/stalker.ts b/src/utils/tiktok_stalker.ts similarity index 98% rename from src/utils/stalker.ts rename to src/utils/tiktok_stalker.ts index fdbd955..81a294d 100644 --- a/src/utils/stalker.ts +++ b/src/utils/tiktok_stalker.ts @@ -19,7 +19,7 @@ export const TiktokStalk = (username: string, options: { cookie: string }): Prom axios .get(`${_tiktokurl}/@${username}`, { headers: { - "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36", + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36", cookie: (options?.cookie ? options.cookie : await getCookie()) as string } })