feed.py 1.2 KB

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