Skip to content

Commit b663aa2

Browse files
committed
revert buggy changes
1 parent 8b8f814 commit b663aa2

1 file changed

Lines changed: 23 additions & 12 deletions

File tree

pybpsapi.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,14 @@ def __init__(self, category, url: str = "https://bpsapi.rajtech.me/v1/", cache_m
141141
self._con = sqlite3.connect(self.db_path + f"/{self.db_name}.db")
142142
self._cur = self._con.cursor()
143143

144-
self._cur.execute("CREATE TABLE IF NOT EXISTS ? (title TEXT, category TEXT, data BLOB)", (self.db_table,))
145-
self._cur.execute("INSERT INTO ? VALUES (?, ?, ?)",
146-
(self.db_table, "circular_list", self.category, pickle.dumps([])))
144+
self._cur.execute(
145+
f"CREATE TABLE IF NOT EXISTS {self.db_table} (title TEXT, category TEXT, data BLOB)")
146+
147+
# check if the cache exists
148+
self._cur.execute(f"SELECT * FROM {self.db_table} WHERE title = ? AND category = ?", ("circular_list", self.category))
149+
if self._cur.fetchone() is None:
150+
self._cur.execute(f"INSERT INTO {self.db_table} VALUES (?, ?, ?)",
151+
("circular_list", self.category, pickle.dumps([])))
147152
self._con.commit()
148153

149154
elif cache_method == "pickle":
@@ -159,6 +164,7 @@ def __init__(self, category, url: str = "https://bpsapi.rajtech.me/v1/", cache_m
159164
import os
160165
if not os.path.exists(self.pickle_path):
161166
os.mkdir(self.pickle_path)
167+
162168
# create a pickle file if it doesn't exist
163169
if not os.path.exists(self.pickle_path + f"/{self.pickle_name}.pickle"):
164170
with open(self.pickle_path + f"/{self.pickle_name}.pickle", "wb") as f:
@@ -167,13 +173,9 @@ def __init__(self, category, url: str = "https://bpsapi.rajtech.me/v1/", cache_m
167173
else:
168174
raise ValueError("Invalid Cache Method")
169175

170-
else:
171-
pass
172-
173176
def get_cache(self) -> list[list]:
174177
if self.cache_method == "database":
175-
self.refresh_db_con()
176-
self._cur.execute("SELECT * FROM ? WHERE category = ?", (self.db_table, self.category))
178+
self._cur.execute(f"SELECT * FROM {self.db_table} WHERE category = ?", (self.category,))
177179
res = self._cur.fetchone()
178180
if res is None:
179181
return []
@@ -190,7 +192,6 @@ def get_cache(self) -> list[list]:
190192

191193
def _set_cache(self, data, title: str = "circular_list"):
192194
if self.cache_method == "database":
193-
self.refresh_db_con()
194195
self._cur.execute(f"DELETE FROM {self.db_table} WHERE category = ?", (self.category,))
195196
self._cur.execute(f"INSERT INTO {self.db_table} VALUES (?, ?, ?)",
196197
(title, self.category, pickle.dumps(data)))
@@ -221,16 +222,25 @@ def check(self) -> list[dict] or list[None]:
221222
self._refresh_cache()
222223
return []
223224

225+
self._cur.execute(f"SELECT * FROM {self.db_table} WHERE category = ?", (self.category,))
226+
res = self._cur.fetchone()
227+
if res is None:
228+
cache = []
229+
else:
230+
cache = pickle.loads(res[2])
231+
224232
self._refresh_cache()
225233
final_dict = self.get_cache()
226234

227235
if final_dict != old_cached: # If the old and new dict are not the same
228-
229236
new_circular_objects = [i for i in final_dict if i not in old_cached]
230-
231237
# print(f"{len(new_circular_objects)} new circular(s) found")
232238

233239
for circular in new_circular_objects:
240+
# check if they are in the database
241+
if circular in cache:
242+
continue
243+
234244
return_dict.append(circular)
235245

236246
return return_dict
@@ -263,7 +273,8 @@ def add(self, checker: CircularChecker, *args: CircularChecker):
263273
raise ValueError("Invalid CircularChecker Object")
264274
self._checkers.append(arg)
265275

266-
def create(self, category, url: str = "https://bpsapi.rajtech.me/v1/", cache_method=None, debug: bool = False, **kwargs):
276+
def create(self, category, url: str = "https://bpsapi.rajtech.me/v1/", cache_method=None, debug: bool = False,
277+
**kwargs):
267278
checker = CircularChecker(category, url, cache_method, debug, **kwargs)
268279
self._checkers.append(checker)
269280

0 commit comments

Comments
 (0)