forked from alfem/telegram-download-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessionManager.py
33 lines (25 loc) · 1.06 KB
/
sessionManager.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
29
30
31
32
33
from os import getenv, path
from telethon.sessions import StringSession
TELEGRAM_DAEMON_SESSION_PATH = getenv("TELEGRAM_DAEMON_SESSION_PATH")
sessionName = "DownloadDaemon"
stringSessionFilename = "{0}.session".format(sessionName)
def _getStringSessionIfExists():
sessionPath = path.join(TELEGRAM_DAEMON_SESSION_PATH,
stringSessionFilename)
if path.isfile(sessionPath):
with open(sessionPath, 'r') as file:
session = file.read()
print("Session loaded from {0}".format(sessionPath))
return session
return None
def getSession():
if TELEGRAM_DAEMON_SESSION_PATH == None:
return sessionName
return StringSession(_getStringSessionIfExists())
def saveSession(session):
if TELEGRAM_DAEMON_SESSION_PATH != None:
sessionPath = path.join(TELEGRAM_DAEMON_SESSION_PATH,
stringSessionFilename)
with open(sessionPath, 'w') as file:
file.write(StringSession.save(session))
print("Session saved in {0}".format(sessionPath))