forked from subzeroid/instagrapi-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storages.py
46 lines (39 loc) · 1.34 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
38
39
40
41
42
43
44
45
46
from urllib import parse
from instagrapi import Client
from tinydb import TinyDB, Query
from tinydb.table import Document
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(" \""))
user_pkid = key.split(":")[0]
self.db.insert(Document({'sessionid': key, 'settings': json.dumps(cl.get_settings())}, doc_id=user_pkid))
return True
def set_custom(self, user_pkid, settings) -> bool:
"""Set client settings
"""
self.db.insert(Document({'sessionid': key, 'settings': settings}, doc_id=user_pkid))
return True
def close(self):
pass