Mines-Paris-Alumni / nominations

0 stars 0 forks source link

Generating a feed in the same format #1

Open theophilec opened 8 months ago

theophilec commented 8 months ago
from lxml import etree
from datetime import datetime

# Create the root element
root = etree.Element("rss", version="2.0",
    xmlns:dc="http://purl.org/dc/elements/1.1/",
    xmlns:atom="http://www.w3.org/2005/Atom",
    xmlns:content="http://purl.org/rss/1.0/modules/content/",
    xmlns:nomination="http://purl.org/rss/1.0/modules/content/")

# Create the channel element
channel = etree.SubElement(root, "channel")

# Add channel elements
etree.SubElement(channel, "title").text = "Your Title"
etree.SubElement(channel, "description").text = "Your Description"
etree.SubElement(channel, "pubDate").text = datetime.now().strftime("%a, %d %b %Y %H:%M:%S +0000")
etree.SubElement(channel, "generator").text = "Your Generator"
etree.SubElement(channel, "link").text = "https://www.yourlink.com"
etree.SubElement(channel, "author").text = "your-email@example.com (Your Name)"
etree.SubElement(channel, "copyright").text = "© 2022 Your Name"
dc_creator = etree.SubElement(channel, "{http://purl.org/dc/elements/1.1/}creator")
dc_creator.text = "Your Name"
atom_link = etree.SubElement(channel, "{http://www.w3.org/2005/Atom}link")
atom_link.attrib["href"] = "/your-link"
atom_link.attrib["rel"] = "self"
atom_link.attrib["type"] = "application/rss+xml"

# Add item elements
item = etree.SubElement(channel, "item")
etree.SubElement(item, "title").text = "Your Item Title"
description = etree.SubElement(item, "description")
description.text = "Your Item Description"
etree.SubElement(item, "pubDate").text = datetime.now().strftime("%Y-%m-%dT%H:%M:%S+00:00")
etree.SubElement(item, "guid", isPermaLink="true").text = "https://www.yourlink.com/your-item"
enclosure = etree.SubElement(item, "enclosure")
enclosure.attrib["length"] = "7109"
enclosure.attrib["type"] = "image/jpeg"
enclosure.attrib["url"] = "https://www.yourlink.com/your-image.jpg"

# Add nomination elements
nomination_textlength = etree.SubElement(item, "{http://purl.org/rss/1.0/modules/content/}textlength")
nomination_textlength.text = "638"
category_textlength = etree.SubElement(item, "category", domain="textlength")
category_textlength.text = "638"

# ... add other nomination elements as needed ...

# Convert the XML tree to a string
xml_string = etree.tostring(root, pretty_print=True, xml_declaration=True, encoding='UTF-8').decode('UTF-8')

# Write the XML string to a file
with open("your_feed.xml", "w") as f:
    f.write(xml_string)
theophilec commented 8 months ago

What about CDATA?