1import re
2
3from livestreamer.plugin import Plugin
4from livestreamer.plugin.api import http, validate
5from livestreamer.plugin.api.utils import parse_query
6from livestreamer.stream import RTMPStream
7
8BALANCER_URL = "http://www.mips.tv:1935/loadbalancer"
9PLAYER_URL = "http://mips.tv/embedplayer/{0}/1/500/400"
10SWF_URL = "http://mips.tv/content/scripts/eplayer.swf"
11
12_url_re = re.compile("http(s)?://(\w+.)?mips.tv/(?P<channel>[^/&?]+)")
13_flashvars_re = re.compile("'FlashVars', '([^']+)'")
14_rtmp_re = re.compile("redirect=(.+)")
15
16_schema = validate.Schema(
17    validate.transform(_flashvars_re.search),
18    validate.any(
19        None,
20        validate.all(
21            validate.get(1),
22            validate.transform(parse_query),
23            {
24                "id": validate.transform(int),
25                validate.optional("s"): validate.text
26            }
27        )
28    )
29)
30_rtmp_schema = validate.Schema(
31    validate.transform(_rtmp_re.search),
32    validate.get(1),
33)
34
35
36class Mips(Plugin):
37    @classmethod
38    def can_handle_url(self, url):
39        return _url_re.match(url)
40
41    def _get_streams(self):
42        match = _url_re.match(self.url)
43        channel = match.group("channel")
44
45        headers = {"Referer": self.url}
46        url = PLAYER_URL.format(channel)
47        res = http.get(url, headers=headers, schema=_schema)
48        if not res or "s" not in res:
49            return
50
51        streams = {}
52        server = http.get(BALANCER_URL, headers=headers, schema=_rtmp_schema)
53        playpath = "{0}?{1}".format(res["s"], res["id"])
54        streams["live"] = RTMPStream(self.session, {
55            "rtmp": "rtmp://{0}/live/{1}".format(server, playpath),
56            "pageUrl": self.url,
57            "swfVfy": SWF_URL,
58            "conn": "S:OK",
59            "live": True
60        })
61
62        return streams
63
64__plugin__ = Mips
65