Compare commits
No commits in common. "a865c192bfe80c017d5e1db1dd5103b8d2abb564" and "3bf02993f6b050affc3fc55ebf071b23a8835ba8" have entirely different histories.
a865c192bf
...
3bf02993f6
3
src/.gitignore → .gitignore
vendored
3
src/.gitignore → .gitignore
vendored
@ -129,5 +129,4 @@ dist
|
|||||||
.yarn/build-state.yml
|
.yarn/build-state.yml
|
||||||
.yarn/install-state.gz
|
.yarn/install-state.gz
|
||||||
.pnp.*
|
.pnp.*
|
||||||
# cookies
|
|
||||||
cookies.txt
|
|
||||||
@ -1 +0,0 @@
|
|||||||
|
|
||||||
244
src/bot.js
244
src/bot.js
@ -1,244 +0,0 @@
|
|||||||
const TelegramBot = require("node-telegram-bot-api");
|
|
||||||
const fs = require("fs");
|
|
||||||
const DownloadVideo = require("./download");
|
|
||||||
const https = require("https");
|
|
||||||
|
|
||||||
async function getImage(url) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
https
|
|
||||||
.get(url, (res) => {
|
|
||||||
if (res.statusCode !== 200) {
|
|
||||||
reject(new Error(`HTTP ${res.statusCode} при загрузке изображения`));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const chunks = [];
|
|
||||||
res.on("data", (chunk) => chunks.push(chunk));
|
|
||||||
res.on("end", () => resolve(Buffer.concat(chunks)));
|
|
||||||
})
|
|
||||||
.on("error", reject);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const config = {
|
|
||||||
token: process.env.BOT_TOKEN,
|
|
||||||
admins: process.env.ADMIN_LIST?.split(/\s*,\s*/g).map((x) => +x) ?? [],
|
|
||||||
};
|
|
||||||
config.whitelist = [].concat(
|
|
||||||
process.env.WHITELIST?.split(/\s*,\s*/g).map((x) => +x) ?? [],
|
|
||||||
config.admins,
|
|
||||||
);
|
|
||||||
config.whitelist = Object.keys(
|
|
||||||
Object.fromEntries(config.whitelist.map((x) => [x, true])),
|
|
||||||
).map((x) => +x);
|
|
||||||
|
|
||||||
console.info(config);
|
|
||||||
|
|
||||||
config.admins = Object.fromEntries(config.admins.map((x) => [x, true]));
|
|
||||||
config.whitelist = Object.fromEntries(config.whitelist.map((x) => [x, true]));
|
|
||||||
|
|
||||||
const ytLinkRegexp = /^https:\/\/www\.youtube\.com/m;
|
|
||||||
|
|
||||||
class TgBotSender {
|
|
||||||
constructor(parent, bot, msg, memory = {}) {
|
|
||||||
this.parent = parent;
|
|
||||||
this.bot = bot;
|
|
||||||
this.msg = msg;
|
|
||||||
|
|
||||||
this.memory = memory;
|
|
||||||
}
|
|
||||||
|
|
||||||
async textWithMedia(data, reply_markup) {
|
|
||||||
await this.bot.editMessageMedia(data, {
|
|
||||||
chat_id: this.msg.chat.id,
|
|
||||||
message_id: this.msg.message_id,
|
|
||||||
reply_markup,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async text(text, reply_markup) {
|
|
||||||
await this.bot.editMessageText(text, {
|
|
||||||
chat_id: this.msg.chat.id,
|
|
||||||
message_id: this.msg.message_id,
|
|
||||||
reply_markup,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async download(video, quality = DownloadVideo.qualities.mp4_best) {
|
|
||||||
const { msg } = this.memory;
|
|
||||||
|
|
||||||
const sent = await this.parent.send(msg, "Start download");
|
|
||||||
const info = await video.getVideoInfo();
|
|
||||||
await video.setupQuality(quality, false);
|
|
||||||
let cooldown = new Date();
|
|
||||||
video
|
|
||||||
.download(({ percent }) => {
|
|
||||||
const now = new Date();
|
|
||||||
if (now - cooldown <= 1500) return;
|
|
||||||
cooldown = new Date();
|
|
||||||
sent.text(`Downloaded (${percent}%)`);
|
|
||||||
})
|
|
||||||
.then((downloaded) => {
|
|
||||||
sent.text("Download complete");
|
|
||||||
if (downloaded.format.type === "video") {
|
|
||||||
this.bot.sendVideo(
|
|
||||||
msg.chat.id,
|
|
||||||
downloaded.data,
|
|
||||||
{ caption: info.title },
|
|
||||||
{
|
|
||||||
filename: `downloaded.${downloaded.format.exc}`,
|
|
||||||
contentType: downloaded.format.mime,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
} else if (downloaded.format.type === "audio") {
|
|
||||||
this.bot.sendAudio(
|
|
||||||
msg.chat.id,
|
|
||||||
downloaded.data,
|
|
||||||
{ caption: info.title },
|
|
||||||
{
|
|
||||||
filename: `downloaded.${downloaded.format.exc}`,
|
|
||||||
contentType: downloaded.format.mime,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
//console.error(err.stack);
|
|
||||||
this.bot.sendMessage(msg.chat.id, "Download failed");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const actions = new Object();
|
|
||||||
class TgBot {
|
|
||||||
constructor() {
|
|
||||||
this.bot = new TelegramBot(config.token, {
|
|
||||||
polling: true,
|
|
||||||
});
|
|
||||||
this.bot.on("text", (m) => this.newMessageHandler(m));
|
|
||||||
this.bot.on("callback_query", (m) => this.cbHandler(m));
|
|
||||||
}
|
|
||||||
|
|
||||||
async cbHandler(query) {
|
|
||||||
const key = query.from.id + ":" + query.data;
|
|
||||||
//console.debug(query, actions[key], key);
|
|
||||||
actions[key]?.();
|
|
||||||
}
|
|
||||||
|
|
||||||
async send(msg, txt) {
|
|
||||||
const result = await this.bot.sendMessage(msg.chat.id, txt);
|
|
||||||
return new TgBotSender(this, this.bot, result, { msg });
|
|
||||||
}
|
|
||||||
|
|
||||||
async newMessageHandler(msg) {
|
|
||||||
if (msg.chat.id === msg.from.id) {
|
|
||||||
if (config.whitelist[msg.from.id]) {
|
|
||||||
if (ytLinkRegexp.test(msg.text)) {
|
|
||||||
const sent = await this.send(msg, "Wait..");
|
|
||||||
const errHandler = (err) => {
|
|
||||||
//console.error(err.stack);
|
|
||||||
sent.text("Error. Invalid/Hidden video or Forbidden for download");
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
const video = new DownloadVideo(msg.text);
|
|
||||||
const info = await video.getVideoInfo();
|
|
||||||
//console.debug(info);
|
|
||||||
// Download only video with best quality
|
|
||||||
const qualities = await video.qualities();
|
|
||||||
//console.debug(qualities);
|
|
||||||
let keyboard = [];
|
|
||||||
let keyboardM = [];
|
|
||||||
let kbChunk = [];
|
|
||||||
let kbChunkM = [];
|
|
||||||
|
|
||||||
for (let quality in qualities) {
|
|
||||||
const qualityItem = qualities[quality];
|
|
||||||
if (qualityItem.type !== null) {
|
|
||||||
if (qualityItem.type === "audio")
|
|
||||||
kbChunkM.push({
|
|
||||||
text: `🎧 ${qualityItem.exc}/${qualityItem.resolution}`,
|
|
||||||
callback_data: quality + "_" + msg.message_id,
|
|
||||||
});
|
|
||||||
else if (qualityItem.type === "video")
|
|
||||||
kbChunk.push({
|
|
||||||
text: `📹 ${qualityItem.exc}/${qualityItem.resolution}`,
|
|
||||||
callback_data: quality + "_" + msg.message_id,
|
|
||||||
});
|
|
||||||
if (kbChunk.length >= 3) {
|
|
||||||
keyboard.push(kbChunk);
|
|
||||||
kbChunk = [];
|
|
||||||
}
|
|
||||||
if (kbChunkM.length >= 3) {
|
|
||||||
keyboardM.push(kbChunkM);
|
|
||||||
kbChunkM = [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (kbChunk.length > 0) keyboard.push(kbChunk);
|
|
||||||
if (kbChunkM.length > 0) keyboardM.push(kbChunkM);
|
|
||||||
keyboard = keyboard.concat(keyboardM);
|
|
||||||
//keyboard = keyboard.slice(-4);
|
|
||||||
//console.debug(keyboard);
|
|
||||||
//console.debug(info.title, info.thumbnail);
|
|
||||||
const thenHandler = () => {
|
|
||||||
for (let quality in qualities) {
|
|
||||||
const qualityItem = qualities[quality];
|
|
||||||
const actKey =
|
|
||||||
msg.from.id + ":" + quality + "_" + msg.message_id;
|
|
||||||
actions[actKey] = () => {
|
|
||||||
sent
|
|
||||||
.download(video, qualityItem)
|
|
||||||
.then(() => {})
|
|
||||||
.catch(errHandler);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return Promise.resolve(null);
|
|
||||||
};
|
|
||||||
await sent
|
|
||||||
.textWithMedia(
|
|
||||||
{
|
|
||||||
type: "photo",
|
|
||||||
media: info.thumbnail,
|
|
||||||
//media: { source: await getImage(info.thumbnail) },
|
|
||||||
//media: "https://placekitten.com/500/350",
|
|
||||||
caption: info.title,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
inline_keyboard: keyboard,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.then(thenHandler)
|
|
||||||
.catch(() => {
|
|
||||||
sent
|
|
||||||
.textWithMedia(
|
|
||||||
{
|
|
||||||
type: "photo",
|
|
||||||
//media: info.thumbnail,
|
|
||||||
media: "https://placekitten.com/500/350",
|
|
||||||
caption: info.title,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
inline_keyboard: keyboard,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.then(thenHandler)
|
|
||||||
.catch(errHandler);
|
|
||||||
});
|
|
||||||
|
|
||||||
//this.download(msg, video);
|
|
||||||
//this.send(msg, "Start download");
|
|
||||||
} catch (e) {
|
|
||||||
errHandler(e);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
this.send(msg, "Invalid youtube link");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
this.send(msg, "Permission denied");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const bot = new TgBot();
|
|
||||||
180
src/download.js
180
src/download.js
@ -1,180 +0,0 @@
|
|||||||
const { spawn } = require("child_process");
|
|
||||||
const mime = require("mime");
|
|
||||||
const path = require("path");
|
|
||||||
|
|
||||||
const formatsCache = new Map();
|
|
||||||
const infoCache = new Map();
|
|
||||||
class DownloadVideo {
|
|
||||||
static get qualities () {
|
|
||||||
const q = {
|
|
||||||
webm_best: { exc: "webm", flag: "best", mime: "video/webm" },
|
|
||||||
mp4_best: { exc: "mp4", flag: "best[ext=mp4]", mime: "video/mp4" },
|
|
||||||
};
|
|
||||||
q.default = q.webm_best;
|
|
||||||
|
|
||||||
return q;
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(url, quality) {
|
|
||||||
const CLASS = this.__proto__.constructor;
|
|
||||||
|
|
||||||
this.url = url;
|
|
||||||
this.quality = quality;
|
|
||||||
if (!this.quality) this.quality = CLASS.qualities.default;
|
|
||||||
}
|
|
||||||
|
|
||||||
async getVideoInfo() {
|
|
||||||
const fromCache = infoCache.get(this.url);
|
|
||||||
if (fromCache) return fromCache;
|
|
||||||
|
|
||||||
const result = await new Promise((resolve, reject) => {
|
|
||||||
const child = spawn("yt-dlp", ["--cookies", "cookies.txt", "-j", this.url]);
|
|
||||||
|
|
||||||
let data = "";
|
|
||||||
|
|
||||||
child.stdout.on("data", chunk => {
|
|
||||||
data += chunk.toString();
|
|
||||||
});
|
|
||||||
|
|
||||||
child.stderr.on("data", err => {
|
|
||||||
console.error("yt-dlp error:", err.toString());
|
|
||||||
});
|
|
||||||
|
|
||||||
child.on("close", code => {
|
|
||||||
if (code !== 0) return reject(new Error("yt-dlp failed to get info"));
|
|
||||||
|
|
||||||
try {
|
|
||||||
const info = JSON.parse(data);
|
|
||||||
|
|
||||||
resolve({
|
|
||||||
id: info.id,
|
|
||||||
title: info.title,
|
|
||||||
description: info.description,
|
|
||||||
uploader: info.uploader,
|
|
||||||
duration: info.duration, // секунды
|
|
||||||
thumbnail:
|
|
||||||
info.thumbnails?.[info.thumbnails.length - 1]?.url ||
|
|
||||||
info.thumbnail, // лучшее превью
|
|
||||||
url: info.webpage_url,
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
reject(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
infoCache.set(this.url, result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
async getFormats() {
|
|
||||||
const fromCache = formatsCache.get(this.url);
|
|
||||||
if (fromCache) return fromCache;
|
|
||||||
|
|
||||||
const result = await new Promise((resolve, reject) => {
|
|
||||||
const child = spawn("yt-dlp", ["--cookies", "cookies.txt", "-j", this.url]);
|
|
||||||
let data = "";
|
|
||||||
|
|
||||||
child.stdout.on("data", (chunk) => (data += chunk.toString()));
|
|
||||||
child.on("close", (code) => {
|
|
||||||
if (code !== 0) return reject(new Error("yt-dlp failed"));
|
|
||||||
const info = JSON.parse(data);
|
|
||||||
resolve(info.formats);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
formatsCache.set(this.url, result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
async qualities() {
|
|
||||||
const formats = await this.getFormats();
|
|
||||||
const q = {};
|
|
||||||
|
|
||||||
// Перебираем форматы и группируем по расширению
|
|
||||||
formats.forEach(f => {
|
|
||||||
if (!f.ext || !f.format_id) return;
|
|
||||||
//if (!q[f.ext]) q[f.ext] = [];
|
|
||||||
|
|
||||||
const resolution = f.height ? `${f.height}p` : 'audio-only';
|
|
||||||
|
|
||||||
let flag = `bestaudio[ext=${f.ext}]`;
|
|
||||||
if (f.height) {
|
|
||||||
flag = `best[height<=${f.height}][ext=${f.ext}]`;
|
|
||||||
}
|
|
||||||
//const flag = f.format_id;
|
|
||||||
|
|
||||||
const mimeType = mime.getType(f.ext);
|
|
||||||
const type = (resolution === "audio-only" || /^audio\//m.test(mimeType)) ? "audio" : (
|
|
||||||
/^video\//m.test(mimeType) ? "video" : null
|
|
||||||
);
|
|
||||||
q[f.ext + "_" + resolution] = {
|
|
||||||
format_id: f.format_id,
|
|
||||||
flag, // <- ключевой момент для yt-dlp
|
|
||||||
exc: f.ext, // <- расширение
|
|
||||||
resolution,
|
|
||||||
filesize: f.filesize || null,
|
|
||||||
mime: mimeType, type: !mimeType ? null : type
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
return q;
|
|
||||||
}
|
|
||||||
|
|
||||||
async setupQuality (quality, check=true) {
|
|
||||||
if (!check) {
|
|
||||||
this.quality = quality;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
const CLASS = this.__proto__.constructor;
|
|
||||||
|
|
||||||
const all = Object.assign({}, (await this.qualities()), CLASS.qualities);
|
|
||||||
if (all[quality.exc + "_" + quality.resolution]) {
|
|
||||||
this.quality = quality;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
async download(cb) {
|
|
||||||
const data = await new Promise((resolve, reject) => {
|
|
||||||
const params = [
|
|
||||||
"--cookies", "cookies.txt",
|
|
||||||
"-f", this.quality.flag,
|
|
||||||
"-o", "-", this.url,
|
|
||||||
];
|
|
||||||
const child = spawn("yt-dlp", params);
|
|
||||||
|
|
||||||
const chunks = [];
|
|
||||||
let downloaded = 0;
|
|
||||||
|
|
||||||
child.stdout.on("data", (chunk) => {
|
|
||||||
chunks.push(chunk);
|
|
||||||
downloaded += chunk.length;
|
|
||||||
//if (cb) cb({ downloaded });
|
|
||||||
});
|
|
||||||
|
|
||||||
child.stderr.on("data", (data) => {
|
|
||||||
const str = data.toString();
|
|
||||||
const match = str.match(/(\d+\.\d+)%/);
|
|
||||||
if (match && cb) {
|
|
||||||
cb({ percent: parseFloat(match[1]), downloaded });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
child.on("close", (code) => {
|
|
||||||
if (code === 0) {
|
|
||||||
resolve(Buffer.concat(chunks));
|
|
||||||
} else {
|
|
||||||
reject(new Error(`yt-dlp exited with code ${code}`));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = {
|
|
||||||
format: this.quality,
|
|
||||||
data,
|
|
||||||
};
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = DownloadVideo;
|
|
||||||
2216
src/package-lock.json
generated
2216
src/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +0,0 @@
|
|||||||
{
|
|
||||||
"dependencies": {
|
|
||||||
"mime": "^3.0.0",
|
|
||||||
"node-telegram-bot-api": "^0.66.0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user