feed.py 839 B

123456789101112131415161718192021222324
  1. import feedparser
  2. # https://feedparser.readthedocs.io/en/latest/
  3. def check_feed(channel_id):
  4. """Accepts YT channel id and returns dict: channel info (title and url) and new video items (15) (id, link,
  5. title) """
  6. url = f"https://www.youtube.com/feeds/videos.xml?channel_id={channel_id}"
  7. feed = feedparser.parse(url)
  8. list_of_videos = []
  9. try:
  10. channel_info = {"channel_title": feed["channel"]["title"], "channel_id": channel_id}
  11. for entry in feed.entries:
  12. list_of_videos.append({
  13. "id": entry["yt_videoid"],
  14. "link": entry["link"],
  15. "title": entry["title"]
  16. })
  17. except KeyError:
  18. channel_info = {"channel_title": f"There is no YT channel with ID {channel_id}"}
  19. return {"info": channel_info, "videos": list_of_videos}