-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient.py
More file actions
55 lines (48 loc) · 1.51 KB
/
Copy pathclient.py
File metadata and controls
55 lines (48 loc) · 1.51 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import logging, asyncio
from database import database
from pyrogram.client import Client
from config import Config
from time import time
TimeHandler = {}
ClientHolder = {}
async def stopClient(user):
while (client := ClientHolder.get(user)) and TimeHandler.get(user):
logging.debug(f"checking user:client {user}")
diff = (time() - TimeHandler[user]) / 60
# if client is not used for more than 15 minutes
# disconnect the client
if diff > 15:
logging.debug(f"stopping user:client {user}")
await client.stop()
del ClientHolder[user]
del TimeHandler[user]
await asyncio.sleep(30)
async def getClient(user):
user = str(user)
TimeHandler[user] = time()
if ClientHolder.get(user):
return ClientHolder[user]
ref = database.child(user)
token = ref.child("userLogin").get()
apiId = ref.child("apiId").get()
apiHash = ref.child("apiHash").get()
if not token:
return
logging.info(f"Logging as [{user}]")
client = Client(
f"sessions/{user}",
api_id=apiId or Config.API_ID,
api_hash=apiHash or Config.API_HASH,
session_string=token,
app_version=Config.APP_VERSION,
workers=Config.WORKERS,
# proxy={
# "scheme": "socks5",
# "hostname": "172.93.110.144",
# "port": 32268
# }
)
ClientHolder[user] = client
await client.start()
asyncio.create_task(stopClient(user))
return client