Skip to content

Latest commit

Β 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

News Analytics Pipeline with Elasticsearch & Kibana

A complete news monitoring and analytics system using newsdata.io API and serverless Elasticsearch with Kibana dashboards.

alt-name alt-name-2

🎯 Features

  • Fetch news articles from newsdata.io API
  • Index articles into Elasticsearch
  • Sentiment analysis on news content
  • Kibana dashboards for visualization
  • Deduplication to avoid indexing duplicate articles
  • Error handling and logging
  • Configurable topics and categories

πŸ“‹ Prerequisites

  • Python 3.8+
  • Serverless Elasticsearch instance (Elastic Cloud)
  • newsdata.io API key
  • Kibana access (comes with Elastic Cloud)

πŸš€ Quick Start

1. Clone the Repository

cd news-elasticsearch-pipeline

2. Install Dependencies

pip install -r requirements.txt

3. Configure Environment Variables

Copy the example environment file and fill in your credentials:

cp .env.example .env

Edit .env file with your credentials:

# newsdata.io API
NEWSDATA_API_KEY=your_newsdata_api_key_here

# Elasticsearch Configuration
ELASTIC_CLOUD_ID=your_cloud_id_here
ELASTIC_API_KEY=your_api_key_here

# Or use endpoint + credentials
ELASTIC_ENDPOINT=https://your-deployment.es.us-central1.gcp.cloud.es.io
ELASTIC_USERNAME=elastic
ELASTIC_PASSWORD=your_password_here

# Application Settings
INDEX_NAME=news-articles
LOG_LEVEL=INFO

4. Run Initial Setup

This creates the Elasticsearch index with proper mapping:

python scripts/setup_index.py

5. Fetch and Index News

python scripts/fetch_news.py

6. Access Kibana Dashboard

  1. Log into your Elastic Cloud Kibana instance
  2. Go to Management β†’ Stack Management β†’ Kibana β†’ Saved Objects
  3. Import the dashboard: kibana/news_dashboard.ndjson
  4. View your dashboard under Analytics β†’ Dashboard

πŸ“ Project Structure

news-elasticsearch-pipeline/
β”œβ”€β”€ config/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ settings.py          # Configuration management
β”‚   └── elasticsearch.py     # ES connection setup
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ news_fetcher.py      # newsdata.io API client
β”‚   β”œβ”€β”€ indexer.py           # Elasticsearch indexing logic
β”‚   β”œβ”€β”€ sentiment.py         # Sentiment analysis
β”‚   └── utils.py             # Helper functions
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ setup_index.py       # Create ES index
β”‚   β”œβ”€β”€ fetch_news.py        # Main script to fetch and index
β”‚   └── delete_index.py      # Utility to delete index
β”œβ”€β”€ kibana/
β”‚   └── news_dashboard.ndjson # Pre-built Kibana dashboard
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ test_news_fetcher.py
β”‚   └── test_indexer.py
β”œβ”€β”€ logs/                    # Application logs (auto-created)
β”œβ”€β”€ .env.example             # Environment variables template
β”œβ”€β”€ .gitignore
β”œβ”€β”€ requirements.txt
└── README.md

πŸ”§ Configuration

Topics and Categories

Edit config/settings.py to customize what news to fetch:

SEARCH_QUERIES = [
    'artificial intelligence',
    'machine learning',
    'cryptocurrency',
    'climate change'
]

CATEGORIES = ['technology', 'business', 'science', 'politics']
COUNTRIES = ['us', 'gb', 'in']
LANGUAGES = ['en']

πŸ“Š Kibana Dashboards

The included dashboard provides:

  • Article Timeline: Publications over time
  • Category Distribution: Pie chart of news categories
  • Sentiment Analysis: Positive/Negative/Neutral breakdown
  • Top Sources: Most active news sources
  • Trending Keywords: Word cloud of popular terms
  • Recent Articles: Table of latest indexed news

πŸ”„ Scheduling (Optional)

Using Cron (Linux/Mac)

# Edit crontab
crontab -e

# Add this line to run every 6 hours
0 */6 * * * cd /path/to/news-elasticsearch-pipeline && /usr/bin/python3 scripts/fetch_news.py >> logs/cron.log 2>&1

Using Task Scheduler (Windows)

  1. Open Task Scheduler
  2. Create Basic Task
  3. Set trigger (daily, hourly, etc.)
  4. Action: Start a program
  5. Program: python
  6. Arguments: scripts/fetch_news.py
  7. Start in: C:\path\to\news-elasticsearch-pipeline

πŸ§ͺ Testing

Run tests:

pytest tests/

πŸ“ Usage Examples

Fetch Specific Topic

from src.news_fetcher import NewsFetcher

fetcher = NewsFetcher()
articles = fetcher.fetch_news(query='bitcoin', country='us')
print(f"Found {len(articles)} articles")

Index Custom Articles

from src.indexer import NewsIndexer

indexer = NewsIndexer()
indexer.index_articles(articles)

Analyze Sentiment

from src.sentiment import analyze_sentiment

text = "This is great news for the technology sector!"
sentiment = analyze_sentiment(text)
print(sentiment)  # 'positive'

πŸ› Troubleshooting

Connection Issues

If you get connection errors:

  • Verify your Elasticsearch credentials in .env
  • Check if your IP is whitelisted in Elastic Cloud
  • Ensure your Elastic deployment is running

API Rate Limits

newsdata.io free tier limits:

  • 200 requests per day
  • Implement caching or reduce fetch frequency

Index Already Exists

# Delete and recreate index
python scripts/delete_index.py
python scripts/setup_index.py

πŸ“š API Documentation

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

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

⚠️ Disclaimer

This project is for educational purposes. Make sure to comply with:

  • newsdata.io Terms of Service
  • Elasticsearch Cloud usage policies
  • News source copyright and attribution requirements

πŸ™ Acknowledgments

πŸ“§ Contact

For questions or support, please open an issue on GitHub.


Happy News Monitoring! πŸ“°βœ¨

About

A Python-powered news monitoring system that fetches real-time articles from newsdata.io API and indexes them into Elasticsearch. Features automated sentiment analysis, smart deduplication, and beautiful Kibana dashboards. Perfect for tracking news trends across topics and sources.

Topics

Resources

Contributing

Stars

2 stars

Watchers

0 watching

Forks

Contributors

Languages