-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #140: kotatogram's upload speed boost big
- Loading branch information
Showing
7 changed files
with
507 additions
and
260 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
Oops, something went wrong.