Skip to content

Commit

Permalink
yt-dlp for tiktok
Browse files Browse the repository at this point in the history
  • Loading branch information
MasedMSD committed Nov 2, 2024
1 parent 71f9d1e commit 128a5d6
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"pg": "^8.12.0",
"sagiri": "^4.2.0",
"telegra.ph": "^1.0.1",
"tslib": "^2.6.2"
"tslib": "^2.6.2",
"yt-dlp-wrap": "^2.3.12"
},
"devDependencies": {
"@types/bun": "^1.1.11",
Expand Down
1 change: 1 addition & 0 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from "./githubLink.composer";
export * from "./msdincorporated.composer";
export * from "./randomEmoji.compose";
export * from "./telegraph.composer";
export * from "./ytdl.composer";

export * from "./eval.command";
export * from "./exec.command";
Expand Down
61 changes: 61 additions & 0 deletions src/handlers/ytdl.composer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { createWriteStream, unlink } from "fs";
import { Composer, InputFile } from "grammy";
import { tmpdir } from "os";
import { join } from "path";
import YTDlpWrap from "yt-dlp-wrap";

export const YTDLComposer = new Composer();
const ytDlpWrap = new YTDlpWrap();

YTDLComposer.hears(
/https?:\/\/(?:www\.tiktok\.com\/(?:embed\/|@[\w.-]+?\/video\/)|(?:vm|vt)\.tiktok\.com\/|www\.tiktok\.com\/t\/)([\w\d]+)/i,
async ctx => {
// if (ctx.chatId !== -1001705068191) return;

const url = ctx.match[0];
const metadata = await ytDlpWrap.getVideoInfo(url);

console.log(metadata);

const formattedMetadata = metadata
? [
`Название: <code>${metadata.title}</code>\n`,
`Количество просмотров: <code>${metadata.view_count}</code>`,
`Количество лайков: <code>${metadata.like_count}</code>`,
`Количество комментариев: <code>${metadata.comment_count}</code>`,
`Количество репостов: <code>${metadata.repost_count}</code>`,
].join("\n")
: "Метаданные видео недоступны";

const stream = ytDlpWrap.execStream([url]);
const tempFilePath = join(tmpdir(), `download-${Date.now()}.mp4`);
const fileStream = createWriteStream(tempFilePath);

stream.pipe(fileStream);

fileStream.on("finish", async () => {
await ctx.replyWithVideo(new InputFile(tempFilePath), {
caption: formattedMetadata,
parse_mode: "HTML",
reply_markup: {
inline_keyboard: [
[
{
text: "Автор",
url: metadata.uploader_url,
},
{
text: "Ссылка на видео",
url,
},
],
],
},
});

unlink(tempFilePath, err => {
if (err) console.error(`Не удалось удалить файл ${tempFilePath}`, err);
});
});
}
);
2 changes: 2 additions & 0 deletions src/structures/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
randomEmojiComposer,
startCommand,
telegraphComposer,
YTDLComposer,
} from "../handlers";
import { Database } from "./database";

Expand Down Expand Up @@ -62,6 +63,7 @@ export class Client {
this.bot.use(msdIncorporatedComposer);
this.bot.use(randomEmojiComposer);
this.bot.use(telegraphComposer);
this.bot.use(YTDLComposer);

this.bot.command("eval", evalCommand);
this.bot.command("exec", execCommand);
Expand Down

0 comments on commit 128a5d6

Please sign in to comment.