
- Add Base import to models/base.py to re-export it properly - Fix issue where models/__init__.py was trying to import Base from models/base.py - Add noqa comment to suppress unused import warning - Maintain the same model architecture but with correct imports
News Aggregation Service
A FastAPI application that aggregates news from various sources using the Mediastack API.
Features
- User authentication and authorization
- News article fetching and storage from Mediastack API
- Filtering news by keywords, sources, categories, countries, and languages
- User preferences for personalized news
- Save articles for later reading
- Background tasks for periodic news updates
Tech Stack
- FastAPI: High-performance web framework
- SQLite: Database for storing news articles and user data
- SQLAlchemy: ORM for database interactions
- Alembic: Database migration tool
- Pydantic: Data validation and settings management
- JWT: Token-based authentication
Requirements
- Python 3.8+
- Mediastack API key (required for fetching news data)
Setup
-
Clone the repository
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables:
# Required for fetching news from Mediastack API MEDIASTACK_API_KEY=your_api_key_here # Optional - For JWT authentication security # If not set, a secure random key will be generated on startup # but JWT tokens will be invalidated when the server restarts SECRET_KEY=your_secret_key_here
-
Run database migrations:
alembic upgrade head
-
Start the server:
uvicorn main:app --reload
API Endpoints
The API is documented with Swagger UI, available at /docs
when the server is running.
Main Endpoints
/api/v1/users/register
: Register a new user/api/v1/users/token
: Login and get JWT token/api/v1/news
: Get news articles with optional filtering/api/v1/news/personalized
: Get personalized news based on user preferences/api/v1/news/saved
: Save articles for later reading/health
: Health check endpoint
Project Structure
app/api
: API routes and endpointsapp/core
: Core functionality (config, security, utils)app/db
: Database connection and sessionapp/models
: SQLAlchemy modelsapp/schemas
: Pydantic schemas for validationapp/services
: Business logic servicesmigrations
: Database migrations
Security Notes
The application uses two distinct keys for different purposes:
-
Mediastack API Key: Used to authenticate with the external Mediastack news API service. This key is required to fetch news data.
-
Secret Key: Used internally for JWT token generation and verification in the authentication system. If not provided via the SECRET_KEY environment variable, a secure random key will be generated automatically on each startup. Note that this means all active JWT tokens will be invalidated when the server restarts.
Development
To run linting checks:
ruff check .
To fix linting issues automatically:
ruff check --fix .