Compare commits

..

No commits in common. "056ea7d02f95215662cd13f310ae260c04e1e508" and "5b2911feaf3828467a1a3d023dc9a122d623437a" have entirely different histories.

4 changed files with 44 additions and 109 deletions

View File

@ -50,19 +50,6 @@ try {
const ytLinkRegexp = /^https:\/\/www\.youtube\.com/m; const ytLinkRegexp = /^https:\/\/www\.youtube\.com/m;
async function getFileSize(pathname) {
const stream = fs.createReadStream(pathname);
let size = 0;
return await new Promise((resolve, reject) => {
stream.on("data", data => size += data.length);
stream.on("end", () => resolve(size));
stream.on("error", reject);
});
}
// Check if download process
const tgChatsDownloaded = new Object();
class TgBotSender { class TgBotSender {
constructor(parent, bot, msg, memory = {}) { constructor(parent, bot, msg, memory = {}) {
this.parent = parent; this.parent = parent;
@ -103,25 +90,15 @@ class TgBotSender {
const filename = `${(new Date()).getTime()}.${quality.exc}`; const filename = `${(new Date()).getTime()}.${quality.exc}`;
const isStream = path.join(__dirname, `tmp/${filename}`); const isStream = path.join(__dirname, `tmp/${filename}`);
let uploadFileInterval = null;
const finallyHandler = (result) => { const finallyHandler = (result) => {
clearInterval(uploadFileInterval); if (isStream) {
//delete tgChatsDownloaded[msg.chat.id];
if (isStream && isStream !== true) {
fs.rm(isStream, { recursive: true, force: true }, () => {}); fs.rm(isStream, { recursive: true, force: true }, () => {});
if (result) if (result)
fs.rm(result, { recursive: true, force: true }, () => {}); fs.rm(result, { recursive: true, force: true }, () => {});
} }
}; };
const errHandler = (err) => { video
console.error(err.stack);
//this.bot.sendMessage(msg.chat.id, "Download failed");
sent.text("Download failed");
finallyHandler();
}
await video
.download(({ percent, downloaded, fine, msg }) => { .download(({ percent, downloaded, fine, msg }) => {
//console.debug({ percent, fine, downloaded, msg }); //console.debug({ percent, fine, downloaded, msg });
const now = new Date(); const now = new Date();
@ -134,47 +111,41 @@ class TgBotSender {
}, isStream) }, isStream)
.then(async (downloaded) => { .then(async (downloaded) => {
console.debug("downloaded:", downloaded); console.debug("downloaded:", downloaded);
try { if (downloaded.format.type === "video") {
if (downloaded.format.type === "video") { await this.bot.sendChatAction(msg.chat.id, 'upload_document');
const filesize = typeof isStream !== "string" ? 0 : await getFileSize(downloaded.data); const uploadFileInterval = setInterval(() => {
//console.debug("filesize:", filesize); this.bot.sendChatAction(msg.chat.id, 'upload_document');
if (filesize < 50 * 1024 * 1024) { }, 4250);
await this.bot.sendChatAction(msg.chat.id, 'upload_document'); await new Promise(r => setTimeout(r, 3000));
uploadFileInterval = setInterval(() => { await this.bot.sendVideo(
this.bot.sendChatAction(msg.chat.id, 'upload_document'); msg.chat.id,
}, 4250); downloaded.data,
await new Promise(r => setTimeout(r, 3000)); { caption: info.title },
await this.bot.sendVideo( {
msg.chat.id, filename,
downloaded.data, contentType: isStream === true ? "application/octet-stream" : downloaded.format.mime,
{ caption: info.title }, //contentType: downloaded.format.mime,
{ },
filename, );
contentType: isStream === true ? "application/octet-stream" : downloaded.format.mime, clearInterval(uploadFileInterval);
//contentType: downloaded.format.mime, } else if (downloaded.format.type === "audio") {
}, await this.bot.sendAudio(
); msg.chat.id,
} else { downloaded.data,
return await sent.text(`Maximum filesize reached. Try any types (Current file: ${ { caption: info.title },
((filesize / (1024 * 1024))+"").split(".")[0] {
} MB)`); filename: `downloaded.${downloaded.format.exc}`,
} contentType: downloaded.format.mime,
} else if (downloaded.format.type === "audio") { },
await this.bot.sendAudio( );
msg.chat.id,
downloaded.data,
{ caption: info.title },
{
filename: `downloaded.${downloaded.format.exc}`,
contentType: downloaded.format.mime,
},
);
}
} catch (err) {
return errHandler(err);
} }
finallyHandler(downloaded.data); finallyHandler(downloaded.data);
}).catch(errHandler); })
.catch((err) => {
console.error(err.stack);
this.bot.sendMessage(msg.chat.id, "Download failed");
finallyHandler();
});
} }
} }
@ -231,12 +202,7 @@ class TgBot {
async cbHandler(query) { async cbHandler(query) {
const key = query.from.id + ":" + query.data; const key = query.from.id + ":" + query.data;
//console.debug(query, this.videosGC.actions[key], key); //console.debug(query, this.videosGC.actions[key], key);
if (!this.videosGC.actions[key]) this.videosGC.actions[key]?.();
return this.bot.answerCallbackQuery(query.id, {
text: "Resend me video link, please",
show_alert: true
});
this.videosGC.actions[key](query.id);
} }
async send(msg, txt) { async send(msg, txt) {
@ -304,23 +270,12 @@ class TgBot {
const qualityItem = qualities[quality]; const qualityItem = qualities[quality];
const actKey = const actKey =
msg.from.id + ":" + quality + "_" + msg.message_id; msg.from.id + ":" + quality + "_" + msg.message_id;
this.videosGC.register(actKey, async (queryId) => { this.videosGC.register(actKey, () => {
//console.debug("ACT btn:", actKey); //console.debug("ACT btn:", actKey);
if (tgChatsDownloaded[msg.from.id])
return this.bot.answerCallbackQuery(queryId, {
text: 'Wait until download was not finished',
show_alert: true
});
tgChatsDownloaded[msg.from.id] = true;
sent sent
.download(video, qualityItem) .download(video, qualityItem)
.then(() => { .then(() => {})
delete tgChatsDownloaded[msg.from.id]; .catch(errHandler);
})
.catch((err) => {
errHandler(err);
delete tgChatsDownloaded[msg.from.id];
});
}, video); }, video);
/*this.videosGC.actions[actKey] = () => { /*this.videosGC.actions[actKey] = () => {
sent sent

View File

@ -223,8 +223,7 @@ class DownloadVideo {
exc: f.ext, // <- расширение exc: f.ext, // <- расширение
resolution, resolution,
filesize: f.filesize || null, filesize: f.filesize || null,
mime: mimeType, type: !mimeType ? null : type, mime: mimeType, type: !mimeType ? null : type
forceAll: () => f
}; };
}); });
if (withDefaults) { if (withDefaults) {
@ -269,48 +268,29 @@ class DownloadVideo {
[this.quality.flag] : this.quality.flag; [this.quality.flag] : this.quality.flag;
const params = [ const params = [
"--cookies", "cookies.txt", "--cookies", "cookies.txt",
"--user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
//"--extractor-args", "youtube:player-client=default,mweb;po_token=mweb.gvs+XXX",
"--extractor-args", "youtube:player_client=default,mweb",
"-f", ...flags, "-f", ...flags,
"-o", !isPath ? "-" : isStream, this.url, "-o", !isPath ? "-" : isStream, this.url,
]; ];
d("yt-dlp " + params.join(" ")); d("yt-dlp " + params.join(" "));
//console.debug({ HOME: process.env.HOME }); const child = spawn("yt-dlp", params);
const child = spawn("yt-dlp", params, {
env: {
...process.env,
//HOME: process.env.HOME,
LANG: "en_US.UTF-8",
},
stdio: ["pipe","pipe","pipe"],
//stdio: ["pipe","inherit","pipe"],
//stdio: ["inherit", "inherit", "inherit"],
});
let downloaded = 0; let downloaded = 0;
let percent = 0; let percent = 0;
let sizeTg = 0; let sizeTg = 0;
let fine = false; let fine = false;
const getDwnld = () => `Real size: ${downloaded}, total: ${sizeTg}`; const getDwnld = () => `Real size: ${downloaded}, total: ${sizeTg}`;
const stderrHandler = (data) => { child.stderr.on("data", (data) => {
const str = data.toString(); const str = data.toString();
//console.debug("DEBUG >>>>>>", str);
//console.debug(str); //console.debug(str);
const match = str.match(/(\d+\.\d+)%/); const match = str.match(/(\d+\.\d+)%/);
if (match && cb) { if (match && cb) {
percent = parseFloat(match[1]); percent = parseFloat(match[1]);
cb({ percent, downloaded: !isPath ? getDwnld() : "Processing", fine }); cb({ percent, downloaded: !isPath ? getDwnld() : "Processing", fine });
} }
}; });
child.stderr.on("data", stderrHandler);
if (isPath) { if (isPath) {
child.stdout.on("data", stderrHandler);
child.on("close", (code) => { child.on("close", (code) => {
//console.debug(">>>>>>>>>>>>>FINISHED");
signal.emit("finish"); signal.emit("finish");
fine = true; fine = true;
if (code !== 0) { if (code !== 0) {

Binary file not shown.