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.