forked from japonskygorodovoy/autoclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.py
28 lines (24 loc) · 768 Bytes
/
session.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import telethon
import base64
import pickle
from os.path import isfile as file_exists
class Session(telethon.tl.Session):
def __init__(self, *args):
super(Session, self).__init__(*args)
def save(self):
if self.session_user_id:
s = base64.encodebytes(pickle.dumps(self, protocol=0))
with open('{}.session'.format(self.session_user_id), 'wb') as f:
f.write(s)
@staticmethod
def try_load_or_create_new(session_user_id):
if session_user_id is None:
return Session(None)
else:
path = '{}.session'.format(session_user_id)
if file_exists(path):
with open(path, 'rb') as f:
s = base64.decodebytes(f.read())
return pickle.loads(s)
else:
return Session(session_user_id)