sr_ongoings.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import requests
  2. import sys
  3. from plugins.plugin import Plugin
  4. from src.history import History
  5. from src.config import Config
  6. sys.path.append("..")
  7. from src.playlist import Playlist
  8. from src.video import Video
  9. class SovetRomanticaOngoings(Plugin):
  10. def __init__(self, history: History=History(), config: Config=Config()):
  11. super().__init__(history, config)
  12. self.history = history
  13. self.config = config
  14. self.name = "[+] SovetRomantica: Ongoings"
  15. self.category = "SovetRomantica"
  16. self.params = {}
  17. self.flag = "-sro"
  18. self.full_flag = "--srongoins"
  19. self.flag_help = "show all sovetromantica ongoings"
  20. self.flag_action = "store_true"
  21. self.save_config()
  22. self.api_instance = self.config.get_option(self.category, 'api-instance')
  23. def get_episodes(self, anime_id):
  24. resp = requests.get(
  25. f"{self.api_instance}/anime/{anime_id}/episodes"
  26. )
  27. if resp.status_code != 200: return list()
  28. episodes = resp.json()
  29. return episodes
  30. def get_animes(self, ids):
  31. resp = requests.post(
  32. f"{self.api_instance}/animes",
  33. data={'anime_id_array' : ids}
  34. )
  35. if resp.status_code != 200: return list()
  36. return resp.json()
  37. def get_ongoings(self):
  38. resp = requests.get(f"{self.api_instance}/ongoing")
  39. if resp.status_code != 200: return list()
  40. anime_list = resp.json()
  41. items = []
  42. anime_list = self.get_animes(str(anime_list))
  43. for it in anime_list:
  44. if type(it) != dict: continue
  45. title = f"{it['anime_name']} ({it['anime_name_russian']})"
  46. anime_id = it['anime_id']
  47. episodes = list()
  48. episodes_js = self.get_episodes(anime_id)
  49. for episode in episodes_js:
  50. if type(episode) != dict or episode['episode_type'] > 0: continue
  51. ititle = f"Episode {episode['episode_count']}"
  52. video = Video(
  53. title=ititle,
  54. #playlist_title=title,
  55. url=episode['embed']
  56. )
  57. video.isWatched= video.in_history(self.history)
  58. item = video.to_json()
  59. episodes.append(item)
  60. episodes.sort(key=lambda v: int(v['title'][8:]))
  61. playlist = Playlist(title, episodes)
  62. items.append(playlist.to_json())
  63. return items
  64. def get_items(self, params):
  65. return self.get_ongoings()
  66. def save_config(self):
  67. self.config.set_option(self.category, 'api-instance', 'https://service.sovetromantica.com/v1')