@tobyg74/tiktok-api-dl
Version:
Scraper for downloading media in the form of videos, images and audio from Tiktok. Also for stalking Tiktok Users
135 lines (134 loc) • 5.7 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getComments = void 0;
const axios_1 = __importDefault(require("axios"));
const api_1 = require("../../constants/api");
const params_1 = require("../../constants/params");
const https_proxy_agent_1 = require("https-proxy-agent");
const socks_proxy_agent_1 = require("socks-proxy-agent");
const TiktokURLregex = /https:\/\/(?:m|www|vm|vt|lite)?\.?tiktok\.com\/((?:.*\b(?:(?:usr|v|embed|user|video|photo)\/|\?shareId=|\&item_id=)(\d+))|\w+)/;
const getComments = async (url, proxy, commentLimit) => new Promise(async (resolve) => {
if (!TiktokURLregex.test(url)) {
return resolve({
status: "error",
message: "Invalid Tiktok URL. Make sure your url is correct!"
});
}
url = url.replace("https://vm", "https://vt");
(0, axios_1.default)(url, {
method: "HEAD",
httpsAgent: (proxy &&
(proxy.startsWith("http") || proxy.startsWith("https")
? new https_proxy_agent_1.HttpsProxyAgent(proxy)
: proxy.startsWith("socks")
? new socks_proxy_agent_1.SocksProxyAgent(proxy)
: undefined)) ||
undefined
})
.then(async ({ request }) => {
const { responseUrl } = request.res;
let ID = responseUrl.match(/\d{17,21}/g);
if (ID === null)
return resolve({
status: "error",
message: "Failed to fetch tiktok url. Make sure your tiktok url is correct!"
});
ID = ID[0];
const resultComments = await parseComments(ID, commentLimit, proxy);
return resolve({
status: "success",
result: resultComments.comments,
totalComments: resultComments.total
});
})
.catch((e) => resolve({ status: "error", message: e.message }));
});
exports.getComments = getComments;
const requestComments = async (id, commentLimit, proxy) => {
const { data } = await (0, axios_1.default)((0, api_1._tiktokGetComments)((0, params_1._getCommentsParams)(id, commentLimit)), {
method: "GET",
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"
},
httpsAgent: (proxy &&
(proxy.startsWith("http") || proxy.startsWith("https")
? new https_proxy_agent_1.HttpsProxyAgent(proxy)
: proxy.startsWith("socks")
? new socks_proxy_agent_1.SocksProxyAgent(proxy)
: undefined)) ||
undefined
});
return data;
};
const parseComments = async (id, commentLimit, proxy) => {
const comments = [];
let cursor = 0;
let total = 0;
let hasMore = true;
while (hasMore) {
const result = await requestComments(id, commentLimit, proxy);
hasMore = result.has_more === 1;
cursor = hasMore ? result.cursor : 0;
if (result.comments) {
result.comments.forEach((v) => {
const comment = {
cid: v.cid,
text: v.text,
commentLanguage: v.comment_language,
createTime: v.create_time,
likeCount: v.digg_count,
isAuthorLiked: v.is_author_digged,
isCommentTranslatable: v.is_comment_translatable,
replyCommentTotal: v.reply_comment_total,
user: {
uid: v.user.uid,
avatarThumb: v.user.avatar_thumb.url_list,
nickname: v.user.nickname,
username: v.user.unique_id,
isVerified: v.user.custom_verify !== ""
},
url: v.share_info?.url || "",
replyComment: []
};
if (v.reply_comment !== null) {
v.reply_comment.forEach((v) => {
comment.replyComment.push({
cid: v.cid,
text: v.text,
commentLanguage: v.comment_language,
createTime: v.create_time,
likeCount: v.digg_count,
isAuthorLiked: v.is_author_digged,
isCommentTranslatable: v.is_comment_translatable,
replyCommentTotal: v.reply_comment_total,
user: {
uid: v.user.uid,
avatarThumb: v.user.avatar_thumb.url_list,
nickname: v.user.nickname,
username: v.user.unique_id,
isVerified: v.user.custom_verify !== ""
},
url: v.share_info?.url || "",
replyComment: []
});
total++;
});
}
total++;
comments.push(comment);
});
}
if (commentLimit && comments.length >= commentLimit) {
hasMore = false;
break;
}
}
const response = commentLimit ? comments.slice(0, commentLimit) : comments;
return {
total: response.length,
comments: response
};
};