Spaces:
Running
Running
Update sefaria.py
Browse files- sefaria.py +83 -83
sefaria.py
CHANGED
@@ -1,83 +1,83 @@
|
|
1 |
-
import requests, json
|
2 |
-
|
3 |
-
SEFARIA_API_BASE_URL = "
|
4 |
-
|
5 |
-
def _get_request_json_data(endpoint, ref=None, param=None):
|
6 |
-
"""
|
7 |
-
Helper function to make GET requests to the Sefaria API and parse the JSON response.
|
8 |
-
"""
|
9 |
-
url = f"{SEFARIA_API_BASE_URL}/{endpoint}"
|
10 |
-
|
11 |
-
if ref:
|
12 |
-
url += f"{ref}"
|
13 |
-
|
14 |
-
if param:
|
15 |
-
url += f"?{param}"
|
16 |
-
|
17 |
-
try:
|
18 |
-
response = requests.get(url)
|
19 |
-
response.raise_for_status() # Raise an exception for bad status codes
|
20 |
-
data = response.json()
|
21 |
-
return data
|
22 |
-
except requests.exceptions.RequestException as e:
|
23 |
-
print(f"Error during API request: {e}")
|
24 |
-
return None
|
25 |
-
|
26 |
-
|
27 |
-
def get_text(reference: str) -> str:
|
28 |
-
"""
|
29 |
-
Retrieves the text for a given reference.
|
30 |
-
"""
|
31 |
-
return str(_get_hebrew_text(reference))
|
32 |
-
|
33 |
-
|
34 |
-
def get_weekly_parasha():
|
35 |
-
"""
|
36 |
-
Retrieves the weekly Parasha data using the Calendars API.
|
37 |
-
"""
|
38 |
-
data = _get_request_json_data("api/calendars")
|
39 |
-
|
40 |
-
if data:
|
41 |
-
calendar_items = data.get('calendar_items', [])
|
42 |
-
for item in calendar_items:
|
43 |
-
if item.get('title', {}).get('en') == 'Parashat Hashavua':
|
44 |
-
parasha_ref = item.get('ref')
|
45 |
-
parasha_description = item.get('description', {}).get('he')
|
46 |
-
parasha_name_he = item.get('displayValue', {}).get('he')
|
47 |
-
return {
|
48 |
-
"ref": parasha_ref,
|
49 |
-
"description": parasha_description,
|
50 |
-
"name_he": parasha_name_he
|
51 |
-
}
|
52 |
-
|
53 |
-
print("Could not retrieve Parasha data.")
|
54 |
-
return None
|
55 |
-
|
56 |
-
|
57 |
-
def _get_hebrew_text(parasha_ref):
|
58 |
-
"""
|
59 |
-
Retrieves the Hebrew text and version title for the given verse.
|
60 |
-
"""
|
61 |
-
data = _get_request_json_data("api/v3/texts/", parasha_ref)
|
62 |
-
|
63 |
-
if data and "versions" in data and len(data['versions']) > 0:
|
64 |
-
he_pasuk = data['versions'][0]['text']
|
65 |
-
return he_pasuk
|
66 |
-
else:
|
67 |
-
print(f"Could not retrieve Hebrew text for {parasha_ref}")
|
68 |
-
return None
|
69 |
-
|
70 |
-
|
71 |
-
def get_commentaries(parasha_ref)-> list[str]:
|
72 |
-
"""
|
73 |
-
Retrieves and filters commentaries on the given verse.
|
74 |
-
"""
|
75 |
-
data = _get_request_json_data("api/related/", parasha_ref)
|
76 |
-
|
77 |
-
commentaries = []
|
78 |
-
if data and "links" in data:
|
79 |
-
for linked_text in data["links"]:
|
80 |
-
if linked_text.get('type') == 'commentary':
|
81 |
-
commentaries.append(linked_text.get('sourceHeRef'))
|
82 |
-
|
83 |
-
return commentaries
|
|
|
1 |
+
import requests, json
|
2 |
+
|
3 |
+
SEFARIA_API_BASE_URL = "https://sefaria.com"
|
4 |
+
|
5 |
+
def _get_request_json_data(endpoint, ref=None, param=None):
|
6 |
+
"""
|
7 |
+
Helper function to make GET requests to the Sefaria API and parse the JSON response.
|
8 |
+
"""
|
9 |
+
url = f"{SEFARIA_API_BASE_URL}/{endpoint}"
|
10 |
+
|
11 |
+
if ref:
|
12 |
+
url += f"{ref}"
|
13 |
+
|
14 |
+
if param:
|
15 |
+
url += f"?{param}"
|
16 |
+
|
17 |
+
try:
|
18 |
+
response = requests.get(url)
|
19 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
20 |
+
data = response.json()
|
21 |
+
return data
|
22 |
+
except requests.exceptions.RequestException as e:
|
23 |
+
print(f"Error during API request: {e}")
|
24 |
+
return None
|
25 |
+
|
26 |
+
|
27 |
+
def get_text(reference: str) -> str:
|
28 |
+
"""
|
29 |
+
Retrieves the text for a given reference.
|
30 |
+
"""
|
31 |
+
return str(_get_hebrew_text(reference))
|
32 |
+
|
33 |
+
|
34 |
+
def get_weekly_parasha():
|
35 |
+
"""
|
36 |
+
Retrieves the weekly Parasha data using the Calendars API.
|
37 |
+
"""
|
38 |
+
data = _get_request_json_data("api/calendars")
|
39 |
+
|
40 |
+
if data:
|
41 |
+
calendar_items = data.get('calendar_items', [])
|
42 |
+
for item in calendar_items:
|
43 |
+
if item.get('title', {}).get('en') == 'Parashat Hashavua':
|
44 |
+
parasha_ref = item.get('ref')
|
45 |
+
parasha_description = item.get('description', {}).get('he')
|
46 |
+
parasha_name_he = item.get('displayValue', {}).get('he')
|
47 |
+
return {
|
48 |
+
"ref": parasha_ref,
|
49 |
+
"description": parasha_description,
|
50 |
+
"name_he": parasha_name_he
|
51 |
+
}
|
52 |
+
|
53 |
+
print("Could not retrieve Parasha data.")
|
54 |
+
return None
|
55 |
+
|
56 |
+
|
57 |
+
def _get_hebrew_text(parasha_ref):
|
58 |
+
"""
|
59 |
+
Retrieves the Hebrew text and version title for the given verse.
|
60 |
+
"""
|
61 |
+
data = _get_request_json_data("api/v3/texts/", parasha_ref)
|
62 |
+
|
63 |
+
if data and "versions" in data and len(data['versions']) > 0:
|
64 |
+
he_pasuk = data['versions'][0]['text']
|
65 |
+
return he_pasuk
|
66 |
+
else:
|
67 |
+
print(f"Could not retrieve Hebrew text for {parasha_ref}")
|
68 |
+
return None
|
69 |
+
|
70 |
+
|
71 |
+
def get_commentaries(parasha_ref)-> list[str]:
|
72 |
+
"""
|
73 |
+
Retrieves and filters commentaries on the given verse.
|
74 |
+
"""
|
75 |
+
data = _get_request_json_data("api/related/", parasha_ref)
|
76 |
+
|
77 |
+
commentaries = []
|
78 |
+
if data and "links" in data:
|
79 |
+
for linked_text in data["links"]:
|
80 |
+
if linked_text.get('type') == 'commentary':
|
81 |
+
commentaries.append(linked_text.get('sourceHeRef'))
|
82 |
+
|
83 |
+
return commentaries
|