__author__ = 'agrigoryev' import os import struct import json import io from numbers import Number from binascii import hexlify class TlConstructor: def __init__(self, json_dict): self.id = int(json_dict['id']) self.type = json_dict['type'] self.predicate = json_dict['predicate'] self.params = [] # case of vector for param in json_dict['params']: if param['type'] == "Vector": param['type'] = "Vector t" param['subtype'] = "long" elif param['type'] == "vector<%Message>": param['type'] = "vector" param['subtype'] = "message" elif param['type'] == "vector": param['type'] = "vector" param['subtype'] = "future_salt" else: param['subtype'] = None self.params.append(param) class TlMethod: def __init__(self, json_dict): self.id = int(json_dict['id']) self.type = json_dict['type'] self.method = json_dict['method'] self.params = json_dict['params'] class TLObject(dict): def __init__(self, tl_elem): self.name = tl_elem.predicate class TL: def __init__(self, filename): with open(filename, 'r') as f: TL_dict = json.load(f) # Read constructors self.constructors = TL_dict['constructors'] self.constructor_id = {} self.constructor_type = {} for elem in self.constructors: z = TlConstructor(elem) self.constructor_id[z.id] = z self.constructor_type[z.predicate] = z self.methods = TL_dict['methods'] self.method_id = {} self.method_name = {} for elem in self.methods: z = TlMethod(elem) self.method_id[z.id] = z self.method_name[z.method] = z ## Loading TL_schema (should be placed in the same directory as mtproto.py tl = TL(os.path.join(os.path.dirname(__file__), "TL_schema.JSON")) def serialize_obj(type_, **kwargs): bytes_io = io.BytesIO() try: tl_constructor = tl.constructor_type[type_] except KeyError: raise Exception("Could not extract type: %s" % type_) bytes_io.write(struct.pack('