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
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import time
|
2019-06-14 00:08:43 +00:00
|
|
|
import tempfile
|
2019-06-14 09:41:57 +00:00
|
|
|
import mimetypes
|
2018-09-02 19:55:19 +00:00
|
|
|
from telethon.tl.types import DocumentAttributeFilename
|
2019-06-14 09:41:57 +00:00
|
|
|
from telethon.tl.types import Document
|
|
|
|
from telethon.utils import get_input_media
|
|
|
|
from telethon.errors.rpc_error_list import LocationInvalidError
|
2019-06-16 17:30:00 +00:00
|
|
|
# from telegram_client_x import TelegramClientX
|
|
|
|
from telethon.telegram_client import TelegramClient
|
2019-06-14 09:41:57 +00:00
|
|
|
from telethon.tl.types import Message
|
2018-09-02 19:55:19 +00:00
|
|
|
from tg_access import *
|
2019-06-14 09:41:57 +00:00
|
|
|
from io import BytesIO
|
2019-06-14 00:08:43 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
class Buffer: # {{{1
|
|
|
|
|
|
|
|
"""
|
|
|
|
This class wraps cStringIO.StringIO with two additions: The __len__
|
|
|
|
method and a dirty flag to determine whether a buffer has changed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2019-06-14 09:41:57 +00:00
|
|
|
self.buf = BytesIO()
|
2019-06-14 00:08:43 +00:00
|
|
|
self.dirty = False
|
|
|
|
|
|
|
|
def __getattr__(self, attr, default=None):
|
|
|
|
""" Delegate to the StringIO object. """
|
|
|
|
return getattr(self.buf, attr, default)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
""" Get the total size of the buffer in bytes. """
|
|
|
|
position = self.buf.tell()
|
|
|
|
self.buf.seek(0, os.SEEK_END)
|
|
|
|
length = self.buf.tell()
|
|
|
|
self.buf.seek(position, os.SEEK_SET)
|
|
|
|
return length
|
2018-09-02 19:55:19 +00:00
|
|
|
|
2019-06-14 00:08:43 +00:00
|
|
|
def truncate(self, *args):
|
|
|
|
""" Truncate the file at the current position and set the dirty flag. """
|
|
|
|
if len(self) > self.buf.tell():
|
|
|
|
self.dirty = True
|
|
|
|
return self.buf.truncate(*args)
|
|
|
|
|
|
|
|
def write(self, *args):
|
|
|
|
""" Write a string to the file and set the dirty flag. """
|
|
|
|
self.dirty = True
|
|
|
|
return self.buf.write(*args)
|
|
|
|
|
|
|
|
|
|
|
|
path_home = './' # os.path.abspath('.')
|
2018-09-02 19:55:19 +00:00
|
|
|
path_local = './local'
|
2019-06-16 17:30:00 +00:00
|
|
|
# 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(8)#8
|
2019-06-14 00:08:43 +00:00
|
|
|
last_call_time_sent = time.time()
|
|
|
|
last_call_time_receive = time.time()
|
2019-06-17 09:52:12 +00:00
|
|
|
client.connect()
|
|
|
|
if not client.is_user_authorized():
|
|
|
|
phone = input('Enter phone: ')
|
|
|
|
client.send_code_request(phone)
|
|
|
|
client.sign_in(phone, input('Enter code: '))
|
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
|
|
|
|
|
|
|
|
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
|
|
|
#
|
2019-06-17 09:31:47 +00:00
|
|
|
def download_block(hash_uid,chat_id=None):
|
2018-09-02 19:55:19 +00:00
|
|
|
try:
|
2019-06-14 09:41:57 +00:00
|
|
|
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())
|
2019-06-16 15:38:44 +00:00
|
|
|
messages = client.get_messages(entity, limit=40,search=hash_uid)
|
2019-06-14 09:41:57 +00:00
|
|
|
for i in range(len(messages)):
|
2018-09-02 19:55:19 +00:00
|
|
|
msg = messages[i]
|
2019-06-14 09:41:57 +00:00
|
|
|
if msg.message == hash_uid:
|
2019-06-14 00:08:43 +00:00
|
|
|
outbuf = tempfile.NamedTemporaryFile()
|
2019-06-14 09:41:57 +00:00
|
|
|
client.download_media(msg, file=outbuf, progress_callback=on_download_progress)
|
2019-06-14 00:08:43 +00:00
|
|
|
outbuf.seek(0)
|
|
|
|
sys.stdout.buffer.write(outbuf.read())
|
|
|
|
outbuf.close()
|
|
|
|
return 0
|
2019-06-14 09:41:57 +00:00
|
|
|
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-17 09:31:47 +00:00
|
|
|
def upload_block(bytesin, hash_uid,chat_id=None):
|
2018-09-02 19:55:19 +00:00
|
|
|
try:
|
2019-06-14 09:41:57 +00:00
|
|
|
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())
|
2019-06-14 09:41:57 +00:00
|
|
|
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)
|
|
|
|
# message.id
|
2019-06-14 00:08:43 +00:00
|
|
|
return 0
|
2019-06-14 09:41:57 +00:00
|
|
|
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])
|
|
|
|
download_block(hash_uid=uid)
|
2019-06-14 00:08:43 +00:00
|
|
|
return 0
|
|
|
|
elif service == 'upload':
|
|
|
|
data = sys.stdin.buffer.read()
|
2019-06-17 09:31:47 +00:00
|
|
|
uid = str(argv[2])
|
|
|
|
upload_block(bytesin=data, 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
|
|
|
|
|
2019-06-17 09:31:47 +00:00
|
|
|
main(sys.argv[0:])
|