blankx.gitlab.io/gensbbsrss.py

46 lines
2.0 KiB
Python

import os
from datetime import datetime
from xml.etree import ElementTree
from bs4 import BeautifulSoup
daydict = {1: 'Mon', 2: 'Tue', 3: 'Wed', 4: 'Thu', 5: 'Fri', 6: 'Sat', 7: 'Sun'}
monthdict = {1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun', 7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec'}
with open('index.html') as file:
soup = BeautifulSoup(file.read())
sbbsbase = soup.find('link', rel='canonical')['href']
feed = ElementTree.Element('rss', version='2.0')
channel = ElementTree.SubElement(feed, 'channel')
title = ElementTree.SubElement(channel, 'title')
title.text = 'software being broken showcase'
link = ElementTree.SubElement(channel, 'link')
link.text = sbbsbase
description = ElementTree.SubElement(channel, 'description')
description.text = 'just a showcase of software being broken :)'
language = ElementTree.SubElement(channel, 'language')
language.text = 'en-us'
items = []
for i in os.listdir('.'):
if i in ('index.html', 'index.xml'):
continue
with open(i) as file:
soup = BeautifulSoup(file.read())
item = ElementTree.Element('item')
title = ElementTree.SubElement(item, 'title')
title.text = soup.h1.string
link = ElementTree.SubElement(item, 'link')
link.text = soup.find('link', rel='canonical')['href']
guid = ElementTree.SubElement(item, 'guid')
guid.text = f'sbbs/{os.path.splitext(i)[0]}'
description = ElementTree.SubElement(item, 'description')
description.text = str(soup.find('div', class_='content'))
items.append((datetime.fromisoformat(soup.find('meta', {'name': 'created'})['content']), item))
for date, i in sorted(items, reverse=True, key=lambda i: i[0]):
# http://johnbokma.com/blog/2019/10/09/rfc-822-and-rfc-3339-dates-in-python.html
ctime = date.ctime()
pubdate = ElementTree.SubElement(i, 'pubDate')
pubdate.text = f"{ctime[0:3]}, {date.day:02d} {ctime[4:7]} {date.strftime(' %Y %H:%M:%S %z')}"
channel.append(i)
ElementTree.ElementTree(feed).write('index.xml', 'utf-8')