mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-09 05:19:41 +00:00
Many code-style improvements
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
from telethon.tl.types import UpdateShortChatMessage
|
||||
from telethon.tl.types import UpdateShortMessage
|
||||
from telethon import TelegramClient, RPCError
|
||||
|
||||
from telethon.utils import get_display_name, get_input_peer
|
||||
|
||||
import shutil
|
||||
from getpass import getpass
|
||||
|
||||
from telethon import RPCError, TelegramClient
|
||||
from telethon.tl.types import UpdateShortChatMessage, UpdateShortMessage
|
||||
from telethon.utils import get_display_name, get_input_peer
|
||||
|
||||
# Get the (current) number of lines in the terminal
|
||||
cols, rows = shutil.get_terminal_size()
|
||||
|
||||
@@ -27,7 +25,8 @@ def bytes_to_string(byte_count):
|
||||
byte_count /= 1024
|
||||
suffix_index += 1
|
||||
|
||||
return '{:.2f}{}'.format(byte_count, [' bytes', 'KB', 'MB', 'GB', 'TB'][suffix_index])
|
||||
return '{:.2f}{}'.format(byte_count,
|
||||
[' bytes', 'KB', 'MB', 'GB', 'TB'][suffix_index])
|
||||
|
||||
|
||||
class InteractiveTelegramClient(TelegramClient):
|
||||
@@ -58,7 +57,8 @@ class InteractiveTelegramClient(TelegramClient):
|
||||
# Two-step verification may be enabled
|
||||
except RPCError as e:
|
||||
if e.password_required:
|
||||
pw = getpass('Two step verification is enabled. Please enter your password: ')
|
||||
pw = getpass(
|
||||
'Two step verification is enabled. Please enter your password: ')
|
||||
code_ok = self.sign_in(password=pw)
|
||||
else:
|
||||
raise e
|
||||
@@ -117,10 +117,14 @@ class InteractiveTelegramClient(TelegramClient):
|
||||
print('Available commands:')
|
||||
print(' !q: Quits the current chat.')
|
||||
print(' !Q: Quits the current chat and exits.')
|
||||
print(' !h: prints the latest messages (message History) of the chat.')
|
||||
print(' !up <path>: Uploads and sends a Photo located at the given path.')
|
||||
print(' !uf <path>: Uploads and sends a File document located at the given path.')
|
||||
print(' !dm <msg-id>: Downloads the given message Media (if any).')
|
||||
print(
|
||||
' !h: prints the latest messages (message History) of the chat.')
|
||||
print(
|
||||
' !up <path>: Uploads and sends a Photo located at the given path.')
|
||||
print(
|
||||
' !uf <path>: Uploads and sends a File document located at the given path.')
|
||||
print(
|
||||
' !dm <msg-id>: Downloads the given message Media (if any).')
|
||||
print(' !dp: Downloads the current dialog Profile picture.')
|
||||
print()
|
||||
|
||||
@@ -136,10 +140,12 @@ class InteractiveTelegramClient(TelegramClient):
|
||||
# History
|
||||
elif msg == '!h':
|
||||
# First retrieve the messages and some information
|
||||
total_count, messages, senders = self.get_message_history(input_peer, limit=10)
|
||||
total_count, messages, senders = self.get_message_history(
|
||||
input_peer, limit=10)
|
||||
# Iterate over all (in reverse order so the latest appears the last in the console)
|
||||
# and print them in "[hh:mm] Sender: Message" text format
|
||||
for msg, sender in zip(reversed(messages), reversed(senders)):
|
||||
for msg, sender in zip(
|
||||
reversed(messages), reversed(senders)):
|
||||
# Get the name of the sender if any
|
||||
name = sender.first_name if sender else '???'
|
||||
|
||||
@@ -147,13 +153,15 @@ class InteractiveTelegramClient(TelegramClient):
|
||||
if msg.media:
|
||||
self.found_media.add(msg)
|
||||
content = '<{}> {}'.format( # The media may or may not have a caption
|
||||
msg.media.__class__.__name__, getattr(msg.media, 'caption', ''))
|
||||
msg.media.__class__.__name__,
|
||||
getattr(msg.media, 'caption', ''))
|
||||
else:
|
||||
content = msg.message
|
||||
|
||||
# And print it to the user
|
||||
print('[{}:{}] (ID={}) {}: {}'.format(
|
||||
msg.date.hour, msg.date.minute, msg.id, name, content))
|
||||
msg.date.hour, msg.date.minute, msg.id, name,
|
||||
content))
|
||||
|
||||
# Send photo
|
||||
elif msg.startswith('!up '):
|
||||
@@ -176,18 +184,21 @@ class InteractiveTelegramClient(TelegramClient):
|
||||
print('Downloading profile picture...')
|
||||
success = self.download_profile_photo(entity.photo, output)
|
||||
if success:
|
||||
print('Profile picture downloaded to {}'.format(output))
|
||||
print('Profile picture downloaded to {}'.format(
|
||||
output))
|
||||
else:
|
||||
print('"{}" does not seem to have a profile picture.'
|
||||
.format(get_display_name(entity)))
|
||||
|
||||
# Send chat message (if any)
|
||||
elif msg:
|
||||
self.send_message(input_peer, msg, markdown=True, no_web_page=True)
|
||||
self.send_message(
|
||||
input_peer, msg, markdown=True, no_web_page=True)
|
||||
|
||||
def send_photo(self, path, peer):
|
||||
print('Uploading {}...'.format(path))
|
||||
input_file = self.upload_file(path, progress_callback=self.upload_progress_callback)
|
||||
input_file = self.upload_file(
|
||||
path, progress_callback=self.upload_progress_callback)
|
||||
|
||||
# After we have the handle to the uploaded file, send it to our peer
|
||||
self.send_photo_file(input_file, peer)
|
||||
@@ -195,7 +206,8 @@ class InteractiveTelegramClient(TelegramClient):
|
||||
|
||||
def send_document(self, path, peer):
|
||||
print('Uploading {}...'.format(path))
|
||||
input_file = self.upload_file(path, progress_callback=self.upload_progress_callback)
|
||||
input_file = self.upload_file(
|
||||
path, progress_callback=self.upload_progress_callback)
|
||||
|
||||
# After we have the handle to the uploaded file, send it to our peer
|
||||
self.send_document_file(input_file, peer)
|
||||
@@ -212,9 +224,10 @@ class InteractiveTelegramClient(TelegramClient):
|
||||
# Let the output be the message ID
|
||||
output = str('usermedia/{}'.format(msg_media_id))
|
||||
print('Downloading media with name {}...'.format(output))
|
||||
output = self.download_msg_media(msg.media,
|
||||
file_path=output,
|
||||
progress_callback=self.download_progress_callback)
|
||||
output = self.download_msg_media(
|
||||
msg.media,
|
||||
file_path=output,
|
||||
progress_callback=self.download_progress_callback)
|
||||
print('Media downloaded to {}!'.format(output))
|
||||
|
||||
except ValueError:
|
||||
@@ -222,32 +235,35 @@ class InteractiveTelegramClient(TelegramClient):
|
||||
|
||||
@staticmethod
|
||||
def download_progress_callback(downloaded_bytes, total_bytes):
|
||||
InteractiveTelegramClient.print_progress('Downloaded', downloaded_bytes, total_bytes)
|
||||
InteractiveTelegramClient.print_progress('Downloaded',
|
||||
downloaded_bytes, total_bytes)
|
||||
|
||||
@staticmethod
|
||||
def upload_progress_callback(uploaded_bytes, total_bytes):
|
||||
InteractiveTelegramClient.print_progress('Uploaded', uploaded_bytes, total_bytes)
|
||||
InteractiveTelegramClient.print_progress('Uploaded', uploaded_bytes,
|
||||
total_bytes)
|
||||
|
||||
@staticmethod
|
||||
def print_progress(progress_type, downloaded_bytes, total_bytes):
|
||||
print('{} {} out of {} ({:.2%})'.format(
|
||||
progress_type,
|
||||
bytes_to_string(downloaded_bytes),
|
||||
bytes_to_string(total_bytes),
|
||||
downloaded_bytes / total_bytes))
|
||||
print('{} {} out of {} ({:.2%})'.format(progress_type, bytes_to_string(
|
||||
downloaded_bytes), bytes_to_string(total_bytes), downloaded_bytes /
|
||||
total_bytes))
|
||||
|
||||
@staticmethod
|
||||
def update_handler(update_object):
|
||||
if type(update_object) is UpdateShortMessage:
|
||||
if update_object.out:
|
||||
print('You sent {} to user #{}'.format(update_object.message, update_object.user_id))
|
||||
print('You sent {} to user #{}'.format(update_object.message,
|
||||
update_object.user_id))
|
||||
else:
|
||||
print('[User #{} sent {}]'.format(update_object.user_id, update_object.message))
|
||||
print('[User #{} sent {}]'.format(update_object.user_id,
|
||||
update_object.message))
|
||||
|
||||
elif type(update_object) is UpdateShortChatMessage:
|
||||
if update_object.out:
|
||||
print('You sent {} to chat #{}'.format(update_object.message, update_object.chat_id))
|
||||
print('You sent {} to chat #{}'.format(update_object.message,
|
||||
update_object.chat_id))
|
||||
else:
|
||||
print('[Chat #{}, user #{} sent {}]'.format(update_object.chat_id,
|
||||
update_object.from_id,
|
||||
update_object.message))
|
||||
print('[Chat #{}, user #{} sent {}]'.format(
|
||||
update_object.chat_id, update_object.from_id,
|
||||
update_object.message))
|
||||
|
Reference in New Issue
Block a user