šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
DemoInstallSign in
Socket

finance-news-api

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

finance-news-api

A powerful API that aggregates financial news and provides comprehensive market sentiment analysis

1.0.8
PyPI
Maintainers
1

Finance News API

A powerful API that aggregates financial news and provides comprehensive market sentiment analysis.

Features

  • šŸ” Comprehensive Data Sources

    • Yahoo Finance - Real-time market data and news
    • Reuters - Global financial news and analysis
    • Bloomberg - Market insights and breaking news
    • MarketWatch - Financial news and market commentary
    • Seeking Alpha - In-depth analysis and research
    • Company information and fundamentals
    • Market metrics and analyst recommendations
  • šŸ“Š Advanced Analytics

    • Sentiment analysis with granular classification
    • Subjectivity analysis
    • Time-based sentiment trends
    • Market sentiment aggregation
    • Multi-source news comparison and analysis
    • Source-specific sentiment tracking
  • šŸ“ˆ Market Data

    • Real-time and historical price data
    • Volume analysis
    • Key market metrics (P/E ratio, market cap, etc.)
    • Analyst recommendations
    • Company fundamentals
  • ⚔ Performance Features

    • Async processing
    • Input validation
    • Comprehensive error handling
    • Flexible filtering options
    • Customizable news sources

Getting Started

Prerequisites

  • Python 3.8+
  • pip (Python package manager)

Installation

The easiest way to install Finance News API is directly from PyPI:

pip install finance-news-api

Option 2: Install from Source

  • Clone the repository:
git clone https://github.com/pratyushkhanal/finance-news-api.git
cd finance-news-api
  • Install dependencies:
pip install -r requirements.txt

Basic Usage

After installing the package, you can use it in your Python code:

from newsapi2 import app
import uvicorn

# Run the FastAPI application
uvicorn.run(app, host="0.0.0.0", port=8000)

You can also import specific services and use them directly:

from newsapi2.services.news_sources import get_news
from newsapi2.services.sentiment import analyze_sentiment

# Get news for a specific stock
apple_news = get_news("AAPL")

# Analyze sentiment of a text
sentiment_score = analyze_sentiment("This company reported better than expected earnings")

API Client Usage

If you want to use the Finance News API as a client:

import requests

headers = {
    "X-API-Key": "your_api_key_here"
}

# Get news for Apple
response = requests.get(
    "http://your-api-endpoint.com/api/news/AAPL",
    headers=headers,
    params={
        "sentiment_threshold": 0.2, 
        "time_range_hours": 24,
        "sources": ["reuters", "bloomberg"]
    }
)

# Parse the JSON response
news_data = response.json()

# Print the results
for article in news_data["articles"]:
    print(f"Title: {article['title']}")
    print(f"Source: {article['source']}")
    print(f"Sentiment: {article['sentiment_score']}")
    print(f"URL: {article['url']}")
    print("---")

Upgrading the Package

To upgrade to the latest version:

pip install --upgrade finance-news-api

Command-Line Usage

After installing the package, you can run it directly from the command line:

# Start the API server (if configured in entry_points)
finance-news-api

# Or run it using Python
python -m newsapi2.main

This will start the FastAPI application on the default host (0.0.0.0) and port (8000).

Running as a Web Service

You can also run the Finance News API as a web service:

  • Set up environment variables:
# Create a .env file
touch .env

# Add the following to your .env file
ADMIN_USERNAME=your_admin_username
ADMIN_PASSWORD=your_secure_password
  • Run the application:
# If installed from PyPI
finance-news-api serve

# Or if using the source code
uvicorn main:app --reload

The API will be available at http://localhost:8000

API Key Management

The API includes a built-in admin interface for managing API keys. Access it at http://localhost:8000/admin

Security

  • Admin interface is protected with HTTP Basic Authentication
  • Admin credentials are configured via environment variables
  • All admin actions require authentication
  • API keys are securely generated and stored

Features

  • Create new users and generate API keys
  • View all existing users and their API keys
  • Delete users and their API keys
  • Copy API keys to clipboard

Using the Admin Interface

  • Open http://localhost:8000/admin in your browser
  • Enter your admin credentials when prompted
  • Use the form to create new users
  • View and manage existing users in the table
  • Copy API keys using the "Copy" button
  • Delete users using the "Delete" button

API Usage

Authentication

All endpoints require API key authentication. Include your API key in the X-API-Key header:

import requests

headers = {
    "X-API-Key": "your_api_key_here"
}

response = requests.get("http://localhost:8000/api/news/AAPL", headers=headers)

Example Endpoints

Get News for a Stock

response = requests.get(
    "http://localhost:8000/api/news/AAPL",
    params={
        "sentiment_threshold": 0.2,
        "time_range_hours": 24,
        "sources": ["reuters", "bloomberg"]
    },
    headers=headers
)

Documentation

  • API Documentation: /docs (Swagger UI)
  • ReDoc Documentation: /redoc
  • Full API Documentation: API.md

Development

Running Tests

pytest

Code Style

The project follows PEP 8 style guidelines. To check your code:

flake8 .

Contributing

  • Fork the repository
  • Create a feature branch
  • Commit your changes
  • Push to the branch
  • Create a Pull Request

Project Structure

finance-news-api/
ā”œā”€ā”€ main.py              # FastAPI application entry point
ā”œā”€ā”€ routers/            # API route handlers
│   ā”œā”€ā”€ news.py         # News endpoints
│   ā”œā”€ā”€ users.py        # User registration
│   └── admin.py        # Admin interface
ā”œā”€ā”€ services/           # Business logic
│   ā”œā”€ā”€ yahoo.py        # Yahoo Finance integration
│   ā”œā”€ā”€ news_sources.py # News source integrations
│   ā”œā”€ā”€ sentiment.py    # Sentiment analysis
│   └── google.py       # Google News integration
ā”œā”€ā”€ templates/          # HTML templates
│   └── admin.html      # Admin interface template
ā”œā”€ā”€ middleware/         # Middleware components
│   └── admin_auth.py   # Admin authentication
ā”œā”€ā”€ database.py         # Database configuration
ā”œā”€ā”€ models.py           # SQLAlchemy models
ā”œā”€ā”€ schemas.py          # Pydantic models
ā”œā”€ā”€ auth.py            # API key authentication
ā”œā”€ā”€ tests/              # Test files
ā”œā”€ā”€ .env               # Environment variables
ā”œā”€ā”€ requirements.txt   # Project dependencies
ā”œā”€ā”€ API.md            # API documentation
└── README.md         # Project documentation

Dependencies

  • FastAPI - Web framework
  • Uvicorn - ASGI server
  • YFinance - Yahoo Finance API
  • TextBlob - Sentiment analysis
  • BeautifulSoup4 - Web scraping
  • Feedparser - RSS feed parsing
  • Playwright - Browser automation
  • Python-dotenv - Environment management

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Support

For support or to request a new API key, please contact:

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts