Totally refactored source files location

Now it *should* be easier to turn Telethon
into a pip package
This commit is contained in:
Lonami
2016-09-17 20:42:34 +02:00
parent 27ec7292d8
commit 51a531225f
42 changed files with 518 additions and 531 deletions

2
telethon/tl/__init__.py Executable file
View File

@@ -0,0 +1,2 @@
from telethon.tl.mtproto_request import MTProtoRequest
from telethon.tl.session import Session

40
telethon/tl/mtproto_request.py Executable file
View File

@@ -0,0 +1,40 @@
from datetime import datetime, timedelta
class MTProtoRequest:
def __init__(self):
self.sent = False
self.msg_id = 0 # Long
self.sequence = 0
self.dirty = False
self.send_time = None
self.confirm_received = False
# These should be overrode
self.constructor_id = 0
self.confirmed = False
self.responded = False
# These should not be overrode
def on_send_success(self):
self.send_time = datetime.now()
self.sent = True
def on_confirm(self):
self.confirm_received = True
def need_resend(self):
return self.dirty or (self.confirmed and not self.confirm_received and
datetime.now() - self.send_time > timedelta(seconds=3))
# These should be overrode
def on_send(self, writer):
pass
def on_response(self, reader):
pass
def on_exception(self, exception):
pass

64
telethon/tl/session.py Executable file
View File

@@ -0,0 +1,64 @@
from os.path import isfile as file_exists
import os
import time
import pickle
import random
import telethon.helpers as utils
class Session:
def __init__(self, session_user_id):
self.session_user_id = session_user_id
self.server_address = '91.108.56.165'
self.port = 443
self.auth_key = None
self.id = utils.generate_random_long(signed=False)
self.sequence = 0
self.salt = 0 # Unsigned long
self.time_offset = 0
self.last_message_id = 0 # Long
self.user = None
def save(self):
"""Saves the current session object as session_user_id.session"""
if self.session_user_id:
with open('{}.session'.format(self.session_user_id), 'wb') as file:
pickle.dump(self, file)
def delete(self):
"""Deletes the current session file"""
try:
os.remove('{}.session'.format(self.session_user_id))
return True
except:
return False
@staticmethod
def try_load_or_create_new(session_user_id):
"""Loads a saved session_user_id session, or creates a new one if none existed before.
If the given session_user_id is None, we assume that it is for testing purposes"""
if session_user_id is None:
return Session(None)
else:
filepath = '{}.session'.format(session_user_id)
if file_exists(filepath):
with open(filepath, 'rb') as file:
return pickle.load(file)
else:
return Session(session_user_id)
def get_new_msg_id(self):
"""Generates a new message ID based on the current time (in ms) since epoch"""
# Refer to mtproto_plain_sender.py for the original method, this is a simple copy
ms_time = int(time.time() * 1000)
new_msg_id = (((ms_time // 1000 + self.time_offset) << 32) | # "must approximately equal unixtime*2^32"
((ms_time % 1000) << 22) | # "approximate moment in time the message was created"
random.randint(0, 524288) << 2) # "message identifiers are divisible by 4"
if self.last_message_id >= new_msg_id:
new_msg_id = self.last_message_id + 4
self.last_message_id = new_msg_id
return new_msg_id