Automated Action 90c1cdef34 Setup News Aggregation Service
- Fix code linting issues
- Update README with detailed documentation
- Configure database paths for the current environment
- Create necessary directory structure

The News Aggregation Service is now ready to use with FastAPI and SQLite.
2025-05-27 18:50:11 +00:00

80 lines
2.4 KiB
Python

import asyncio
import logging
from typing import List, Optional
from app.db.session import SessionLocal
from app.services.news import fetch_and_store_news
logger = logging.getLogger(__name__)
async def refresh_news_periodically(
interval_minutes: int = 60,
categories: Optional[List[str]] = None,
countries: Optional[List[str]] = None,
languages: Optional[List[str]] = None,
):
"""
Periodically fetch and store news from the Mediastack API.
Args:
interval_minutes: The interval in minutes between fetches.
categories: A list of categories to fetch news for.
countries: A list of countries to fetch news for.
languages: A list of languages to fetch news for.
"""
categories_str = None
if categories:
categories_str = ",".join(categories)
countries_str = None
if countries:
countries_str = ",".join(countries)
languages_str = None
if languages:
languages_str = ",".join(languages)
while True:
try:
logger.info("Refreshing news...")
db = SessionLocal()
try:
await fetch_and_store_news(
db,
categories=categories_str,
countries=countries_str,
languages=languages_str,
limit=100,
)
logger.info("News refresh completed successfully.")
finally:
db.close()
# Sleep for the specified interval
await asyncio.sleep(interval_minutes * 60)
except Exception as e:
logger.error(f"Error refreshing news: {e}")
# If an error occurs, wait a shorter time before retrying
await asyncio.sleep(300) # 5 minutes
async def start_background_tasks():
"""
Start background tasks for the application.
"""
# Default categories, countries, and languages to fetch news for
categories = ["general", "business", "technology", "entertainment", "health", "science", "sports"]
countries = ["us", "gb", "ca", "au"] # US, UK, Canada, Australia
languages = ["en"] # English only
# Start the background task
asyncio.create_task(
refresh_news_periodically(
interval_minutes=60,
categories=categories,
countries=countries,
languages=languages,
)
)