-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter.py
37 lines (32 loc) · 1.32 KB
/
Chapter.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
import requests
import json
import Utils
from Ahadith import Ahadith
class Chapter:
def __init__(self, bookID: int, chapterID: int, chapterName: str):
self.id = chapterID
self.name = chapterName
self.ahadiths = self.getAhadiths(bookID)
self.ahadithsCount = len(self.ahadiths)
def getAhadiths(self, bookID: int) -> []:
ahadithEndpoint: str = f"https://ahadith-api.herokuapp.com/api/ahadith/{bookID}/{self.id}/en"
try:
response = requests.get(ahadithEndpoint)
# printJSON(response.json())
ahadithsText = json.dumps(response.json(), sort_keys=True, indent=4)
ahadithsData = json.loads(ahadithsText)
except requests.exceptions.RequestException as e:
print(e)
raise SystemExit(e)
ahadiths = []
for item in ahadithsData['Chapter']:
ahadith = Ahadith(item['Hadith_ID'], item['En_Sanad'], item['En_Text'])
# print(f"{ahadith.id}, {ahadith.sanad}, {ahadith.text}")
ahadiths.append(ahadith)
return ahadiths
def getAhadith(self, ahadithID: int):
index: int = ahadithID - 1
if index < 0 or index >= self.ahadithsCount:
raise Exception(f"Unable to get ahadith {ahadithID}")
else:
return self.ahadiths[index]