-
Notifications
You must be signed in to change notification settings - Fork 130
/
storages.py
37 lines (32 loc) · 1.03 KB
/
storages.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
34
35
36
37
from urllib import parse
from instagrapi import Client
from tinydb import TinyDB, Query
import json
class ClientStorage:
db = TinyDB('./db.json')
def client(self):
"""Get new client (helper)
"""
cl = Client()
cl.request_timeout = 0.1
return cl
def get(self, sessionid: str) -> Client:
"""Get client settings
"""
key = parse.unquote(sessionid.strip(" \""))
try:
settings = json.loads(self.db.search(Query().sessionid == key)[0]['settings'])
cl = Client()
cl.set_settings(settings)
cl.get_timeline_feed()
return cl
except IndexError:
raise Exception('Session not found (e.g. after reload process), please relogin')
def set(self, cl: Client) -> bool:
"""Set client settings
"""
key = parse.unquote(cl.sessionid.strip(" \""))
self.db.insert({'sessionid': key, 'settings': json.dumps(cl.get_settings())})
return True
def close(self):
pass