room.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import re
  2. _csrf_re = re.compile("SrGlobal.csrfToken = \\'([\\w\\d-]+)\\';")
  3. class RoomEndpointsMixin:
  4. """
  5. For endpoints in */api/room/* and */room/*
  6. As well as */api/event/* and */api/support/* since there's only one each
  7. and they go with event_and_support which is already here
  8. """
  9. # TODO: find more event endpoints, surely they exist?
  10. # e.g. there is /event/room_list?event_id={}, but this is very messy
  11. def event_and_support(self, room_id):
  12. """
  13. Gets support and event info for a room
  14. Result has three top fields:
  15. support
  16. regular_event
  17. event
  18. support is either none or has the following fields:
  19. is_achieved: bool
  20. goal_point: int
  21. tutorial_url: url # "https://www.showroom-live.com/room/tutorial"
  22. title: str
  23. support_id: int
  24. next_level: int
  25. current_point: int
  26. regular_event is either null or ?
  27. event is either null or has the following fields:
  28. ranking: dict # only for ranking events?
  29. event_name: str
  30. event_id: int
  31. event_description: str
  32. event_type: str # TODO: list possible event types
  33. image: url
  34. ended_at: timestamp # time the event ends, in UTC
  35. started_at: timestamp # time the event started, in UTC
  36. tutorial_url: url # "https://www.showroom-live.com/room/tutorial"
  37. event_url: url
  38. known event types:
  39. ranking
  40. event.ranking has the following fields:
  41. gap: int # points between this room and the next
  42. point: int
  43. text: str
  44. next_rank: int
  45. rank: int
  46. :param room_id:
  47. :return:
  48. """
  49. endpoint = "/api/room/event_and_support"
  50. results = self._api_get(endpoint, params={"room_id": room_id})
  51. return results
  52. def contribution_ranking(self, room_id, event_id):
  53. endpoint = "/api/event/contribution_ranking"
  54. result = self._api_get(endpoint, params=dict(room_id=room_id, event_id=event_id))
  55. # TODO: find example responses
  56. return result
  57. def support_users(self, room_id, support_id):
  58. endpoint = "/api/support/support_users"
  59. result = self._api_get(endpoint, params=dict(room_id=room_id, support_id=support_id))
  60. # TODO: find example responses
  61. return result
  62. def room_settings(self, room_id):
  63. endpoint = "/api/room/settings"
  64. results = self._api_get(endpoint, params={"room_id": room_id})
  65. return results
  66. def banners(self, room_id):
  67. endpoint = "/api/room/banners"
  68. results = self._api_get(endpoint, params={"room_id": room_id})
  69. return results
  70. def next_live(self, room_id):
  71. """
  72. Get the time of the next scheduled live
  73. :param room_id:
  74. :return: UTC epoch time of the next live
  75. """
  76. # TODO: return datetime object instead?
  77. # or just return the result as is?
  78. endpoint = "/api/room/next_live"
  79. results = self._api_get(endpoint, params={"room_id": room_id})
  80. return results.get('epoch')
  81. def is_live(self, room_id):
  82. """
  83. Checks if a room is live
  84. :param room_id:
  85. :return: bool
  86. """
  87. # WARNING: watch this endpoint for deprecation
  88. endpoint = "/room/is_live"
  89. result = self._api_get(endpoint, params={"room_id": room_id}, default={'ok': 0})
  90. return bool(result["ok"])
  91. def profile(self, room_id):
  92. """
  93. Get profile info for a room
  94. :param room_id:
  95. :return:
  96. """
  97. endpoint = "/api/room/profile"
  98. result = self._api_get(endpoint, params={"room_id": room_id})
  99. return result
  100. def follow(self, room_id, flag=1):
  101. """
  102. Requires auth
  103. :param room_id:
  104. :param flag: 0 (unfollow) or 1 (follow)
  105. :return:
  106. """
  107. endpoint = "/api/room/follow"
  108. r = self._api_post(endpoint, data={"room_id": room_id, "flag": flag, "csrf_token": self._csrf_token})
  109. return r
  110. def unfollow(self, room_id):
  111. return self.follow(room_id, 0)