Skip to content

Instantly share code, notes, and snippets.

@athoik
Created April 1, 2014 16:57
Show Gist options
  • Save athoik/9918293 to your computer and use it in GitHub Desktop.
Save athoik/9918293 to your computer and use it in GitHub Desktop.
Livestreamer live.nicovideo.jp plugin
from livestreamer.compat import unquote, urlparse
from livestreamer.plugin import Plugin
from livestreamer.plugin.api import http
from livestreamer.stream import RTMPStream
from livestreamer.utils import parse_xml
PLAYER_API = "http://ext.live.nicovideo.jp/api/getplayerstatus"
class Nicovideo(Plugin):
@classmethod
def can_handle_url(self, url):
return "live.nicovideo.jp/watch/" in url
def _get_streams(self):
if not RTMPStream.is_usable(self.session):
raise PluginError("rtmpdump is not usable and required by Nicovideo plugin")
parsed = urlparse(self.url)
v = parsed.path.split("/")[-1]
self.logger.debug("Fetching stream info")
res = http.get(PLAYER_API, params=dict(v=v))
root = parse_xml(res.text.encode("utf8"))
if root.attrib["status"] != "ok":
error = root.find("error/code").text
self.logger.error(error)
return
contents = unquote(root.find("stream/contents_list/contents").text)
contents = contents.replace("case:", "").replace("akamai:", "live:")
contents = contents.split(",")
contents = zip(contents[::2], contents[1::2])
streams = {}
for url, playpath in contents:
name, url = url.split(":rtmp://")
xpath = "tickets/stream[@name='{0}']".format(playpath)
auth = root.find(xpath).text
stream = RTMPStream(self.session, {
"rtmp": "rtmp://{0}".format(url),
"playpath": "{0}?{1}".format(playpath, auth),
"pageUrl": self.url,
"live": True
})
streams[name] = stream
return streams
__plugin__ = Nicovideo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment