Detect current MTProtoLayer automatically

This commit is contained in:
Lonami 2016-09-17 21:09:20 +02:00
parent 51a531225f
commit 6642f73a3d
5 changed files with 19 additions and 7 deletions

View File

@ -27,11 +27,11 @@ def bytes_to_string(byte_count):
class InteractiveTelegramClient(TelegramClient): class InteractiveTelegramClient(TelegramClient):
def __init__(self, session_user_id, user_phone, layer, api_id, api_hash): def __init__(self, session_user_id, user_phone, api_id, api_hash):
print_title('Initialization') print_title('Initialization')
print('Initializing interactive example...') print('Initializing interactive example...')
super().__init__(session_user_id, layer, api_id, api_hash) super().__init__(session_user_id, api_id, api_hash)
# Store all the found media in memory here, # Store all the found media in memory here,
# so it can be downloaded if the user wants # so it can be downloaded if the user wants

View File

@ -29,21 +29,20 @@ import telethon.network.authenticator as authenticator
from telethon.errors import * from telethon.errors import *
from telethon.network import MtProtoSender, TcpTransport from telethon.network import MtProtoSender, TcpTransport
from telethon.parser.markdown_parser import parse_message_entities from telethon.parser.markdown_parser import parse_message_entities
from telethon.tl.all_tlobjects import layer
class TelegramClient: class TelegramClient:
# region Initialization # region Initialization
def __init__(self, session_user_id, layer, api_id, api_hash): def __init__(self, session_user_id, api_id, api_hash):
if api_id is None or api_hash is None: if api_id is None or api_hash is None:
raise PermissionError('Your API ID or Hash are invalid. Please read "Requirements" on README.md') raise PermissionError('Your API ID or Hash are invalid. Please read "Requirements" on README.md')
self.api_id = api_id self.api_id = api_id
self.api_hash = api_hash self.api_hash = api_hash
self.layer = layer
self.session = Session.try_load_or_create_new(session_user_id) self.session = Session.try_load_or_create_new(session_user_id)
self.transport = TcpTransport(self.session.server_address, self.session.port) self.transport = TcpTransport(self.session.server_address, self.session.port)
@ -78,7 +77,7 @@ class TelegramClient:
lang_code='en', lang_code='en',
query=GetConfigRequest()) query=GetConfigRequest())
result = self.invoke(InvokeWithLayerRequest(layer=self.layer, query=query)) result = self.invoke(InvokeWithLayerRequest(layer=layer, query=query))
# We're only interested in the DC options, # We're only interested in the DC options,
# although many other options are available! # although many other options are available!

View File

@ -29,3 +29,13 @@ class TLParser:
else: else:
yield TLObject.from_tl(line, is_function) yield TLObject.from_tl(line, is_function)
@staticmethod
def find_layer(file_path):
"""Finds the layer used on the specified scheme.tl file"""
layer_regex = re.compile(r'^//\s*LAYER\s*(\d+)$')
with open(file_path, encoding='utf-8') as file:
for line in file:
match = layer_regex.match(line)
if match:
return int(match.group(1))

View File

@ -167,6 +167,10 @@ class TLGenerator:
builder.writeln('import {}'.format(TLGenerator.get_full_file_name(tlobject))) builder.writeln('import {}'.format(TLGenerator.get_full_file_name(tlobject)))
builder.writeln() builder.writeln()
# Create a variable to indicate which layer this is
builder.writeln('layer = {} # Current generated layer'.format(TLParser.find_layer(scheme_file)))
builder.writeln()
# Then create the dictionary containing constructor_id: class # Then create the dictionary containing constructor_id: class
builder.writeln('tlobjects = {') builder.writeln('tlobjects = {')
builder.current_indent += 1 builder.current_indent += 1

View File

@ -25,7 +25,6 @@ if __name__ == '__main__':
client = InteractiveTelegramClient( client = InteractiveTelegramClient(
session_user_id=settings.get('session_name', 'anonymous'), session_user_id=settings.get('session_name', 'anonymous'),
user_phone=str(settings['user_phone']), user_phone=str(settings['user_phone']),
layer=55,
api_id=settings['api_id'], api_id=settings['api_id'],
api_hash=settings['api_hash']) api_hash=settings['api_hash'])