Spaces:
Sleeping
Sleeping
Update web_ingest.py
Browse files- web_ingest.py +15 -21
web_ingest.py
CHANGED
|
@@ -1,31 +1,25 @@
|
|
| 1 |
-
# web_ingest.py
|
| 2 |
-
|
| 3 |
import requests
|
| 4 |
from datetime import datetime
|
| 5 |
|
| 6 |
-
|
| 7 |
-
PUBLIC_SOURCES = {
|
| 8 |
"gold_price": "https://api.metals.live/v1/spot/gold",
|
| 9 |
-
"oil_price": "https://api.oilpriceapi.com/v1/prices/latest", # example
|
| 10 |
}
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
-
r = requests.get(
|
| 15 |
r.raise_for_status()
|
| 16 |
-
return
|
| 17 |
-
"source": name,
|
| 18 |
-
"timestamp": datetime.utcnow().isoformat(),
|
| 19 |
-
"data": r.json()
|
| 20 |
-
}
|
| 21 |
except Exception as e:
|
| 22 |
-
return {
|
| 23 |
-
"source": name,
|
| 24 |
-
"error": str(e)
|
| 25 |
-
}
|
| 26 |
|
| 27 |
-
def
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
from datetime import datetime
|
| 3 |
|
| 4 |
+
PUBLIC_ENDPOINTS = {
|
|
|
|
| 5 |
"gold_price": "https://api.metals.live/v1/spot/gold",
|
|
|
|
| 6 |
}
|
| 7 |
|
| 8 |
+
NEWS_ENDPOINTS = [
|
| 9 |
+
"https://newsapi.org/v2/top-headlines?category=business&pageSize=20&apiKey=demo"
|
| 10 |
+
]
|
| 11 |
+
|
| 12 |
+
def fetch_json(url):
|
| 13 |
try:
|
| 14 |
+
r = requests.get(url, timeout=5)
|
| 15 |
r.raise_for_status()
|
| 16 |
+
return r.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
except Exception as e:
|
| 18 |
+
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
def ingest():
|
| 21 |
+
return {
|
| 22 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 23 |
+
"prices": {k: fetch_json(v) for k, v in PUBLIC_ENDPOINTS.items()},
|
| 24 |
+
"news": [fetch_json(url) for url in NEWS_ENDPOINTS],
|
| 25 |
+
}
|