Skip to content

Commit

Permalink
📥 Feat: Download progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotiaWang committed Apr 2, 2022
1 parent 3687904 commit ab0c6eb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/handler/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,30 @@ export async function transfer(msg) {

// The last time when the message is edited, in UNIX timestamp format
let lastEditTime = Date.now();

let lastDownloadSize = 0;
try {
await bot.downloadMedia(msg, {
outputFile: filePath,
progressCallback: (downloaded, total) => {
// Limit the edit time interval to 2000 ms
if (Date.now() - lastEditTime > 2000) {
if (downloaded && Date.now() - lastEditTime > 3000) {
// Convert to MB
downloaded = (downloaded / 1000 / 1000).toFixed(2);
total = (total / 1000 / 1000).toFixed(2);
let speed = ((downloaded - lastDownloadSize) / 3).toFixed(2);
let text = strings[lang]["downloadProgress"]
.replace('{1}', total)
.replace('{2}', downloaded)
.replace('{3}', speed)
.replace('{4}', secToTime(Math.round((total - downloaded) / speed)));
let percent = Math.round(downloaded / total * 100);
text += '\n\n<code>[' + '●'.repeat(percent / 5.5) + '○'.repeat(18 - percent / 5.5) + ']</code>';
lastEditTime = Date.now();
lastDownloadSize = downloaded;
bot.editMessage(chat, {
message: editMsg.id,
text: strings[lang]["downloading"] + '...' + (downloaded / total * 100).toFixed(1) + '%'
text: text,
parseMode: 'html'
}).catch(() => { });
}
}
Expand Down Expand Up @@ -137,9 +150,16 @@ function randomString(e = 8) {
* @param {string} text Log content
* @param {boolean} alert If true, send the content to bot owner via Telegram
*/
export function log(text, alert = false) {
function log(text, alert = false) {
console.log(new Date().toLocaleString('zh-CN') + ': ' + text);
if (alert) {
bot.sendMessage(ADMIN_ID, { message: text }).catch(() => { });
}
}

function secToTime(sec) {
let hour = Math.floor(sec / 3600);
let min = Math.floor((sec - hour * 3600) / 60);
let secs = sec - hour * 3600 - min * 60;
return `${hour}:${min}:${secs}`;
}
1 change: 1 addition & 0 deletions src/i18n/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "English",
"help": "🐱 <b>Welcome to Catbox Uploader!</b>\n\nThis bot can upload your files to <a href=\"https://catbox.moe\">Catbox</a> or <a href=\"https://litterbox.catbox.moe\">Litterbox</a>, and return the file's share link. \nThe files you uploaded mustn't violate the <a href=\"https://catbox.moe/faq.php\">Catbox ToS</a> and Litterbox ToS.\n\nCommands:\n/help - Send this help info\n/settings - Choose a language, service or file expiration.\n/delete - Delete files uploaded to Catbox.\n/token - Bind your Catbox token. Also visible in settings.\n\nTake a look at my channel @atashare and @atabots for interesting websites and update notes!\nSource code:<a href=\"https://github.com/AnotiaWang/TG_Catbox_Uploader\">GitHub</a>",
"downloading": "Downloading...",
"downloadProgress": "<b>📥 Downloading</b>\n\n<b>Total size: </b>{1} MB\n<b>Downloaded: </b>{2} MB\n<b>Speed: </b>{3} MB/s\n<b>ETA: </b>{4}",
"uploading": "File downloaded successfully. Now uploading to {s}...",
"uploaded": "🗃️ Uploaded Successfully!\n\nService: {1}\nValidity: {2}\nLink: {3}",
"error": "😥 Transfer failed! Maybe the server is overloaded or network isn't stable now, please try later.",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "简体中文",
"help": "🐱 <b>欢迎使用 Catbox Uploader!</b>\n\n本机器人可以将您发送的文件上传到 <a href=\"https://catbox.moe\">Catbox</a> 或 <a href=\"https://litterbox.catbox.moe\">Litterbox</a>,并返回文件的分享链接。\n上传的文件不得违反 <a href=\"https://catbox.moe/faq.php\">Catbox ToS</a> 以及 Litterbox ToS。\n\n命令列表:\n/help - 显示帮助信息\n/settings - 设置语言、存储服务、文件有效时长\n/delete - 删除上传到 Catbox 的文件\n/token - 绑定 Catbox 令牌(设置中同样可见)\n\n网络资源分享频道: @atashare\nBot 更新频道:@atabots\nBot 源码:<a href=\"https://github.com/AnotiaWang/TG_Catbox_Uploader\">GitHub</a>",
"downloading": "正在下载文件...",
"downloadProgress": "<b>📥 下载中</b>\n\n<b>文件大小:</b>{1} MB\n<b>已下载:</b>{2} MB\n<b>速度:</b>{3} MB/s\n<b>剩余时间:</b>{4}",
"uploading": "正在上传至 {s}...",
"uploaded": "🗃️ 上传完成!\n\n存储服务:{1}\n有效期:{2}\n链接:{3}",
"error": "😥 传输失败!或许是服务器目前负载过大或网络连接不畅,请稍后再试。",
Expand Down

0 comments on commit ab0c6eb

Please sign in to comment.