#!/usr/bin/env python3
"""
Simple RSS Reader
"""
import rssparser
import feedparser
import json
import sys
from datetime import datetime

def read_feed(url):
    """Read RSS feed and return parsed items"""
    try:
        feed = feedparser.parse(url)
        return {
            'title': feed.get('title', 'Unknown'),
            'link': feed.get('link', ''),
            'description': feed.get('description', ''),
            'language': feed.get('language', ''),
            'items': [
                {
                    'title': item.get('title', 'No title'),
                    'link': item.get('link', ''),
                    'description': item.get('description', ''),
                    'pubDate': item.get('pubDate', ''),
                    'author': item.get('author', '')
                }
                for item in feed.get('items', [])[:10]  # Get first 10 items
            ]
        }
    except Exception as e:
        return {'error': str(e)}

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("Usage: python3 rss-reader.py <rss-url>")
        sys.exit(1)

    url = sys.argv[1]
    print(f"Fetching RSS feed: {url}\n")

    data = read_feed(url)

    if 'error' in data:
        print(f"Error: {data['error']}")
    else:
        print(f"Title: {data['title']}")
        print(f"Language: {data['language']}")
        print(f"Last updated: {data.get('description', 'N/A')[:100]}...\n")
        print(f"{'='*60}")
        print(f"Articles ({len(data['items'])} items)\n")
        print(f"{'='*60}\n")

        for i, item in enumerate(data['items'], 1):
            print(f"{i}. {item['title']}")
            print(f"   Link: {item['link']}")
            print(f"   Published: {item['pubDate']}")
            if item['author']:
                print(f"   Author: {item['author']}")
            print()