1
0
Fork 0
mirror of https://github.com/SlavikMIPT/tgcloud.git synced 2025-02-12 11:12:09 +00:00
tgcloud/download_service.py

133 lines
3.9 KiB
Python
Raw Normal View History

2018-09-02 19:55:19 +00:00
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
2018-09-02 19:55:19 +00:00
import os
import time
2019-07-10 02:09:27 +00:00
2018-09-02 19:55:19 +00:00
from telethon.tl.types import DocumentAttributeFilename
2019-06-14 00:08:43 +00:00
2019-07-08 09:51:01 +00:00
from secret import *
2019-07-10 02:09:27 +00:00
from telegram_client_x import TelegramClientX
2019-06-14 00:08:43 +00:00
path_home = './' # os.path.abspath('.')
client = TelegramClientX(entity, api_id, api_hash, update_workers=None, spawn_read_thread=True)
# client = TelegramClient(entity, api_id, api_hash, update_workers=None, spawn_read_thread=True)
client.set_upload_threads_count(24) # 24
client.set_download_threads_count(24) # 8
2019-06-14 00:08:43 +00:00
last_call_time_sent = time.time()
last_call_time_receive = time.time()
client.connect()
if not client.is_user_authorized():
raise Exception("Telegram session not found - run from the project folder: python3.6 telegram_create_session.py")
2018-09-02 19:55:19 +00:00
def on_download_progress(recv_bytes, total_bytes):
2019-06-14 00:08:43 +00:00
global last_call_time_receive
if time.time() - last_call_time_receive < 1:
2018-09-02 19:55:19 +00:00
return 0
2019-06-14 00:08:43 +00:00
last_call_time_receive = time.time()
# print(f"receive {recv_bytes}/{total_bytes}", end="\r")
2018-09-02 19:55:19 +00:00
return 0
2018-09-02 19:55:19 +00:00
def on_upload_progress(send_bytes, total_bytes):
2019-06-14 00:08:43 +00:00
global last_call_time_sent
if time.time() - last_call_time_sent < 1:
2018-09-02 19:55:19 +00:00
return 0
2019-06-14 00:08:43 +00:00
last_call_time_sent = time.time()
# print(f"sent {send_bytes}/{total_bytes}", end="\r")
2018-09-02 19:55:19 +00:00
return 0
2019-06-16 15:38:44 +00:00
#
def download_block(hash_uid, filename):
2018-09-02 19:55:19 +00:00
try:
hash_uid = str(hash_uid)
2019-06-14 00:08:43 +00:00
os.chdir(path_home)
2019-06-17 09:31:47 +00:00
entity = client.get_entity(client.get_me())
messages = client.get_messages(entity, limit=1, search=hash_uid)
for i in range(len(messages)):
2018-09-02 19:55:19 +00:00
msg = messages[i]
if msg.message == hash_uid:
# FIFO = f"dpipe_{hash_uid}"
# import errno
# try:
# os.mkfifo(FIFO)
# except OSError as oe:
# if oe.errno != errno.EEXIST:
# raise
# outbuf = open(FIFO, "wb"):
2019-07-10 02:09:27 +00:00
# os.unlink(FIFO)
client.download_media(msg, file=filename, progress_callback=on_download_progress)
2019-07-10 02:09:27 +00:00
# outbuf.flush()
2019-06-14 00:08:43 +00:00
return 0
except Exception as e:
2019-06-14 00:08:43 +00:00
return -1
2018-09-02 19:55:19 +00:00
finally:
client.disconnect()
def upload_block(hash_uid):
2018-09-02 19:55:19 +00:00
try:
hash_uid = str(hash_uid)
2019-06-14 00:08:43 +00:00
os.chdir(path_home)
2019-06-17 09:31:47 +00:00
entity = client.get_entity(client.get_me())
FIFO = f"upipe_{hash_uid}"
import errno
try:
os.mkfifo(FIFO)
except OSError as oe:
if oe.errno != errno.EEXIST:
raise
2019-07-10 02:09:27 +00:00
messages = client.get_messages(entity, limit=1, search=hash_uid)
with open(FIFO, 'rb') as bytesin:
2019-07-10 02:09:27 +00:00
if messages:
return 0
message = client.send_file(entity,
file=bytesin,
caption=f'{hash_uid}',
attributes=[DocumentAttributeFilename(f'{hash_uid}')],
allow_cache=False,
part_size_kb=512,
force_document=True,
progress_callback=on_upload_progress)
2019-06-14 00:08:43 +00:00
return 0
except Exception:
2019-06-14 00:08:43 +00:00
return -1
2018-09-02 19:55:19 +00:00
finally:
client.disconnect()
2019-06-14 00:08:43 +00:00
def main(argv):
try:
service = str(argv[1])
if service == 'download':
2019-06-17 09:31:47 +00:00
uid = str(argv[2])
2019-07-10 02:09:27 +00:00
filename = str(argv[3])
download_block(hash_uid=uid, filename=filename)
2019-06-14 00:08:43 +00:00
return 0
elif service == 'upload':
2019-06-17 09:31:47 +00:00
uid = str(argv[2])
upload_block(hash_uid=uid)
2019-06-14 00:08:43 +00:00
return 0
except Exception as e:
# print(e)
return -1
finally:
client.disconnect()
return 0
if __name__ == '__main__':
import sys
main(sys.argv[0:])