How can I get data from a JSON response and move it into a string to make an rss feed?

Question:

So over the past couple days, I have been making this code to create an RSS feed for a website that doesn’t have one using their public api. I have been having issues trying to create the feed since I’m not sure what the correct way to get the data from it and successfully generate the feed.

Here’s the code for clarification:

import requests
import json
import os


def get_latest():
    link = "https://api.comick.app/chapter/?page=1&order=new&accept_mature_content=true"
    response = requests.get(
        link,
        headers={
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36"
        },
    )
    if response.status_code != 200:
        raise Exception(
            f"Error getting data from {link}. Status code: {response.status_code}"
        )
    data = json.loads(response.text)
    chapters = {}
    for item in data:
        if "md_comics" in item and "title" in item:
            title = item.get("title")
            chapter = item.get("chap")
            md_comic = item["md_comics"]
            if "slug" in md_comic:
                slug = md_comic["slug"]
                chapters[f"{slug}-{chapter}"] = {
                    "title": title,
                    "slug": slug,
                    "chapter": chapter,
                }
    return chapters


def generate_rss(chapters):
    rss = f"""
<?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 key, value in chapters.items():
        rss += """
<item>
    <title>{}</title>
    <link>{}</link>
    <description>{} by {}</description>
</item>
""".format(
            f"{value['title']} - Chapter {value['chapter']}",
            "https://comick.app/comic/{}".format(value["slug"]),
            f"Chapter {value['chapter']} of {value['title']} is now available on ComicK!",
        )

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


try:
    chapters = get_latest()
    rss = generate_rss(chapters)
    filename = f"./comick/comick-rss.xml"
    os.makedirs(os.path.dirname(filename), exist_ok=True)
    if os.path.exists(filename):
        os.remove(filename)
    with open(filename, "w") as f:
        f.write(rss.strip())
except Exception as e:
    print(f"Comick failed: {e}")

Any help would be appreciated.

So far as it stands, I have tried putting it into lists, dictionaries, and other said types, and kept getting TypeError upon type error until I figured out what the issue was. Now the script creates the file but there aren’t any items in the file since the script (I believe) doesn’t actually put the data anywhere.

Asked By: ldez

||

Answers:

Try:

import requests


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/?page=1&order=new&accept_mature_content=true"
data = requests.get(url).json()


with open('rss.xml', 'w') as f_out:
    print(generate_rss(data), file=f_out)

This will generate rss.xml with contents:

<?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>
<item>
    <title>Hongyue Start - Chapter 46</title>
    <link>https://comick.app/comic/since-the-red-moon-appeared</link>
    <description>Chapter 46 of Hongyue Start is now available on ComicK!</description>
</item>
<item>
    <title>Black Lagoon - Chapter 114</title>
    <link>https://comick.app/comic/black-lagoon</link>
    <description>Chapter 114 of Black Lagoon is now available on ComicK!</description>
</item>
<item>
    <title>Darkness of the Sea, Shadow of the Moon - Chapter 88</title>
    <link>https://comick.app/comic/umi-no-yami-tsuki-no-kage</link>
    <description>Chapter 88 of Darkness of the Sea, Shadow of the Moon is now available on ComicK!</description>
</item>

...and so on.
Answered By: Andrej Kesely
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.