aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/livestreamer/utils.py
blob: 5ab8a6359473853f547a0d9ba47b00abcdd445ac (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
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3

from livestreamer.compat import urllib, bytes
from livestreamer.plugins import PluginError
import hmac, hashlib, zlib, argparse

SWF_KEY = b"Genuine Adobe Flash Player 001"

class CommandLine(object):
    def __init__(self, command):
        self.command = command
        self.args = {}
        self.pipe = None

    def format(self):
        args = []

        for key, value in self.args.items():
            if value == True:
                args.append(("--{0}").format(key))
            else:
                escaped = str(value).replace('"', '\\"').replace("$", "\$").replace("`", "\`")
                args.append(("--{0} \"{1}\"").format(key, escaped))

        args = (" ").join(args)
        cmdline = ("{0} {1}").format(self.command, args)

        if self.pipe:
            cmdline += (" | {0}").format(self.pipe)

        return cmdline

class ArgumentParser(argparse.ArgumentParser):
    def convert_arg_line_to_args(self, line):
        split = line.find("=")
        key = line[:split].strip()
        val = line[split+1:].strip()
        yield "--%s=%s" % (key, val)

def urlget(url, data=None, timeout=None, opener=None):
    try:
        if opener is not None:
            fd = opener.open(url)
        else:
            fd = urllib.urlopen(url, data, timeout)

        data = fd.read()
        fd.close()

    except IOError as err:
        if type(err) is urllib.URLError:
            raise PluginError(err.reason)
        else:
            raise PluginError(err)

    return data

def swfverify(url):
    swf = urlget(url)

    if swf[:3] == b"CWS":
        swf = b"F" + swf[1:8] + zlib.decompress(swf[8:])

    h = hmac.new(SWF_KEY, swf, hashlib.sha256)

    return h.hexdigest(), len(swf)