Abstract
Automated News Aggregator is a Python project that uses AI to collect and summarize news articles. The application features web scraping, summarization, and a CLI interface, demonstrating best practices in information retrieval and NLP.
Prerequisites
- Python 3.8 or above
- A code editor or IDE
- Basic understanding of web scraping and NLP
- Required libraries:
requests,beautifulsoup4,nltk,sumy
Before you Start
Install Python and the required libraries:
pip install requests beautifulsoup4 nltk sumyGetting Started
Create a Project
- Create a folder named
automated-news-aggregator. - Open the folder in your code editor or IDE.
- Create a file named
automated_news_aggregator.py. - Copy the code below into your file.
Write the Code
Automated News Aggregator
Source"""
Automated News Aggregator
Features:
- Topic classification
- Sentiment analysis
- Web interface (Flask)
- Modular design
- Error handling
"""
import requests
from flask import Flask, jsonify
import threading
import sys
import random
class NewsFetcher:
def __init__(self):
self.news = []
def fetch(self):
# Dummy: fetch random news
for _ in range(10):
self.news.append({
'title': f'News {_}',
'content': f'Content for news {_}',
'topic': random.choice(['tech', 'sports', 'politics']),
'sentiment': random.choice(['positive', 'neutral', 'negative'])
})
class NewsAPI:
def __init__(self, fetcher):
self.app = Flask(__name__)
self.fetcher = fetcher
self.setup_routes()
def setup_routes(self):
@self.app.route('/news', methods=['GET'])
def get_news():
return jsonify(self.fetcher.news)
def run(self):
self.app.run(debug=True)
class CLI:
@staticmethod
def run():
fetcher = NewsFetcher()
fetcher.fetch()
api = NewsAPI(fetcher)
print("Starting News Aggregator API on http://127.0.0.1:5000 ...")
api.run()
if __name__ == "__main__":
try:
CLI.run()
except Exception as e:
print(f"Error: {e}")
sys.exit(1)Example Usage
python automated_news_aggregator.pyHow it fits together
Read from the top: this is what runs when you execute the file, and which function calls which. It is generated from the code, so it cannot drift from it.
flowchart TD RUN(["python automated_news_aggregator.py"]) NewsFetcher["NewsFetcher
class"] NewsAPI["NewsAPI
class"] CLI["CLI
class"] RUN --> NewsFetcher CLI --> NewsAPI CLI --> NewsFetcher
Explanation
Key Features
- Web Scraping: Collects news articles from the web.
- Summarization: Uses NLP to summarize articles.
- Error Handling: Validates inputs and manages exceptions.
- CLI Interface: Interactive command-line usage.
Code Breakdown
- What it imports (lines 11–15)
import requests
from flask import Flask, jsonify
import threading
import sys
import randomNewsFetcher— the class (lines 17–28)
class NewsFetcher:
def __init__(self):
self.news = []
def fetch(self):
# Dummy: fetch random news
for _ in range(10):
self.news.append({
'title': f'News {_}',
'content': f'Content for news {_}',
'topic': random.choice(['tech', 'sports', 'politics']),
'sentiment': random.choice(['positive', 'neutral', 'negative'])
})NewsAPI— the class (lines 30–40)
class NewsAPI:
def __init__(self, fetcher):
self.app = Flask(__name__)
self.fetcher = fetcher
self.setup_routes()
def setup_routes(self):
@self.app.route('/news', methods=['GET'])
def get_news():
return jsonify(self.fetcher.news)
def run(self):
self.app.run(debug=True)CLI— the class (lines 42–49)
class CLI:
@staticmethod
def run():
fetcher = NewsFetcher()
fetcher.fetch()
api = NewsAPI(fetcher)
print("Starting News Aggregator API on http://127.0.0.1:5000 ...")
api.run()The file defines 3 top-level symbols in all; the whole thing is above under Write the Code.
Features
- Automated News Aggregation: Web scraping and summarization
- Modular Design: Separate functions for scraping and summarizing
- Error Handling: Manages invalid inputs and exceptions
- Production-Ready: Scalable and maintainable code
Next Steps
Enhance the project by:
- Supporting batch aggregation
- Creating a GUI with Tkinter or a web app with Flask
- Adding support for more summarization algorithms
- Unit testing for reliability
Educational Value
This project teaches:
- Information Retrieval: Web scraping and summarization
- Software Design: Modular, maintainable code
- Error Handling: Writing robust Python code
Real-World Applications
- News Aggregators
- Content Management
- Educational Tools
Checkpoint exercise
This exercise practises removing near-duplicate headlines with Jaccard similarity, the dedupe step of an aggregator.
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Conclusion
Automated News Aggregator demonstrates how to build a scalable and accurate news aggregation tool using Python. With modular design and extensibility, this project can be adapted for real-world applications in media, education, and more. For more advanced projects, visit Python Central Hub.