Skip to content

Commit ffabbd6

Browse files
committed
more fixes including making categories dynamic
1 parent b663aa2 commit ffabbd6

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

pybpsapi.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ class API:
88
def __init__(self, url="https://bpsapi.rajtech.me/v1/"):
99
self.url = url
1010

11+
json = requests.get(self.url + "categories").json()
12+
if json['http_status'] == 200:
13+
self.categories = json['data']
14+
else:
15+
raise ConnectionError("Invalid API Response. API says there are no categories.")
16+
1117
# /latest endpoint
1218
def latest(self, category: str or int, cached: bool = False) -> dict or None:
1319
"""The `/latest` endpoint returns the latest circular from a particular category"""
@@ -17,7 +23,7 @@ def latest(self, category: str or int, cached: bool = False) -> dict or None:
1723
raise ValueError("Invalid category Number")
1824

1925
else:
20-
if category not in ["ptm", "general", "exam"]:
26+
if category not in self.categories:
2127
raise ValueError("Invalid category Name")
2228

2329
params = {'category': category}
@@ -41,7 +47,7 @@ def list(self, category: str or int, amount: int = -1) -> list or None:
4147
raise ValueError("Invalid category Number")
4248

4349
else:
44-
if category not in ["ptm", "general", "exam"]:
50+
if category not in self.categories:
4551
raise ValueError("Invalid category Name")
4652

4753
if amount < -1:
@@ -107,6 +113,13 @@ def __init__(self, category, url: str = "https://bpsapi.rajtech.me/v1/", cache_m
107113
self.category = category
108114
self._cache = []
109115

116+
json = requests.get(self.url + "categories").json()
117+
if json['http_status'] == 200:
118+
self.categories = json['data']
119+
else:
120+
raise ConnectionError("Invalid API Response. API says there are no categories.")
121+
122+
110123
if debug:
111124
self.set_cache = self._set_cache
112125
self.refresh_cache = self._refresh_cache
@@ -117,7 +130,8 @@ def __init__(self, category, url: str = "https://bpsapi.rajtech.me/v1/", cache_m
117130
raise ValueError("Invalid category Number")
118131

119132
else:
120-
if category not in ["ptm", "general", "exam"]:
133+
if category not in self.categories:
134+
print(category)
121135
raise ValueError("Invalid category Name")
122136

123137
self._params = {'category': category}
@@ -145,10 +159,11 @@ def __init__(self, category, url: str = "https://bpsapi.rajtech.me/v1/", cache_m
145159
f"CREATE TABLE IF NOT EXISTS {self.db_table} (title TEXT, category TEXT, data BLOB)")
146160

147161
# check if the cache exists
148-
self._cur.execute(f"SELECT * FROM {self.db_table} WHERE title = ? AND category = ?", ("circular_list", self.category))
162+
self._cur.execute(f"SELECT * FROM {self.db_table} WHERE title = ? AND category = ?",
163+
("circular_list", self.category))
149164
if self._cur.fetchone() is None:
150165
self._cur.execute(f"INSERT INTO {self.db_table} VALUES (?, ?, ?)",
151-
("circular_list", self.category, pickle.dumps([])))
166+
("circular_list", self.category, pickle.dumps([])))
152167
self._con.commit()
153168

154169
elif cache_method == "pickle":

0 commit comments

Comments
 (0)