Skip to content

Commit

Permalink
plugins.nicolive: resolve new api format (#3039)
Browse files Browse the repository at this point in the history
closes #3028
  • Loading branch information
wiresp33d committed Jun 21, 2020
1 parent 099a6c3 commit 39c3542
Showing 1 changed file with 55 additions and 20 deletions.
75 changes: 55 additions & 20 deletions src/streamlink/plugins/nicolive.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class NicoLive(Plugin):
stream_reader = None
_ws = None

frontend_id = None

@classmethod
def can_handle_url(cls, url):
return _url_re.match(url) is not None
Expand Down Expand Up @@ -118,10 +120,20 @@ def get_wss_api_url(self):
_log.debug(e)
_log.warning("Failed to extract broadcast id")

try:
self.frontend_id = extract_text(
resp.text, ""frontendId":", ","")
except Exception as e:
_log.debug(e)
_log.warning("Failed to extract frontend id")

self.wss_api_url = "{0}&frontend_id={1}".format(self.wss_api_url, self.frontend_id)

_log.debug("Video page response code: {0}".format(resp.status_code))
_log.trace(u"Video page response body: {0}".format(resp.text))
_log.debug("Got wss_api_url: {0}".format(self.wss_api_url))
_log.debug("Got broadcast_id: {0}".format(self.broadcast_id))
_log.debug("Got frontend_id: {0}".format(self.frontend_id))

return self.wss_api_url.startswith("wss://")

Expand Down Expand Up @@ -172,33 +184,50 @@ def send_message(self, type_, body):
else:
_log.warning("wss api is not connected.")

def send_playerversion(self):
body = {
"command": "playerversion",
"params": ["leo"]
}
self.send_message("watch", body)
def send_no_body_message(self, type_):
msg = {"type": type_}
msg_json = json.dumps(msg)
_log.debug(u"Sending: {0}".format(msg_json))
if self._ws and self._ws.sock.connected:
self._ws.send(msg_json)
else:
_log.warning("wss api is not connected.")

def send_getpermit(self, require_new_stream=True):
def send_custom_message(self, msg):
msg_json = json.dumps(msg)
_log.debug(u"Sending: {0}".format(msg_json))
if self._ws and self._ws.sock.connected:
self._ws.send(msg_json)
else:
_log.warning("wss api is not connected.")

def send_playerversion(self):
body = {
"command": "getpermit",
"requirement": {
"broadcastId": self.broadcast_id,
"route": "",
"type": "startWatching",
"data": {
"stream": {
"quality": "high",
"protocol": "hls",
"requireNewStream": require_new_stream,
"priorStreamQuality": "abr",
"isLowLatency": True,
"isChasePlay": False
"latency": "high",
"chasePlay": False
},
"room": {
"isCommentable": True,
"protocol": "webSocket"
}
"protocol": "webSocket",
"commentable": True
},
"reconnect": False
}
}
self.send_message("watch", body)
self.send_custom_message(body)

def send_getpermit(self, require_new_stream=True):
body = {
"type": "getAkashic",
"data": {
"chasePlay": False
}
}
self.send_custom_message(body)

def send_watching(self):
body = {
Expand All @@ -208,12 +237,18 @@ def send_watching(self):
self.send_message("watch", body)

def send_pong(self):
self.send_message("pong", {})
self.send_no_body_message("pong")
self.send_no_body_message("keepSeat")

def handle_api_message(self, message):
_log.debug(u"Received: {0}".format(message))
message_parsed = json.loads(message)

if message_parsed["type"] == "stream":
data = message_parsed["data"]
self.hls_stream_url = data["uri"]
self.is_stream_ready = True

if message_parsed["type"] == "watch":
body = message_parsed["body"]
command = body["command"]
Expand Down

0 comments on commit 39c3542

Please sign in to comment.