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

Commit

Permalink
Add inline test, fix async and inline downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
anatolinicolae committed Jul 28, 2020
1 parent a64bc94 commit a538aa3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 33 deletions.
57 changes: 29 additions & 28 deletions handler/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ const handleChatMessage = async (ctx) => {
return ctx.reply(ctx.i18n.t("errors.whitelist"));
}

let urls = getUrls(ctx.message.text);
let urls = Array.from(getUrls(ctx.message.text));

urls.forEach((url) => {
if (url.match(shortLinkRegex)) {
ctx.replyWithChatAction("upload_video");
replyWithVideo(ctx, url);
}
});
return await Promise.all(
urls.map(async (url) => {
if (url.match(shortLinkRegex)) {
await ctx.replyWithChatAction("upload_video");
return await replyWithVideo(ctx, url);
}

return false;
})
);
};

const handleInlineMessage = async (ctx) => {
Expand All @@ -48,24 +52,24 @@ const handleInlineMessage = async (ctx) => {
let data = await fetchTikTok(url);
if (!data) return;

let info = data.props?.pageProps?.videoData?.itemInfos;
let cover = info?.covers[0];
let meta = info?.video?.videoMeta;
let video = await fetchVideoMeta(data);
if (!video) return false

return {
type: "video",
id: data.props?.pageProps?.key,
video_url: getVideoUrl(data),
id: video.id,
video_url: video.videoUrlNoWaterMark,
mime_type: "video/mp4",
thumb_url: cover,
title: `via @${config.telegram.username}`,
video_width: meta?.width,
video_height: meta?.height,
video_duration: meta?.duration,
thumb_url: video.imageUrl,
title: `${video.text} via @${config.telegram.username}`,
video_width: video.videoMeta?.width,
video_height: video.videoMeta?.height,
video_duration: video.videoMeta?.duration,
};
}
})
);

if (!results) return;

try {
Expand All @@ -79,16 +83,16 @@ const replyWithVideo = async (ctx, url) => {
let data = await fetchTikTok(url);
if (!data) return ctx.reply(ctx.i18n.t("errors.http"));

let video = await getVideoUrl(data);
let video = await fetchVideoMeta(data);
if (!video) return ctx.reply(ctx.i18n.t("errors.stream"));

try {
return ctx.replyWithVideo(
{source: got.stream(video)},
return await ctx.replyWithVideo(
{source: got.stream(video.videoUrlNoWaterMark)},
{reply_to_message_id: ctx.message.message_id}
);
} catch (e) {
return ctx.reply(video);
return ctx.reply(video.videoUrlNoWaterMark);
}
};

Expand All @@ -103,13 +107,10 @@ const fetchTikTok = async (url) => {
}
};

const getVideoUrl = async (location) => {
try {
const videoMeta = await TikTokScraper.getVideoMeta(location);
return videoMeta?.videoUrlNoWaterMark;
} catch (error) {
return null;
}
const fetchVideoMeta = async (url) => {
return await TikTokScraper.getVideoMeta(url, {
hdVideo: true,
});
};

module.exports = {
Expand Down
39 changes: 34 additions & 5 deletions test/001-fetch-tiktok-page.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
const {shortLink, chatMessage} = require('../handler/fetch');
const {
chatMessage,
inlineMessage,
shortLink
} = require('../handler/fetch');

describe('Fetch TikTok page', () => {
describe('Check regex', () => {
it('Should return true if equal', () => {
return /https??:\/\/(vm\.)??tiktok\.com\/(\w|\W|\d)+/ === shortLink;
});
});

it('Should return true if equal', async () => {
describe('Fetch TikTok video from chat message', () => {
it('Should return Promise', async () => {
const ctx = {
message: {
from: {
Expand All @@ -14,10 +20,10 @@ describe('Fetch TikTok page', () => {
text: 'https://vm.tiktok.com/JY4YFyv/'
},
replyWithChatAction: e => {
console.log(e)

},
replyWithVideo: e => {
console.log(e)

},
reply: e => {
console.log(e)
Expand All @@ -29,3 +35,26 @@ describe('Fetch TikTok page', () => {
return await chatMessage(ctx);
});
});

describe('Fetch TikTok video from inline query', () => {
it('Should return Promise', async () => {
const ctx = {
inlineQuery: {
from: {
username: 'implode'
},
query: 'https://vm.tiktok.com/JY4YFyv/'
},
answerInlineQuery: e => {
console.log(e)
},
reply: e => {
console.log(e)
},
i18n: {
t: s => s
}
};
return await inlineMessage(ctx);
});
});

0 comments on commit a538aa3

Please sign in to comment.