Skip to content

Commit 09e0a69

Browse files
committed
fix a few issues and refresh db connection func
1 parent de58f96 commit 09e0a69

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

pybpsapi.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ 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,))
144+
self._cur.execute("CREATE TABLE IF NOT EXISTS ? (title TEXT, category TEXT, data BLOB)",
145+
(self.db_table,))
145146
self._cur.execute("INSERT INTO ? VALUES (?, ?, ?)",
146147
(self.db_table, "circular_list", self.category, pickle.dumps([])))
147148
self._con.commit()
@@ -167,11 +168,18 @@ def __init__(self, category, url: str = "https://bpsapi.rajtech.me/v1/", cache_m
167168
else:
168169
raise ValueError("Invalid Cache Method")
169170

170-
else:
171-
pass
171+
def refresh_db_con(self):
172+
if not self.cache_method == "database":
173+
return
174+
175+
import sqlite3
176+
177+
self._con = sqlite3.connect(self.db_path + f"/{self.db_name}.db")
178+
self._cur = self._con.cursor()
172179

173180
def get_cache(self) -> list[list]:
174181
if self.cache_method == "database":
182+
self.refresh_db_con()
175183
self._cur.execute("SELECT * FROM ? WHERE category = ?", (self.db_table, self.category))
176184
res = self._cur.fetchone()
177185
if res is None:
@@ -189,8 +197,10 @@ def get_cache(self) -> list[list]:
189197

190198
def _set_cache(self, data, title: str = "circular_list"):
191199
if self.cache_method == "database":
200+
self.refresh_db_con()
192201
self._cur.execute("DELETE FROM ? WHERE category = ?", (self.db_table, self.category,))
193-
self._cur.execute("INSERT INTO ? VALUES (?, ?, ?)", (self.db_table, title, self.category, pickle.dumps(data)))
202+
self._cur.execute("INSERT INTO ? VALUES (?, ?, ?)",
203+
(self.db_table, title, self.category, pickle.dumps(data)))
194204
self._con.commit()
195205

196206
elif self.cache_method == "pickle":
@@ -203,10 +213,12 @@ def _set_cache(self, data, title: str = "circular_list"):
203213
def _refresh_cache(self):
204214
request = requests.get(self.url + "list", params=self._params)
205215
json = request.json()
216+
206217
try:
207218
json['http_status']
208219
except KeyError:
209220
raise ValueError("Invalid API Response")
221+
210222
if json['http_status'] == 200:
211223
self._set_cache(json['data'])
212224

@@ -237,13 +249,10 @@ def check(self) -> list[dict] or list[None]:
237249

238250

239251
class CircularCheckerGroup:
240-
def __init__(self, *args, **kwargs):
252+
def __init__(self, *args, debug: bool = False):
241253
self._checkers = []
242254

243-
if kwargs.get("debug"):
244-
self.debug = True
245-
else:
246-
self.debug = False
255+
self.debug = debug
247256

248257
for arg in args:
249258
if type(arg) != CircularChecker:
@@ -260,7 +269,8 @@ def add(self, checker: CircularChecker, *args: CircularChecker):
260269
raise ValueError("Invalid CircularChecker Object")
261270
self._checkers.append(arg)
262271

263-
def create(self, category, url: str = "https://bpsapi.rajtech.me/v1/", cache_method=None, debug: bool = False, **kwargs):
272+
def create(self, category, url: str = "https://bpsapi.rajtech.me/v1/", cache_method=None, debug: bool = False,
273+
**kwargs):
264274
checker = CircularChecker(category, url, cache_method, debug, **kwargs)
265275
self._checkers.append(checker)
266276

0 commit comments

Comments
 (0)