UNPKG

tiktok-downloader

Version:

A JavaScript package to download videos from TikTok.

209 lines (208 loc) 12 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var bypassCorsHeaders = "https://cors-tiktok.herokuapp.com/?u="; //Bypass CORS (Github : https://github.com/abdelyouni/cors-tiktok) var linkPattern = '<link data-react-helmet="true" rel="canonical" href="'; var greaterSymbol = '>'; var closingTag = '/>'; var invertedTag = '</'; var idPattern = 'id="__NEXT_DATA__"'; var downloadParameter = '&d=1'; var tiktokRegex = /https?:\/\/www\.tiktok\.com\/@.+\/video\/\d*/gm; var tiktokMobileRegex = /https?:\/\/m\.tiktok\.com\/v\/\d*/gm; var tiktokMobileAltRegex = /https?:\/\/vm\.tiktok\.com\/\d*/gm; var JSTikTok; (function (JSTikTok) { function validateUrl(url) { return url && (tiktokRegex.test(url) || tiktokMobileRegex.test(url) || tiktokMobileAltRegex.test(url)); } JSTikTok.validateUrl = validateUrl; function urlEncode(str) { if (str) { str = encodeURIComponent(str) .replace('!', '%21') .replace('\'', '%27') .replace('(', '%28') .replace(')', '%29') .replace('*', '%2A') .replace('%20', '+'); } return str; } JSTikTok.urlEncode = urlEncode; function getBlob(url) { return new Promise(function (response, reject) { var xhr = new XMLHttpRequest(); xhr.open('get', url, true); xhr.responseType = 'blob'; xhr.onload = function () { return response(xhr.response); }; xhr.onerror = function (e) { return reject(e); }; xhr.send(); }); } JSTikTok.getBlob = getBlob; function getInfo(url) { return __awaiter(this, void 0, void 0, function () { var tiktokUrl, resp, json, video, music, author; return __generator(this, function (_a) { switch (_a.label) { case 0: tiktokUrl = bypassCorsHeaders + urlEncode(url); return [4 /*yield*/, fetch(tiktokUrl).then(function (response) { return response.text(); }).then(function (data) { return data; })]; case 1: resp = _a.sent(); if (!resp.includes(linkPattern)) return [3 /*break*/, 3]; tiktokUrl = urlEncode(resp.split(linkPattern)[1].split(greaterSymbol)[0]); return [4 /*yield*/, fetch(bypassCorsHeaders + tiktokUrl).then(function (response) { return response.text(); }).then(function (data) { return data; })]; case 2: resp = _a.sent(); _a.label = 3; case 3: json = JSON.parse(resp.split(idPattern)[1].split(invertedTag)[0].split(closingTag)[1]); video = { id: json.props.pageProps.itemInfo.itemStruct.video.id, height: json.props.pageProps.itemInfo.itemStruct.video.height, duration: json.props.pageProps.itemInfo.itemStruct.video.duration, sizeFormat: json.props.pageProps.itemInfo.itemStruct.video.ratio, cover: json.props.pageProps.itemInfo.itemStruct.video.cover, animatedCover: json.props.pageProps.itemInfo.itemStruct.video.dynamicCover, likes: json.props.pageProps.itemInfo.itemStruct.stats.diggCount, shares: json.props.pageProps.itemInfo.itemStruct.stats.shareCount, comments: json.props.pageProps.itemInfo.itemStruct.stats.commentCount, views: json.props.pageProps.itemInfo.itemStruct.stats.playCount, title: json.props.pageProps.metaParams.title, keywords: json.props.pageProps.metaParams.keywords, description: json.props.pageProps.metaParams.description, original_url: json.props.pageProps.metaParams.canonicalHref, download_url: json.props.pageProps.itemInfo.itemStruct.video.downloadAddr, }; music = { id: json.props.pageProps.itemInfo.itemStruct.music.id, title: json.props.pageProps.itemInfo.itemStruct.music.title, cover_large: json.props.pageProps.itemInfo.itemStruct.music.coverLarge, cover_medium: json.props.pageProps.itemInfo.itemStruct.music.coverMedium, cover_small: json.props.pageProps.itemInfo.itemStruct.music.coverThumb, artist: json.props.pageProps.itemInfo.itemStruct.music.authorName, album: json.props.pageProps.itemInfo.itemStruct.music.album, duration: json.props.pageProps.itemInfo.itemStruct.music.duration, url: json.props.pageProps.itemInfo.itemStruct.music.playUrl, }; author = { id: json.props.pageProps.itemInfo.itemStruct.author.id, uniqueId: json.props.pageProps.itemInfo.itemStruct.author.uniqueId, username: json.props.pageProps.itemInfo.itemStruct.author.nickname, avatar_large: json.props.pageProps.itemInfo.itemStruct.author.avatarLarger, avatar_medium: json.props.pageProps.itemInfo.itemStruct.author.avatarMedium, avatar_small: json.props.pageProps.itemInfo.itemStruct.author.avatarThumb, signature: json.props.pageProps.itemInfo.itemStruct.author.signature, createDate: json.props.pageProps.itemInfo.itemStruct.author.createTime, isVerified: json.props.pageProps.itemInfo.itemStruct.author.verified, followers: json.props.pageProps.itemInfo.itemStruct.authorStats.followerCount, followings: json.props.pageProps.itemInfo.itemStruct.authorStats.followingCount, hearts: json.props.pageProps.itemInfo.itemStruct.authorStats.heart, totalVideos: json.props.pageProps.itemInfo.itemStruct.authorStats.videoCount, diggCount: json.props.pageProps.itemInfo.itemStruct.authorStats.diggCount, }; return [2 /*return*/, { video: video, music: music, author: author }]; } }); }); } JSTikTok.getInfo = getInfo; function downloadMusic(url) { return __awaiter(this, void 0, void 0, function () { var info, downloadUrl, blob; return __generator(this, function (_a) { switch (_a.label) { case 0: if (url == null) { throw 'No URL was provided.'; } if (!validateUrl(url)) { throw 'The URL provided is not valid.'; } return [4 /*yield*/, getInfo(url)]; case 1: info = _a.sent(); downloadUrl = bypassCorsHeaders + urlEncode(info.music.url) + downloadParameter; return [4 /*yield*/, getBlob(downloadUrl)]; case 2: blob = _a.sent(); return [2 /*return*/, { blob: blob, fileName: info.music.id, type: 'mp3' }]; } }); }); } JSTikTok.downloadMusic = downloadMusic; function downloadVideo(url) { return __awaiter(this, void 0, void 0, function () { var info, downloadUrl, blob; return __generator(this, function (_a) { switch (_a.label) { case 0: if (url == null) { throw 'No URL was provided.'; } if (!validateUrl(url)) { throw 'The URL provided is not valid.'; } return [4 /*yield*/, getInfo(url)]; case 1: info = _a.sent(); downloadUrl = bypassCorsHeaders + urlEncode(info.video.download_url) + downloadParameter; return [4 /*yield*/, getBlob(downloadUrl)]; case 2: blob = _a.sent(); return [2 /*return*/, { blob: blob, fileName: info.video.id, type: 'mp4' }]; } }); }); } JSTikTok.downloadVideo = downloadVideo; })(JSTikTok = exports.JSTikTok || (exports.JSTikTok = {}));