newslens / src /ingestion /newsapi_client.py
Jitender20's picture
Add NewsLens Streamlit app
208266a
from src.db.vector_store import NewsVectorStore
from newsapi import NewsApiClient
from src.config import NEWS_API_KEY
def fetch_news(topic="AI regulation", lang="en", page_size=10):
if not NEWS_API_KEY:
raise RuntimeError("NEWSAPI_KEY is not configured. Add it to .env before using /ingest.")
news_instance = NewsApiClient(api_key=NEWS_API_KEY)
try:
print("Fetching latest articles...")
response = news_instance.get_everything(q=topic, language=lang, sort_by='relevancy', page_size=page_size)
if response['status'] == 'ok':
articles = response['articles']
if not articles:
print("No articles found.")
return
print(f"Successfully fetched {len(articles)} articles.")
print("-" * 40)
return articles
else:
print(f"API Error: {response.get('message', 'Unknown error')}")
return []
except Exception as e:
print(f"Pipeline failed: {str(e)}")
return []
def run_pipeline():
print("Fetching articles...")
articles = fetch_news()
if not articles:
print("No articles found.")
return
print(f"Fetched {len(articles)} articles.")
db = NewsVectorStore()
db.store_articles(articles)
print("Pipeline complete.")
if __name__ == "__main__":
run_pipeline()