handle errors

This commit is contained in:
Tobi Saputra 2023-03-25 00:51:45 +07:00
parent fc860d5cc0
commit e683e6fc62
2 changed files with 11 additions and 7 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@tobyg74/tiktok-api-dl", "name": "@tobyg74/tiktok-api-dl",
"version": "1.0.0", "version": "1.0.1-fix",
"description": "Scrapper for download Video, Image, Music from Tiktok", "description": "Scrapper for download Video, Image, Music from Tiktok",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@ -11,17 +11,21 @@ const TiktokDL = (url) => {
}) })
.then(({ request }) => { .then(({ request }) => {
const { responseUrl } = request.res; const { responseUrl } = request.res;
const ID = responseUrl.match(/\d{17,21}/g)[0]; let ID = responseUrl.match(/\d{17,21}/g)
if (ID === null) return reject({
status: "error",
message: "Failed to fetch tiktok url. Make sure your tiktok url is correct!",
});
ID = ID[0]
Axios(_tiktokapi(ID), { Axios(_tiktokapi(ID), {
method: "GET", method: "GET",
}) })
.then(({ data }) => { .then(({ data }) => {
const content = data.aweme_list.filter((v) => v.aweme_id === ID)[0]; const content = data.aweme_list.filter((v) => v.aweme_id === ID)[0];
if (content.aweme_id !== ID) if (!content) return resolve({
return resolve({ status: "error",
status: "error", message: "Failed to find tiktok data. Make sure your tiktok url is correct!",
message: "Failed to find tiktok data. Make sure your tiktok url is correct!", });
});
const statistics = { const statistics = {
play_count: content.statistics.play_count, play_count: content.statistics.play_count,
download_count: content.statistics.download_count, download_count: content.statistics.download_count,