iv_lvideos.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from multiprocessing.pool import ThreadPool
  2. from threading import Thread
  3. from os.path import exists
  4. from invidious import *
  5. import operator
  6. import time
  7. import sys
  8. from plugins.plugin import Plugin
  9. sys.path.append("..")
  10. from src.video import Video
  11. from src.channel import Channel
  12. from src.utils import *
  13. from src.config import Config
  14. from src.history import History
  15. class InvidiousLastVideos(Plugin):
  16. def __init__(self, history: History=History(), config: Config=Config()) -> None:
  17. super().__init__(history, config)
  18. self.history = history
  19. self.config = config
  20. self.name = "[+] Invidious: Last videos"
  21. self.category = "Invidious"
  22. self.query_description = """"""
  23. self.params = {
  24. 'urls' : True
  25. }
  26. if exists(self.config.subs_path): self.SUBS_LIST = [i.strip('\n').split(',')[0] for i in open(self.config.subs_path)]
  27. else: self.SUBS_LIST = []
  28. self.subscriptions = list()
  29. self.sorted_sub_videos = list()
  30. self.flag = "-il"
  31. self.full_flag = "--ivlastvideos"
  32. self.flag_help = "show last videos from subscribed channels"
  33. self.flag_action = "store_true"
  34. def load_subscription(self, authorId=""):
  35. chn = self.invidious.get_channel(authorId)
  36. while chn == None:
  37. time.sleep(1)
  38. chn = self.invidious.get_channel(authorId)
  39. chn.convert(Channel)
  40. self.subscriptions.append(chn)
  41. def load_subscriptions(self):
  42. if len(self.subscriptions) > 0: return
  43. self.subscriptions.clear()
  44. self.invidious.update_mirrors()
  45. pool = ThreadPool(processes=10)
  46. pool.map(self.load_subscription, self.SUBS_LIST)
  47. self.subscriptions = sort_channels_by_link_list(self.subscriptions, self.SUBS_LIST)
  48. def get_last_videos(self, index: int=0):
  49. self.load_subscriptions()
  50. if len(self.sorted_sub_videos) == 0:
  51. items = []
  52. for sub in self.subscriptions:
  53. channel = sub
  54. latestVideos = channel.latestVideos
  55. for video in latestVideos:
  56. if video == None: continue
  57. video.convert(Video)
  58. video.url = self.invidious.get_working_mirror() + "/watch?v=" + video.videoId
  59. video.isWatched = video.in_history(self.history)
  60. items.append(video)
  61. self.sorted_sub_videos = sorted(items, key=operator.attrgetter('published'), reverse=True)
  62. for index, item in enumerate(self.sorted_sub_videos):
  63. self.sorted_sub_videos[index] = item.to_json()
  64. return self.sorted_sub_videos
  65. def get_items(self, params):
  66. return self.get_last_videos()