add transfer.sh progress bar, fix 404 bug

This commit is contained in:
BennyThink 2022-02-24 20:25:26 +08:00
parent ae21e0eb31
commit 41a51cee9a
No known key found for this signature in database
GPG Key ID: 6CD0DBDA5235D481

View File

@ -7,7 +7,6 @@
__author__ = "Benny <benny.think@gmail.com>" __author__ = "Benny <benny.think@gmail.com>"
import json
import logging import logging
import os import os
import pathlib import pathlib
@ -18,6 +17,7 @@ import threading
import time import time
import traceback import traceback
import typing import typing
from hashlib import md5
from urllib.parse import quote_plus from urllib.parse import quote_plus
import psutil import psutil
@ -27,7 +27,7 @@ from celery import Celery
from celery.worker.control import Panel from celery.worker.control import Panel
from pyrogram import Client, idle from pyrogram import Client, idle
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, Message from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, Message
from requests_toolbelt.multipart.encoder import MultipartEncoder from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor
from client_init import create_app from client_init import create_app
from config import (ARCHIVE_ID, AUDIO_FORMAT, BROKER, ENABLE_CELERY, from config import (ARCHIVE_ID, AUDIO_FORMAT, BROKER, ENABLE_CELERY,
@ -239,12 +239,13 @@ def get_dl_source():
return "" return ""
def upload_transfer_sh(paths: list) -> "str": def upload_transfer_sh(bm, paths: list) -> "str":
d = {p.name: (p.name, p.open("rb")) for p in paths} d = {p.name: (md5(p.name.encode("utf8")).hexdigest() + p.suffix, p.open("rb")) for p in paths}
m = MultipartEncoder(fields=d) monitor = MultipartEncoderMonitor(MultipartEncoder(fields=d), lambda x: upload_hook(x.bytes_read, x.len, bm))
headers = {'Content-Type': m.content_type} headers = {'Content-Type': monitor.content_type}
try: try:
req = requests.post("https://transfer.sh", data=m, headers=headers) req = requests.post("https://transfer.sh", data=monitor, headers=headers)
bm.edit_text(f"Download success!✅")
return re.sub(r"https://", "\nhttps://", req.text) return re.sub(r"https://", "\nhttps://", req.text)
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
return f"Upload failed!❌\n\n```{e}```" return f"Upload failed!❌\n\n```{e}```"
@ -267,7 +268,7 @@ def ytdl_normal_download(bot_msg, client, url):
t = f"Your video({sizeof_fmt(st_size)}) is too large for Telegram. I'll upload it to transfer.sh" t = f"Your video({sizeof_fmt(st_size)}) is too large for Telegram. I'll upload it to transfer.sh"
bot_msg.edit_text(t) bot_msg.edit_text(t)
client.send_chat_action(chat_id, 'upload_document') client.send_chat_action(chat_id, 'upload_document')
client.send_message(chat_id, upload_transfer_sh(video_paths)) client.send_message(chat_id, upload_transfer_sh(bot_msg, video_paths))
return return
upload_processor(client, bot_msg, url, video_path) upload_processor(client, bot_msg, url, video_path)
bot_msg.edit_text('Download success!✅') bot_msg.edit_text('Download success!✅')