Skip to content
This repository has been archived by the owner on Jan 11, 2022. It is now read-only.

Commit

Permalink
Everything should be working fine now!
Browse files Browse the repository at this point in the history
[Fixed] issue #10
[Added] long URL support
[Removed] inline queries due to compatibility issues related to #10

Signed-off-by: Anatoli Nicolae <[email protected]>
  • Loading branch information
anatolinicolae committed Nov 5, 2020
1 parent 60aa80f commit 6347197
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 220 deletions.
7 changes: 4 additions & 3 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ bot.command("help", (ctx) =>

bot.command("lang", lang);

bot.on("inline_query", fetch.inlineMessage);
// Disabled inline queries as TikTok requires specific headers to display a video,
// which we can't control since preview downloads are server-side
// bot.on("inline_query", fetch.inlineMessage);

// TODO: add full link support
// bot.hears(fetch.fullLink, fetch.chatMessage);
bot.hears(fetch.fullLink, fetch.chatMessage);
bot.hears(fetch.shortLink, fetch.chatMessage);

bot.launch();
63 changes: 27 additions & 36 deletions handler/fetch.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const {unlink} = require('fs');
const validUrl = require("valid-url");
const getUrls = require("get-urls");
const got = require("got");
const TikTokScraper = require("tiktok-scraper");
const Extra = require("telegraf/extra");
const Markup = require("telegraf/markup");

// Config import and checks
const config = require("../config");
Expand All @@ -12,17 +10,9 @@ if (!config.http || !config.http.agent) {
process.exit();
}

// TODO: add full link support
// const fullLinkRegex = /https??:\/\/(vm\.)??tiktok\.com\/(\w|\W|\d)+/;
const fullLinkRegex = /https:\/\/www\.tiktok\.com\/@.*\/video\/\d{18,}.*/;
const shortLinkRegex = /https??:\/\/(v[m|t]\.)??tiktok\.com\/(\w|\W|\d)+/;

const gotOptions = {
headers: {
'User-Agent': config.http.agent,
'Referer': 'https://www.tiktok.com/',
},
};

const isWhitelisted = (username) => {
return config.whitelist.length == 0 || config.whitelist.includes(username);
};
Expand Down Expand Up @@ -98,32 +88,34 @@ const handleInlineMessage = async (ctx) => {
};

const replyWithVideo = async (ctx, url) => {
let dest = '/tmp';
let path = null;

try {
let video = await fetchVideoMeta(url);
if (!video) {
return ctx.reply(ctx.i18n.t("errors.stream"));
}
let meta = await TikTokScraper.getVideoMeta(url);

if (!validUrl.isUri(video.videoUrlNoWaterMark)) {
return false;
}
let download = await TikTokScraper.video(url, {
download: true,
filepath: dest,
noWaterMark: true,
});

try {
const response = await got(video.videoUrlNoWaterMark, gotOptions);
if (response.statusCode === 200) {
const source = await got.stream(video.videoUrlNoWaterMark, gotOptions);
return await ctx.replyWithVideo(
{source: source},
{reply_to_message_id: ctx.message.message_id}
);
} else {
return await ctx.reply(ctx.i18n.t("errors.stream"));
}
} catch (e) {
return await ctx.reply(ctx.i18n.t("errors.stream"));
}
} catch (e) {
path = `${dest}/${meta.id}.mp4`;

return await ctx.replyWithVideo(
{source: path},
{reply_to_message_id: ctx.message.message_id}
);
} catch (err) {
return ctx.reply(ctx.i18n.t("errors.stream"));
} finally {
if (path) {
unlink(path, (err) => {
if (err) {
console.error(err);
}
});
}
}
};

Expand All @@ -145,8 +137,7 @@ const fetchVideoMeta = async (url) => {
};

module.exports = {
// TODO: add full link support
// fullLink: fullLinkRegex,
fullLink: fullLinkRegex,
shortLink: shortLinkRegex,
chatMessage: handleChatMessage,
inlineMessage: handleInlineMessage,
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{
"name": "tikvidbot",
"version": "1.3.3",
"version": "1.3.7",
"description": "TikTok Video Download Telegram Bot",
"main": "bot.js",
"author": "thundersquared (https://thundersquared.com)",
"license": "MIT",
"dependencies": {
"get-urls": "^10.0.0",
"got": "^11.8.0",
"telegraf": "^3.38.0",
"telegraf-command-parts": "^1.0.3",
"telegraf-i18n": "^6.6.0",
Expand Down
Loading

0 comments on commit 6347197

Please sign in to comment.