aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/livestreamer/plugins/ustreamtv.py
blob: e339fe0ed446a2108905f9ced6720f1e22379841 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from livestreamer.compat import str, bytes
from livestreamer.plugins import Plugin, PluginError, NoStreamsError
from livestreamer.stream import RTMPStream
from livestreamer.utils import urlget

import xml.dom.minidom, re

class UStreamTV(Plugin):
    AMFURL = "http://cgw.ustream.tv/Viewer/getStream/1/{0}.amf"
    SWFURL = "http://cdn1.ustream.tv/swf/4/viewer.rsl.210.swf"

    @classmethod
    def can_handle_url(self, url):
        return "ustream.tv" in url

    def _get_channel_id(self, url):
        res = urlget(url)

        match = re.search("channelId=(\d+)", res.text)
        if match:
            return int(match.group(1))

    def _get_streams(self):
        def get_amf_value(data, key):
            pattern = ("{0}\x02..(.*?)\x00").format(key)
            match = re.search(bytes(pattern, "ascii"), data)
            if match:
                return str(match.group(1), "ascii")

        streams = {}
        channelid = self._get_channel_id(self.url)

        if not channelid:
            raise NoStreamsError(self.url)

        self.logger.debug("Fetching stream info")
        res = urlget(self.AMFURL.format(channelid))
        data = res.content

        playpath = get_amf_value(data, "streamName")
        cdnurl = get_amf_value(data, "cdnUrl")
        fmsurl = get_amf_value(data, "fmsUrl")

        if playpath:
            stream = RTMPStream(self.session, {
                "rtmp": ("{0}/{1}").format(cdnurl or fmsurl, playpath),
                "pageUrl": self.url,
                "swfUrl": self.SWFURL,
                "live": True
            })
            streams["live"] = stream

        return streams

__plugin__ = UStreamTV