divination/divine.py
2025-12-20 22:55:26 -05:00

121 lines
No EOL
4 KiB
Python

import os
import feedparser
import requests
from bs4 import BeautifulSoup
import logging
from urllib.error import URLError
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def fetch_rss_feed(url):
try:
feed = feedparser.parse(url)
# Check if feed was successfully parsed
if feed.bozo:
logger.error(f"Feed parsing error: {feed.bozo_exception}")
return None
rss_items = []
ignored_links = ["https://divination.disinfo.zone/archives/",
"https://divination.disinfo.zone/about/"]
# Check if feed has entries
if not hasattr(feed, 'entries') or not feed.entries:
logger.error("No entries found in feed")
return None
for entry in feed.entries:
title = entry.title
link = entry.link
if link not in ignored_links:
rss_items.append((title, link))
# Verify we got some items
if not rss_items:
logger.error("No valid RSS items found")
return None
return rss_items
except Exception as e:
logger.error(f"Error fetching RSS feed: {str(e)}")
return None
def update_html_file(html_file, rss_items):
if not rss_items:
logger.error("No RSS items provided, skipping update")
return False
try:
with open(html_file, 'r', encoding='utf-8') as file:
html_content = file.read()
soup = BeautifulSoup(html_content, 'html.parser')
# Find the divinations paragraph
divinations_section = soup.find('p', {'class': 'divinations'})
if not divinations_section:
# Fallback to old structure for backwards compatibility
divinations_section = soup.find('div', {'id': 'rss-feed-content'})
if divinations_section:
# Old ul/li structure
ul_tag = soup.new_tag('ul')
for title, link in rss_items:
li = soup.new_tag('li')
a = soup.new_tag('a', href=link)
a.string = title
li.append(a)
ul_tag.append(li)
if ul_tag.find_all('li'):
divinations_section.clear()
divinations_section.append(ul_tag)
else:
logger.error("Could not find divinations section")
return False
else:
# New inline a tag structure for index-new5
divinations_section.clear()
for i, (title, link) in enumerate(rss_items):
a = soup.new_tag('a', href=link)
a.string = title
divinations_section.append(a)
# Add newline for readability in source
divinations_section.append('\n ')
# Write to temporary file first
temp_file = html_file + '.tmp'
with open(temp_file, 'w', encoding='utf-8') as file:
file.write(str(soup))
# If successful, rename temp file to actual file
os.replace(temp_file, html_file)
return True
except Exception as e:
logger.error(f"Error updating HTML file: {str(e)}")
return False
def main():
rss_feed_url = "https://divination.disinfo.zone/index.xml"
html_file = "/var/www/disinfo.zone/html/index.html"
# Fetch RSS items
rss_items = fetch_rss_feed(rss_feed_url)
# Only update if we successfully got items
if rss_items:
success = update_html_file(html_file, rss_items)
if success:
logger.info("Successfully updated HTML file")
else:
logger.error("Failed to update HTML file")
else:
logger.error("Failed to fetch RSS items")
if __name__ == "__main__":
main()