Skip to content

Commit

Permalink
Issue #140: kotatogram's upload speed boost big
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekmo committed Jun 19, 2023
1 parent 9417a58 commit d07a122
Show file tree
Hide file tree
Showing 7 changed files with 507 additions and 260 deletions.
257 changes: 0 additions & 257 deletions telegram_upload/client.py

This file was deleted.

4 changes: 4 additions & 0 deletions telegram_upload/client/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from telegram_upload.client.telegram_manager_client import TelegramManagerClient, get_message_file_attribute


__all__ = ["TelegramManagerClient", "get_message_file_attribute"]
16 changes: 16 additions & 0 deletions telegram_upload/client/progress_bar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from ctypes import c_int64

import click


def get_progress_bar(action, file, length):
bar = click.progressbar(label='{} "{}"'.format(action, file), length=length)
last_current = c_int64(0)

def progress(current, total):
if current < last_current.value:
return
bar.pos = 0
bar.update(current)
last_current.value = current
return progress, bar
46 changes: 46 additions & 0 deletions telegram_upload/client/telegram_download_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from typing import Iterable

from telethon import TelegramClient

from telegram_upload.client.progress_bar import get_progress_bar
from telegram_upload.download_files import DownloadFile
from telegram_upload.exceptions import TelegramUploadNoSpaceError
from telegram_upload.utils import free_disk_usage, sizeof_fmt


class TelegramDownloadClient(TelegramClient):
def find_files(self, entity):
for message in self.iter_messages(entity):
if message.document:
yield message
else:
break

async def iter_files(self, entity):
async for message in self.iter_messages(entity=entity):
if message.document:
yield message

def download_files(self, entity, download_files: Iterable[DownloadFile], delete_on_success: bool = False):
for download_file in download_files:
if download_file.size > free_disk_usage():
raise TelegramUploadNoSpaceError(
'There is no disk space to download "{}". Space required: {}'.format(
download_file.file_name, sizeof_fmt(download_file.size - free_disk_usage())
)
)
progress, bar = get_progress_bar('Downloading', download_file.file_name, download_file.size)
file_name = download_file.file_name
try:
file_name = self.download_media(download_file.message, progress_callback=progress)
download_file.set_download_file_name(file_name)
finally:
bar.label = f'Downloaded "{file_name}"'
bar.update(1, 1)
bar.render_finish()
if delete_on_success:
self.delete_messages(entity, [download_file.message])

def forward_to(self, message, destinations):
for destination in destinations:
self.forward_messages(destination, [message])
Loading

0 comments on commit d07a122

Please sign in to comment.