#!/usr/bin/env python3

import datetime
import feedparser
from jinja2 import Template
from bs4 import BeautifulSoup
import warnings
from bs4 import MarkupResemblesLocatorWarning

# URL of the RSS feed
rss_url = 'https://archives.arosworld.org/modules/rssfeed.php'

# Parse the RSS feed
feed = feedparser.parse(rss_url)

# Suppress MarkupResemblesLocatorWarning
warnings.filterwarnings("ignore", category=MarkupResemblesLocatorWarning)

# Preprocess entries to clean titles using BeautifulSoup
for entry in feed.entries:
    entry.cleaned_title = BeautifulSoup(str(entry.title), 'html.parser').get_text()


# HTML template
html_template = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>RSS Feed</title>
    <meta name="lastupdate" content="{{ date }}">
</head>
<body style="font-size: 13px">
    <!-- This file is automatically generated by the AROS/scripts/updatearchives script -->
    {% for entry in entries %}
        <a href="{{ entry.link }}" target="_top">{{ entry.cleaned_title }}</a></br>
    {% endfor %}
</body>
</html>
"""

# Render the HTML
template = Template(html_template)
html_content = template.render(feed=feed.feed, entries=feed.entries, date=datetime.datetime.now())

# Write the HTML content to a file
with open('/home/project-web/aros/htdocs/archives.html', 'w', encoding='utf-8') as f:
    f.write(html_content)
