podcast-jobs / update-rss.py
fdaudens's picture
fdaudens HF Staff
mp3 gradio, rss logic
c301481
import xml.etree.ElementTree as ET
from datetime import datetime
import os
from app import generate_headline_and_description
def get_next_episode_number(podcast_dir="podcasts"):
files = [f for f in os.listdir(podcast_dir) if f.endswith(".wav")]
return len(files) + 1
def update_rss(subject, audio_url, audio_length, rss_path="rss.xml"):
# Generate headline and description automatically
title, description = generate_headline_and_description(subject)
tree = ET.parse(rss_path)
root = tree.getroot()
channel = root.find("channel")
# Update lastBuildDate
last_build_date = channel.find("lastBuildDate")
now_rfc2822 = datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S +0000")
if last_build_date is not None:
last_build_date.text = now_rfc2822
# Create new item
item = ET.Element("item")
ET.SubElement(item, "title").text = title
ET.SubElement(item, "description").text = description
ET.SubElement(item, "pubDate").text = now_rfc2822
ET.SubElement(item, "enclosure", url=audio_url, length=str(audio_length), type="audio/mpeg")
ET.SubElement(item, "guid").text = audio_url
ET.SubElement(item, "itunes:explicit").text = "false"
# Insert new item after lastBuildDate (i.e., as the first item)
# Find the first <item> and insert before it, or append if none exist
items = channel.findall("item")
if items:
channel.insert(list(channel).index(items[0]), item)
else:
channel.append(item)
# Write back to file
tree.write(rss_path, encoding="utf-8", xml_declaration=True)