Spaces:
Running
Running
File size: 2,460 Bytes
7c3be27 24cf91c 7c3be27 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# gather_news.py
# News Source Integration
# This script integrates with various news sources to fetch the latest articles from the specified news sources, extracts relevant information such as title, URL,Source,Author and Publish date.
import requests
import feedparser
def fetch_articles_newsapi(topic):
"""
Fetch articles from NewsAPI based on the provided topic.
"""
url = 'https://newsapi.org/v2/everything'
params = {
'apiKey': api_key,
'language': 'en',
'q': topic,
'pageSize': 20
}
try:
response = requests.get(url, params=params)
if response.status_code != 200:
return f"Error: Failed to fetch news. Status code: {response.status_code}"
articles = response.json().get("articles", [])
if not articles:
return "No articles found."
# Extract relevant information from each article
extracted_articles = []
for article in articles:
extracted_articles.append({
"title": article.get("title", "No title"),
"url": article.get("url", "#"),
"source": article.get("source", {}).get("name", "Unknown"),
"author": article.get("author", "Unknown"),
"publishedAt": article.get("publishedAt", "Unknown")
})
return extracted_articles
except Exception as e:
return f"Error fetching news: {str(e)}"
def fetch_articles_google(topic):
"""
Fetch articles from Google News RSS feed based on the provided topic.
"""
rss_url = f'https://news.google.com/rss/search?q={topic}&hl=en-US&gl=US&ceid=US:en'
try:
feed = feedparser.parse(rss_url)
if not feed.entries:
return "No articles found."
# Extract relevant information from each article
extracted_articles = []
for entry in feed.entries[:20]: # Limit to top 20 articles
extracted_articles.append({
"title": entry.title,
"url": entry.link,
"source": entry.source.title if hasattr(entry, 'source') else "Unknown",
"author": entry.author if hasattr(entry, 'author') else "Unknown",
"publishedAt": entry.published if hasattr(entry, 'published') else "Unknown"
})
return extracted_articles
except Exception as e:
return f"Error fetching news: {str(e)}"
|