from tinydb import TinyDB, Query from tinydb.operations import set, delete class Database(): def __init__(self): self.db = TinyDB("database.json", indent=4) self.query = Query() self.cookies_table = self.db.table("cookies") self.viewing_table = self.db.table("viewing") self.sites_table = self.db.table("sites") def get_viewing_data(self, url:str ): return (self.viewing_table.get(self.query.url == url)) def get_sites(self): return (self.sites_table.all()) def get_site_by_id(self, id: int): return (self.sites_table.get(self.query.id == id)) def get_sites_table_len(self): return (len(self.sites_table.all())) def get_cookies_table_len(self): return (len(self.cookies_table.all())) def add_cookies(self, arg: dict): self.cookies_table.insert(arg) def get_cookies(self): return (self.cookies_table.get(self.query.id == "0")) def get_last_site_id(self): cookies = self.get_cookies() last_site_id = cookies.get("last_site") return (last_site_id) def get_last_site(self): last_site_id = self.get_last_site_id() last_site = self.get_site_by_id(last_site_id) return (last_site) def get_last_path(self): cookies = self.get_cookies() last_path = cookies.get("last_path") return (last_path) def set_last_site(self, value): self.cookies_table.update(set("last_site", value), self.query.id == "0") def set_last_path(self, value): self.cookies_table.update(set("last_path", value), self.query.id == "0") def add_site(self, site): self.sites_table.insert(site)