Skip to content

Commit

Permalink
optimize audio sending to telegram by providing url, not buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
upisfree committed Sep 19, 2018
1 parent 7cf66d6 commit 9943416
Showing 1 changed file with 9 additions and 30 deletions.
39 changes: 9 additions & 30 deletions src/telegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,15 @@ namespace Telegram {

public sendUrlsToChannel(urls: VK.Audio[]): void {
for (let i = 0; i < urls.length; i++) {
let file = fs.createWriteStream('tmp/' + i + '.mp3');
let get = https.get(urls[i].url, (res) => {
res.headers['Content-Type'] = 'multipart/form-data';
res.headers['Content-Disposition'] = `name*=UTF-8''${ encodeURIComponent(urls[i].artist + ' — ' + urls[i].title) }`;

res.pipe(file);

// console.log(res.headers)

// TODO: указывать прогресс загрузки. Пока есть контроль только над скачиванием файла к себе,
// над загрузкой в телегу контроля нет (его нужно вынести из библиотеки).
// let fullLength = parseInt(res.headers['content-length'] || '');
// let received = 0;

// res.on('data', (d) => {
// received += d.length;

// console.log(Math.round(received / fullLength * 100) + '%');
// });

let fileOptions: TelegramBot.SendAudioOptions = {
performer: urls[i].artist,
title: urls[i].title,
};

this.botInstance.sendAudio(CONFIG.TELEGRAM.GROUP_URL, res, fileOptions).then((m) => {
log('[tg]', `send audio "${ urls[i].artist } — ${ urls[i].title }"`);
}, (e) => {
log('[tg]', 'sendUrlsToChannel() failed:', e);
});
let fileOptions: TelegramBot.SendAudioOptions = {
performer: urls[i].artist,
title: urls[i].title
};

this.botInstance.sendAudio(CONFIG.TELEGRAM.GROUP_URL, urls[i].url, fileOptions).then((m) => {
log('[tg]', `send audio "${ urls[i].artist } — ${ urls[i].title }"`);
}, (e) => {
log('[tg]', 'sendUrlsToChannel() failed:', e);
});
};
}
Expand Down

0 comments on commit 9943416

Please sign in to comment.