apple-touch-icon@2.png


Currently, my rss script generates a rss feed for a website using an API. It worked until I forgot that some special characters aren’t allowed in xml format. What is the best way to get rid of or escape the & symbol?

Here’s the code:

import requests
import os


def generate_rss(data):
    rss = """\
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">

<channel>
<title>ComicK - RSS Feed</title>
<link>https://github.com/ld3z/manga-rss</link>
<description>A simple RSS feed for ComicK!</description>
"""

    for i in data:
        c = i["md_comics"]

        rss += """\
<item>
    <title>{}</title>
    <link>{}</link>
    <description>{}</description>
</item>
""".format(
            f"{c['title']} - Chapter {i['chap']}",
            f"https://comick.app/comic/{c['slug']}",
            f"Chapter {i['chap']} of {c['title']} is now available on ComicK!",
        )

    rss += "\n</channel>\n</rss>"
    return rss


url = "https://api.comick.app/chapter/?lang=en&page=1&order=new&accept_mature_content=true"
data = requests.get(url).json()

filename = f"./comick/comick-rss-nsfw.xml"

os.makedirs(os.path.dirname(filename), exist_ok=True)

with open(filename, "w", encoding="utf-8") as f_out:
    print(generate_rss(data), file=f_out)

I think it would have to be put into a list, but then I am not entirely sure if it would still be the same.



Source link

By Ajay Kumar Verma

techopenion is the one stop solution of all your information needs. Get everything from latest tech updates to latest gadgets in technology.

Leave a Reply

Your email address will not be published. Required fields are marked *