From f00fa49394eab24b9c6af61e38796e9d59038841 Mon Sep 17 00:00:00 2001 From: TobyG74 Date: Sat, 6 Apr 2024 22:23:03 +0700 Subject: [PATCH] fix: regex validate tiktok url --- README.md | 37 +++++++++++++++---- package.json | 2 +- src/index.ts | 4 +- .../downloader/downloader_musicaldown.ts | 2 +- src/utils/downloader/downloader_ssstik.ts | 2 +- src/utils/downloader/downloader_tiktokApi.ts | 2 +- 6 files changed, 34 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 22091e0..96d4fbe 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ - [Description](#description) - [How to get Tiktok Cookie](#how-to-get-tiktok-cookie) +- [Build this project](#build-this-project) - [Install](#install) - [From NPM](#from-npm) - [From Yarn](#from-yarn) @@ -41,12 +42,32 @@ This project uses the Unofficial API from Tiktok. # How to get Tiktok Cookie -(If the Default Cookie Has Expired) +You can use cookies in the form of String or JSON -- Login at [Tiktok](https://www.tiktok.com/) -- When you have finished logging in, you can press CTRL + SHIFT + I or Right Click and select Inspect -- Go to the Console section then type document.cookie -- Then Copy Paste your Cookie and Use it for TiktokStalk +- Method 1 (String) : + + Login to the [Tiktok website](https://tiktok/.com) -> Open the **Inspect Element** or you can press the shortcut **CTRL + SHIFT + I** -> Go to network -> Find the request that goes to tiktok then open the request and look for **Cookie** + +![Capture](https://github.com/TobyG74/tiktok-api-dl/assets/32604979/c516e5e8-7b8f-43cb-aa2e-2bbe92944dac) + +- Method 2 (JSON) : + + You can use the chrome extension [EditThisCookies](https://chromewebstore.google.com/detail/editthiscookie/fngmhnnpilhplaeedifhccceomclgfbg) -> Login to the [Tiktok website](https://tiktok/.com) -> Then open the **EditThisCookie** extension -> Then click **Export Cookie** + +![Capture2](https://github.com/TobyG74/tiktok-api-dl/assets/32604979/fa0e4bc7-a646-4551-a974-160881a98198) + +# Build this project + +- Clone the repository +- Install the dependencies +- Run the build script + +```bash +git clone https://github.com/TobyG74/tiktok-api-dl.git +cd tiktok-api-dl +npm install +npm run build +``` # Install @@ -79,7 +100,7 @@ npm install github:TobyG74/tiktok-api-dl - V3 uses the API from [MusicalDown](https://musicaldown.com/) ```js -const { default: Tiktok } = require("@tobyg74/tiktok-api-dl") +const Tiktok = require("@tobyg74/tiktok-api-dl") const tiktok_url = "https://vt.tiktok.com/ZS84BnrU9" @@ -97,7 +118,7 @@ Tiktok.Downloader(tiktok_url, { - Using Your Cookie ```js -const { default: Tiktok } = require("@tobyg74/tiktok-api-dl") +const Tiktok = require("@tobyg74/tiktok-api-dl") const username = "tobz2k19" @@ -115,7 +136,7 @@ Tiktok.Search(username, { - Using Your Cookie ```js -const { default: Tiktok } = require("@tobyg74/tiktok-api-dl") +const Tiktok = require("@tobyg74/tiktok-api-dl") const username = "tobz2k19" diff --git a/package.json b/package.json index 8363c79..b27c044 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tobyg74/tiktok-api-dl", - "version": "1.1.1", + "version": "1.1.2", "description": "Scraper for downloading media in the form of videos, images and audio from Tiktok. Also for stalking Tiktok Users", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 2a969ec..2cdb20e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,7 +17,7 @@ import { StalkResult } from "./types/search/stalker" type TiktokDownloaderResponse = T extends "v1" ? TiktokAPIResponse : T extends "v2" ? SSSTikResponse : T extends "v3" ? MusicalDownResponse : TiktokAPIResponse type TiktokSearchResponse = T extends "user" ? TiktokUserSearchResponse : T extends "video" ? any : TiktokUserSearchResponse -export const Tiktok = { +export = { Downloader: async (url: string, options?: { version: T }): Promise> => { switch (options?.version) { case "v1": { @@ -59,5 +59,3 @@ export const Tiktok = { return response } } - -export default Tiktok diff --git a/src/utils/downloader/downloader_musicaldown.ts b/src/utils/downloader/downloader_musicaldown.ts index 49ed943..ddc8674 100644 --- a/src/utils/downloader/downloader_musicaldown.ts +++ b/src/utils/downloader/downloader_musicaldown.ts @@ -8,7 +8,7 @@ import { _musicaldownapi, _musicaldownmusicapi, _musicaldownurl } from "../../co * BASE URL : https://ssstik.io */ -const TiktokURLregex = /(?:http[s]?:\/\/)?(?:www\.|m\.)?(?:tiktok\.com\/(?:@[\w.-]+\/video\/|@[\w.-]+\/video\/))?(\d+)/ +const TiktokURLregex = /https:\/\/(?:m|www|vm|vt)?\.?tiktok\.com\/((?:.*\b(?:(?:usr|v|embed|user|video)\/|\?shareId=|\&item_id=)(\d+))|\w+)/ const getRequest = (url: string) => new Promise((resolve, reject) => { diff --git a/src/utils/downloader/downloader_ssstik.ts b/src/utils/downloader/downloader_ssstik.ts index fbde18d..87e5aa3 100644 --- a/src/utils/downloader/downloader_ssstik.ts +++ b/src/utils/downloader/downloader_ssstik.ts @@ -8,7 +8,7 @@ import { _ssstikapi, _ssstikurl } from "../../constants/api" * BASE URL : https://ssstik.io */ -const TiktokURLregex = /(?:http[s]?:\/\/)?(?:www\.|m\.)?(?:tiktok\.com\/(?:@[\w.-]+\/video\/|@[\w.-]+\/video\/))?(\d+)/ +const TiktokURLregex = /https:\/\/(?:m|www|vm|vt)?\.?tiktok\.com\/((?:.*\b(?:(?:usr|v|embed|user|video)\/|\?shareId=|\&item_id=)(\d+))|\w+)/ const fetchTT = () => new Promise(async (resolve, reject) => { diff --git a/src/utils/downloader/downloader_tiktokApi.ts b/src/utils/downloader/downloader_tiktokApi.ts index cdff051..d734a55 100644 --- a/src/utils/downloader/downloader_tiktokApi.ts +++ b/src/utils/downloader/downloader_tiktokApi.ts @@ -3,7 +3,7 @@ import { _tiktokapi, _tiktokurl } from "../../constants/api" import { _tiktokApiParams } from "../../constants/params" import { Author, TiktokAPIResponse, Statistics, Music, responseParser, Video } from "../../types/downloader/tiktokApi" -const TiktokURLregex = /(?:http[s]?:\/\/)?(?:www\.|m\.)?(?:tiktok\.com\/(?:@[\w.-]+\/video\/|@[\w.-]+\/video\/))?(\d+)/ +const TiktokURLregex = /https:\/\/(?:m|www|vm|vt)?\.?tiktok\.com\/((?:.*\b(?:(?:usr|v|embed|user|video)\/|\?shareId=|\&item_id=)(\d+))|\w+)/ /** * Tiktok API Downloader