feed.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from urllib.error import URLError
  2. import feedparser
  3. # https://feedparser.readthedocs.io/en/latest/
  4. from config import YT_CHANNEL_ID
  5. class CheckFeed:
  6. def __init__(self):
  7. # self.url = f"https://www.youtube.com/feeds/videos.xml?channel_id={YT_CHANNEL_ID}"
  8. self.url = f"https://vid.puffyan.us/feed/channel/{YT_CHANNEL_ID}"
  9. self.title = self._get_info()
  10. def read(self):
  11. list_of_videos = []
  12. try:
  13. feed = feedparser.parse(self.url)
  14. for entry in feed.entries:
  15. list_of_videos.append({
  16. "id": entry["yt_videoid"],
  17. "link": entry["link"],
  18. "title": entry["title"]
  19. })
  20. return list_of_videos
  21. except URLError:
  22. return []
  23. def _get_info(self):
  24. try:
  25. feed = feedparser.parse(self.url)
  26. return feed['channel']['title']
  27. except KeyError:
  28. raise Exception('You are provide not valid feed url. Check FEED_URL in config.py')
  29. except URLError:
  30. raise Exception('URL. Check FEED_URL in config.py')