feed.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from urllib.error import URLError
  2. import feedparser # https://feedparser.readthedocs.io/en/latest/
  3. from download import Video, get_info_yt_video
  4. class CheckFeed:
  5. def __init__(self, yt_channel_id: str, items_quantity: int = 5):
  6. self.items_quantity = self.__check_quantity(items_quantity)
  7. self.feed_url = f"https://www.youtube.com/feeds/videos.xml?channel_id={yt_channel_id}"
  8. self.title = None
  9. self.videos = self.read()
  10. self.upadates = []
  11. def __check_quantity(self, quantity: int):
  12. if not isinstance(quantity, int) or quantity <= 0:
  13. raise ValueError("Error. You have to provide an integer value more then zero")
  14. else:
  15. return quantity
  16. def read(self, items_quantity: int = 0) -> list[Video]:
  17. self.videos = []
  18. items_quantity = self.__check_quantity(items_quantity) if items_quantity != 0 \
  19. else self.items_quantity
  20. feed = feedparser.parse(self.feed_url)
  21. try:
  22. self.title = feed['channel']['title']
  23. except KeyError:
  24. raise ValueError('Suppose invalid youtube channel ID. \
  25. Check config.py file')
  26. for entry in feed.entries[:items_quantity]:
  27. try:
  28. video = get_info_yt_video(entry['link'])
  29. except Exception as err:
  30. if 'This live event will begin in a few moments' in str(err):
  31. pass
  32. else:
  33. self.videos.append(video)
  34. return self.videos