Spaces:
Runtime error
Runtime error
Create services/news_service.py
Browse files- services/news_service.py +23 -0
services/news_service.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
|
| 3 |
+
API_KEY = 'your_rapidapi_key_here'
|
| 4 |
+
API_HOST = 'myallies-breaking-news-v1.p.rapidapi.com'
|
| 5 |
+
|
| 6 |
+
def get_market_news(category='general'):
|
| 7 |
+
"""
|
| 8 |
+
Fetch market news by category using a news API.
|
| 9 |
+
"""
|
| 10 |
+
url = f"https://{API_HOST}/GetTopNews"
|
| 11 |
+
headers = {
|
| 12 |
+
'x-rapidapi-key': API_KEY,
|
| 13 |
+
'x-rapidapi-host': API_HOST
|
| 14 |
+
}
|
| 15 |
+
params = {'category': category} # if supported by API
|
| 16 |
+
try:
|
| 17 |
+
response = requests.get(url, headers=headers, params=params)
|
| 18 |
+
response.raise_for_status()
|
| 19 |
+
news_data = response.json()
|
| 20 |
+
# Process and return relevant news items
|
| 21 |
+
return news_data.get('articles', []) # or adapt to your API's response
|
| 22 |
+
except Exception as e:
|
| 23 |
+
raise RuntimeError(f"Failed to fetch market news: {e}")
|