Spaces:
Build error
Build error
# MIT License | |
# Copyright (c) 2022 Hash Minner | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE | |
import os | |
import time | |
import requests | |
import logging | |
from Uploader.functions.display_progress import humanbytes | |
logging.basicConfig(level=logging.DEBUG, | |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
logger = logging.getLogger(__name__) | |
def DetectFileSize(url): | |
r = requests.get(url, allow_redirects=True, stream=True) | |
return int(r.headers.get("content-length", 0)) | |
def DownLoadFile(url, file_name, chunk_size, client, ud_type, message_id, chat_id): | |
if os.path.exists(file_name): | |
os.remove(file_name) | |
if not url: | |
return file_name | |
r = requests.get(url, allow_redirects=True, stream=True) | |
# https://stackoverflow.com/a/47342052/4723940 | |
total_size = int(r.headers.get("content-length", 0)) | |
downloaded_size = 0 | |
with open(file_name, 'wb') as fd: | |
for chunk in r.iter_content(chunk_size=chunk_size): | |
if chunk: | |
fd.write(chunk) | |
downloaded_size += chunk_size | |
if ( | |
client is not None | |
and ((total_size // downloaded_size) % 5) == 0 | |
): | |
time.sleep(0.3) | |
try: | |
client.edit_message_text( | |
chat_id, | |
message_id, | |
text=f"{ud_type}: {humanbytes(downloaded_size)} of {humanbytes(total_size)}", | |
) | |
except Exception as e: | |
logger.info(f"Error: {e}") | |
return | |
return file_name | |