Create guardian.py
Browse files- guardian.py +38 -0
guardian.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# guardian.py
|
| 2 |
+
import requests
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
BASE_URL = "https://content.guardianapis.com/search"
|
| 6 |
+
|
| 7 |
+
def fetch_headlines(section="world", limit=5):
|
| 8 |
+
"""
|
| 9 |
+
Returns a list of (headline, url) tuples from The Guardian.
|
| 10 |
+
"""
|
| 11 |
+
key = st.secrets["guardian"]["api_key"]
|
| 12 |
+
params = {
|
| 13 |
+
"api-key": key,
|
| 14 |
+
"section": section,
|
| 15 |
+
"page-size": limit,
|
| 16 |
+
"order-by": "newest",
|
| 17 |
+
"show-fields": "headline"
|
| 18 |
+
}
|
| 19 |
+
resp = requests.get(BASE_URL, params=params)
|
| 20 |
+
resp.raise_for_status()
|
| 21 |
+
results = resp.json()["response"]["results"]
|
| 22 |
+
return [(item["fields"]["headline"], item["webUrl"]) for item in results]
|
| 23 |
+
|
| 24 |
+
def fetch_full_article(url: str) -> str:
|
| 25 |
+
"""
|
| 26 |
+
Given a Guardian article URL, re-query its body text via the API.
|
| 27 |
+
"""
|
| 28 |
+
key = st.secrets["guardian"]["api_key"]
|
| 29 |
+
path = url.split("content.guardianapis.com")[-1]
|
| 30 |
+
resp = requests.get(
|
| 31 |
+
f"https://content.guardianapis.com{path}",
|
| 32 |
+
params={
|
| 33 |
+
"api-key": key,
|
| 34 |
+
"show-fields": "bodyText"
|
| 35 |
+
}
|
| 36 |
+
)
|
| 37 |
+
resp.raise_for_status()
|
| 38 |
+
return resp.json()["response"]["content"]["fields"]["bodyText"]
|