feed.py 1.8 KB

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