Simple RSS Feed Reader
This utility fetches and displays the latest articles from an RSS or Atom feed. It uses the feedparser library to handle the complexities of different feed formats and webbrowser to optionally open links.
Modules Used:
feedparser: A powerful library for parsing RSS and Atom feeds.- argparse: To handle command-line arguments.
webbrowser: To open article links in the default web browser.
Installation
pip install feedparser
The Code
Save this as rss_reader.py.
import feedparser
import argparse
import webbrowser
import sys
def read_feed(url, limit=5, open_browser=False):
print(f"Fetching feed from: {url}...")
# Parse the feed
# feedparser handles HTTP requests internally
feed = feedparser.parse(url)
# Check for parsing errors (bozo bit)
# Note: feedparser often returns usable data even if the XML is malformed
if feed.bozo:
print("Warning: Feed might be malformed or invalid.")
if not feed.entries:
print("No entries found in this feed. Check the URL.")
return
# Print Feed Title
feed_title = feed.feed.get('title', 'Unknown Feed')
print(f"\n--- {feed_title} ---\n")
# Iterate through entries
display_count = min(limit, len(feed.entries))
for i, entry in enumerate(feed.entries[:display_count]):
title = entry.get('title', 'No Title')
link = entry.get('link', '')
published = entry.get('published', entry.get('updated', 'N/A'))
print(f"{i+1}. {title}")
print(f" Date: {published}")
print(f" Link: {link}")
print("-" * 40)
# Interactive mode to open links
if open_browser:
while True:
choice = input(f"\nEnter number to open (1-{display_count}) or 'q' to quit: ")
if choice.lower() in ['q', 'exit']:
break
try:
idx = int(choice) - 1
if 0 <= idx < display_count:
link = feed.entries[idx].get('link')
if link:
print(f"Opening: {link}")
webbrowser.open(link)
else:
print("Error: No link available for this entry.")
else:
print("Invalid number.")
except ValueError:
print("Please enter a valid number.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Simple RSS Feed Reader")
parser.add_argument("url", help="URL of the RSS feed")
parser.add_argument("-l", "--limit", type=int, default=5, help="Number of articles to show (default: 5)")
parser.add_argument("--open", action="store_true", help="Interactive mode to open articles in browser")
args = parser.parse_args()
read_feed(args.url, args.limit, args.open)
Usage
# Read Hacker News RSS
python rss_reader.py https://news.ycombinator.com/rss
# Read Reddit Python and open links interactively
python rss_reader.py https://www.reddit.com/r/python/.rss --limit 10 --open