iv_search.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from invidious import *
  2. import requests
  3. import sys
  4. from plugins.plugin import Plugin
  5. sys.path.append("..")
  6. from src.video import Video as RVideo
  7. from src.channel import Channel
  8. from src.playlist import Playlist
  9. from src.history import History
  10. from src.config import Config
  11. class InvidiousSearch(Plugin):
  12. def __init__(self, history: History=History(), config: Config=Config) -> None:
  13. super().__init__(history, config)
  14. self.history = history
  15. self.config = config
  16. self.name = "[+] Invidious: Search"
  17. self.category = "Invidious"
  18. self.query_description = """You can input args (separate they with &).
  19. page: number of page.
  20. sort_by: [relevance, rating, upload_date, view_count].
  21. date: [hour, today, week, month, year].
  22. duration: [short, long].
  23. type: [video, playlist, channel, all] (default: video).
  24. feauters: [hd, subtitles, creative_commons, 3d, live, purchased, 4k, 360, location, hdr].
  25. region: ISO 3166 country code (default: US).
  26. Example: distrotube&date=hour
  27. """
  28. self.params = {
  29. 'query' : True,
  30. 'page' : False,
  31. 'sort_by' : False,
  32. 'date' : False,
  33. 'duration' : False,
  34. 'type' : False,
  35. 'feauters' : False,
  36. 'region' : False
  37. }
  38. self.flag = "-is"
  39. self.full_flag = "--ivsearch"
  40. self.flag_help = "search videos from invidious by query"
  41. self.flag_action = ""
  42. def get_search(self, query: str=""):
  43. srch = self.invidious.search(query)
  44. items = []
  45. for sr in srch:
  46. if type(sr) == ChannelItem:
  47. sr = self.invidious.get_channel(sr.authorId)
  48. sr.convert(Channel)
  49. vids = list()
  50. for video in sr.latestVideos:
  51. video.convert(RVideo)
  52. video.url = self.invidious.get_working_mirror() + "/watch?v=" + video.videoId
  53. video.isWatched = video.in_history(self.history)
  54. vids.append(video.to_json())
  55. sr.latestVideos = vids
  56. elif type(sr) == VideoItem:
  57. sr.convert(RVideo)
  58. sr.url = self.invidious.get_working_mirror() + "/watch?v=" + sr.videoId
  59. sr.isWatched = sr.in_history(self.history)
  60. elif type(sr) == PlaylistItem:
  61. sr.convert(Playlist)
  62. vids = list()
  63. for video in sr.videos:
  64. video.convert(RVideo)
  65. video.url = self.invidious.get_working_mirror() + "/watch?v=" + video.videoId
  66. video.isWatched = video.in_history(self.history)
  67. vids.append(video.to_json())
  68. sr.videos = vids
  69. else: continue
  70. item = sr.to_json()
  71. items.append(item)
  72. return items
  73. def get_items(self, params):
  74. query = ""
  75. if 'query' in params:
  76. query = params['query']
  77. return self.get_search(query)