Update guardian.py
Browse files- guardian.py +11 -9
guardian.py
CHANGED
@@ -2,7 +2,8 @@
|
|
2 |
import requests
|
3 |
import streamlit as st
|
4 |
|
5 |
-
BASE_URL
|
|
|
6 |
|
7 |
def fetch_headlines(section="world", limit=5):
|
8 |
"""
|
@@ -10,10 +11,10 @@ def fetch_headlines(section="world", limit=5):
|
|
10 |
"""
|
11 |
key = st.secrets["GUARDIAN_API_KEY"]
|
12 |
params = {
|
13 |
-
"api-key":
|
14 |
-
"section":
|
15 |
-
"page-size":
|
16 |
-
"order-by":
|
17 |
"show-fields": "headline"
|
18 |
}
|
19 |
resp = requests.get(BASE_URL, params=params)
|
@@ -26,16 +27,17 @@ def fetch_full_article(url: str) -> str:
|
|
26 |
Given a Guardian article URL, fetch its body text via the Content API.
|
27 |
"""
|
28 |
key = st.secrets["GUARDIAN_API_KEY"]
|
29 |
-
# strip off the public site domain to get the path
|
30 |
-
|
31 |
-
|
|
|
32 |
else:
|
33 |
raise ValueError(f"Unexpected URL format: {url}")
|
34 |
|
35 |
resp = requests.get(
|
36 |
f"{CONTENT_API}{path}",
|
37 |
params={
|
38 |
-
"api-key":
|
39 |
"show-fields": "bodyText"
|
40 |
}
|
41 |
)
|
|
|
2 |
import requests
|
3 |
import streamlit as st
|
4 |
|
5 |
+
BASE_URL = "https://content.guardianapis.com/search"
|
6 |
+
CONTENT_API = "https://content.guardianapis.com"
|
7 |
|
8 |
def fetch_headlines(section="world", limit=5):
|
9 |
"""
|
|
|
11 |
"""
|
12 |
key = st.secrets["GUARDIAN_API_KEY"]
|
13 |
params = {
|
14 |
+
"api-key": key,
|
15 |
+
"section": section,
|
16 |
+
"page-size": limit,
|
17 |
+
"order-by": "newest",
|
18 |
"show-fields": "headline"
|
19 |
}
|
20 |
resp = requests.get(BASE_URL, params=params)
|
|
|
27 |
Given a Guardian article URL, fetch its body text via the Content API.
|
28 |
"""
|
29 |
key = st.secrets["GUARDIAN_API_KEY"]
|
30 |
+
# strip off the public site domain to get the API path
|
31 |
+
prefix = "https://www.theguardian.com"
|
32 |
+
if url.startswith(prefix):
|
33 |
+
path = url[len(prefix):]
|
34 |
else:
|
35 |
raise ValueError(f"Unexpected URL format: {url}")
|
36 |
|
37 |
resp = requests.get(
|
38 |
f"{CONTENT_API}{path}",
|
39 |
params={
|
40 |
+
"api-key": key,
|
41 |
"show-fields": "bodyText"
|
42 |
}
|
43 |
)
|