-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
29 lines (25 loc) · 933 Bytes
/
util.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
from time import sleep, time
from apis import sessions, bot_logger
import json
import urllib.request
import os
import random
def clean_old_sessions():
interval = 30 * 60 # 30 minutes
while True:
sleep(interval)
for i in list(sessions.keys()):
if time() - sessions[i].time > interval:
person = sessions.pop(i)
person.close()
bot_logger.info("%s session expired id: %s", person.name, person.id)
def get_ram_mem(_id=None):
os.environ['no_proxy'] = "https://rickandmortyapi.com"
base_url = "https://rickandmortyapi.com/api/character/"
count = json.load(urllib.request.urlopen(base_url))['info']['count']
if not _id:
_id = random.randint(0, count + 1)
res = json.load(urllib.request.urlopen(base_url + "/" + str(_id)))
image = urllib.request.urlopen(res['image']).read()
name = res['name']
return name, image