Example #1
0
def get_http_data(url,
                  header=None,
                  data=None,
                  useragent=FIREFOX_UA,
                  referer=None,
                  cookiejar=None):
    """ Get the page to parse it for streams """
    if not cookiejar:
        cookiejar = CookieJar()

    log.debug("HTTP getting %r", url)
    starttime = time.time()

    request = Request(url)
    standard_header = {'Referer': referer, 'User-Agent': useragent}
    for key, value in [head for head in standard_header.items() if head[1]]:
        request.add_header(key, value)
    if header:
        for key, value in [head for head in header.items() if head[1]]:
            request.add_header(key, value)
    if data:
        request.add_data(data)

    opener = build_opener(HTTPCookieProcessor(cookiejar))

    try:
        response = opener.open(request)
    except HTTPError as e:
        log.error("Something wrong with that url")
        log.error("Error code: %s", e.code)
        sys.exit(5)
    except URLError as e:
        log.error("Something wrong with that url")
        log.error("Error code: %s", e.reason)
        sys.exit(5)
    except ValueError as e:
        log.error("Try adding http:// before the url")
        sys.exit(5)
    if is_py3:
        data = response.read()
        try:
            data = data.decode("utf-8")
        except UnicodeDecodeError:
            pass
    else:
        try:
            data = response.read()
        except socket.error as e:
            log.error("Lost the connection to the server")
            sys.exit(5)
    response.close()

    spent_time = time.time() - starttime
    bps = 8 * len(data) / max(spent_time, 0.001)

    log.debug("HTTP got %d bytes from %r in %.2fs (= %dbps)", len(data), url,
              spent_time, bps)

    return data
Example #2
0
 def __init__(self, url):
     Service.__init__(self, url)
     self.cj = CookieJar()
     self.subtitle = None
Example #3
0
class Kanal5(Service):
    supported_domains = ['kanal5play.se', 'kanal9play.se']

    def __init__(self, url):
        Service.__init__(self, url)
        self.cj = CookieJar()
        self.subtitle = None

    def get(self, options):
        match = re.search(r".*video/([0-9]+)", self.url)
        if not match:
            log.error("Can't find video file")
            sys.exit(2)

        video_id = match.group(1)
        if options.username and options.password:
            #bogus
            cc = Cookie(None, 'asdf', None, '80', '80', 'www.kanal5play.se', None, None, '/', None, False, False, 'TestCookie', None, None, None)
            self.cj.set_cookie(cc)
            #get session cookie
            data = get_http_data("http://www.kanal5play.se/", cookiejar=self.cj)
            authurl = "https://kanal5swe.appspot.com/api/user/login?callback=jQuery171029989&email=%s&password=%s&_=136250" % (options.username, options.password)
            data = get_http_data(authurl)
            match = re.search(r"({.*})\);", data)
            jsondata = json.loads(match.group(1))
            if jsondata["success"] == False:
                log.error(jsondata["message"])
                sys.exit(2)
            authToken = jsondata["userData"]["auth"]
            cc = Cookie(version=0, name='authToken',
                          value=authToken,
                          port=None, port_specified=False,
                          domain='www.kanal5play.se',
                          domain_specified=True,
                          domain_initial_dot=True, path='/',
                          path_specified=True, secure=False,
                          expires=None, discard=True, comment=None,
                          comment_url=None, rest={'HttpOnly': None})
            self.cj.set_cookie(cc)

        url = "http://www.kanal5play.se/api/getVideo?format=FLASH&videoId=%s" % video_id
        data = json.loads(get_http_data(url, cookiejar=self.cj))
        if not options.live:
            options.live = data["isLive"]
        if data["hasSubtitle"]:
            yield subtitle_json("http://www.kanal5play.se/api/subtitles/%s" % video_id)

        for i in data["streams"]:
            if i["drmProtected"]:
                log.error("We cant download drm files for this site.")
                sys.exit(2)
            steambaseurl = data["streamBaseUrl"]
            bitrate = i["bitrate"]
            if bitrate > 1000:
                bitrate = bitrate / 1000
            options2 = copy.copy(options)
            options2.other = "-W %s -y %s " % ("http://www.kanal5play.se/flash/K5StandardPlayer.swf", i["source"])
            options2.live = True
            yield RTMP(options2, steambaseurl, bitrate)

        url = "http://www.kanal5play.se/api/getVideo?format=IPAD&videoId=%s" % video_id
        data = json.loads(get_http_data(url, cookiejar=self.cj))
        for i in data["streams"]:
                streams = hlsparse(i["source"])
                for n in list(streams.keys()):
                    yield HLS(copy.copy(options), streams[n], n)
Example #4
0
class Kanal5(Service):
    supported_domains = ["kanal5play.se", "kanal9play.se", "kanal11play.se"]

    def __init__(self, url):
        Service.__init__(self, url)
        self.cj = CookieJar()
        self.subtitle = None

    def get(self, options):
        match = re.search(r".*video/([0-9]+)", self.url)
        if not match:
            log.error("Can't find video file")
            return

        video_id = match.group(1)
        if options.username and options.password:
            # get session cookie
            error, data = get_http_data("http://www.kanal5play.se/", cookiejar=self.cj)
            authurl = (
                "https://kanal5swe.appspot.com/api/user/login?callback=jQuery171029989&email=%s&password=%s&_=136250"
                % (options.username, options.password)
            )
            error, data = get_http_data(authurl)
            match = re.search(r"({.*})\);", data)
            jsondata = json.loads(match.group(1))
            if jsondata["success"] is False:
                log.error(jsondata["message"])
                return
            authToken = jsondata["userData"]["auth"]
            cc = Cookie(
                version=0,
                name="authToken",
                value=authToken,
                port=None,
                port_specified=False,
                domain="www.kanal5play.se",
                domain_specified=True,
                domain_initial_dot=True,
                path="/",
                path_specified=True,
                secure=False,
                expires=None,
                discard=True,
                comment=None,
                comment_url=None,
                rest={"HttpOnly": None},
            )
            self.cj.set_cookie(cc)
            options.cookies = self.cj

        url = "http://www.kanal5play.se/api/getVideo?format=FLASH&videoId=%s" % video_id
        error, data = get_http_data(url, cookiejar=self.cj)
        if error:
            log.error("Can't download video info")
            return
        data = json.loads(data)
        options.cookiejar = self.cj
        if not options.live:
            options.live = data["isLive"]

        if options.output_auto:
            directory = os.path.dirname(options.output)
            options.service = "kanal5"

            title = "%s-s%s-%s-%s-%s" % (
                data["program"]["name"],
                data["seasonNumber"],
                data["episodeText"],
                data["id"],
                options.service,
            )
            title = filenamify(title)
            if len(directory):
                options.output = "%s/%s" % (directory, title)
            else:
                options.output = title

        if self.exclude(options):
            return

        if data["hasSubtitle"]:
            yield subtitle(copy.copy(options), "json", "http://www.kanal5play.se/api/subtitles/%s" % video_id)

        if options.force_subtitle:
            return

        if "streams" in data.keys():
            for i in data["streams"]:
                if i["drmProtected"]:
                    log.error("We cant download drm files for this site.")
                    return
                steambaseurl = data["streamBaseUrl"]
                bitrate = i["bitrate"]
                if bitrate > 1000:
                    bitrate = bitrate / 1000
                options2 = copy.copy(options)
                options2.other = "-W %s -y %s " % ("http://www.kanal5play.se/flash/K5StandardPlayer.swf", i["source"])
                options2.live = True
                yield RTMP(options2, steambaseurl, bitrate)

            url = "http://www.kanal5play.se/api/getVideo?format=IPAD&videoId=%s" % video_id
            error, data = get_http_data(url, cookiejar=self.cj)
            if error:
                log.error("Cant get video info")
                return
            data = json.loads(data)
            if "streams" in data.keys():
                for i in data["streams"]:
                    streams = hlsparse(i["source"])
                    for n in list(streams.keys()):
                        yield HLS(copy.copy(options), streams[n], n)
        if "reasonsForNoStreams" in data:
            log.error(data["reasonsForNoStreams"][0])
Example #5
0
 def __init__(self, url):
     Service.__init__(self, url)
     self.cj = CookieJar()
     self.subtitle = None
Example #6
0
class Kanal5(Service):
    supported_domains = ['kanal5play.se', 'kanal9play.se', 'kanal11play.se']

    def __init__(self, url):
        Service.__init__(self, url)
        self.cj = CookieJar()
        self.subtitle = None

    def get(self, options):
        match = re.search(r".*video/([0-9]+)", self.url)
        if not match:
            log.error("Can't find video file")
            return

        video_id = match.group(1)
        if options.username and options.password:
            # get session cookie
            data = get_http_data("http://www.kanal5play.se/", cookiejar=self.cj)
            authurl = "https://kanal5swe.appspot.com/api/user/login?callback=jQuery171029989&email=%s&password=%s&_=136250" % \
                      (options.username, options.password)
            data = get_http_data(authurl)
            match = re.search(r"({.*})\);", data)
            jsondata = json.loads(match.group(1))
            if jsondata["success"] is False:
                log.error(jsondata["message"])
                return
            authToken = jsondata["userData"]["auth"]
            cc = Cookie(version=0, name='authToken',
                        value=authToken,
                        port=None, port_specified=False,
                        domain='www.kanal5play.se',
                        domain_specified=True,
                        domain_initial_dot=True, path='/',
                        path_specified=True, secure=False,
                        expires=None, discard=True, comment=None,
                        comment_url=None, rest={'HttpOnly': None})
            self.cj.set_cookie(cc)

        url = "http://www.kanal5play.se/api/getVideo?format=FLASH&videoId=%s" % video_id
        data = json.loads(get_http_data(url, cookiejar=self.cj))
        options.cookiejar = self.cj
        if not options.live:
            options.live = data["isLive"]
        if data["hasSubtitle"]:
            yield subtitle_json("http://www.kanal5play.se/api/subtitles/%s" % video_id)

        if options.output_auto:
            directory = os.path.dirname(options.output)
            options.service = "kanal5"

            title = "%s-s%s-%s-%s-%s" % (data["program"]["name"], data["seasonNumber"], data["episodeText"], data["id"], options.service)
            title = filenamify(title)
            if len(directory):
                options.output = "%s/%s" % (directory, title)
            else:
                options.output = title

        if options.force_subtitle:
            return

        if "streams" in data:
            for i in data["streams"]:
                if i["drmProtected"]:
                    log.error("We cant download drm files for this site.")
                    return
                steambaseurl = data["streamBaseUrl"]
                bitrate = i["bitrate"]
                if bitrate > 1000:
                    bitrate = bitrate / 1000
                options2 = copy.copy(options)
                options2.other = "-W %s -y %s " % ("http://www.kanal5play.se/flash/K5StandardPlayer.swf", i["source"])
                options2.live = True
                yield RTMP(options2, steambaseurl, bitrate)

            url = "http://www.kanal5play.se/api/getVideo?format=IPAD&videoId=%s" % video_id
            data = json.loads(get_http_data(url, cookiejar=self.cj))
            if "streams" in data.keys():
                for i in data["streams"]:
                    streams = hlsparse(i["source"])
                    for n in list(streams.keys()):
                        yield HLS(copy.copy(options), streams[n], n)
        if "reasonsForNoStreams" in data:
            log.error(data["reasonsForNoStreams"][0])
Example #7
0
def get_http_data(url, header=None, post=None, useragent=FIREFOX_UA, referer=None, cookiejar=None):
    error = None
    method = "GET"
    standard_headers = {"Referer": referer, "User-Agent": useragent}
    request_headers = {}
    for key, value in [head for head in standard_headers.items() if head[1]]:
        request_headers[key] = value
    if header:
        for key, value in [head for head in header.items() if head[1]]:
            request_headers[key] = value

    body = ""
    if post:
        method = "POST"
        body = urlencode(post)

    if not cookiejar:
        cookiejar = CookieJar()
    cookie_header = []
    for cookie in cookiejar:
        cookie_header.append(cookie.name + '="' + cookie.value.replace('"', r"\"") + '"')
    if len(cookie_header) > 0:
        request_headers["Cookie"] = "; ".join(cookie_header)

    if url.find("manifest.f4m") > 0:
        parse = urlparse(url)
        url = "%s://%s%s?%s&hdcore=3.3.0" % (parse.scheme, parse.netloc, parse.path, parse.query)

    log.debug("HTTP getting %r", url)
    starttime = time.time()

    (response_headers, content) = h.request(url, method, headers=request_headers, body=body)
    if "set-cookie" in response_headers:
        cookies = SimpleCookie()
        cookies.load(response_headers["set-cookie"])
        for key, c in cookies.items():
            expires = c["expires"] or None
            if expires:
                try:
                    expires = time.strptime(expires, "%a, %d %b %Y %H:%M:%S GMT")
                except ValueError:
                    log.debug("Failed to decode cookie expires field '%s' as a time" % expires)
                    expires = None
                    pass
            cookie = Cookie(
                version=int(c["version"] or "0"),
                name=key,
                value=c.value,
                port=None,
                port_specified=False,
                domain=c["domain"] or "",
                domain_specified=True if c["domain"] else False,
                domain_initial_dot=True if (c["domain"] and c["domain"].startswith(".")) else False,
                path=c["path"] or None,
                path_specified=True if c["path"] else False,
                secure=True if c["secure"] else False,
                expires=expires,
                discard=True,
                comment=None,
                comment_url=None,
                rest={"HttpOnly": None},
            )
            cookiejar.set_cookie(cookie)

    if is_py3:
        try:
            content = content.decode("utf-8")
        except UnicodeDecodeError:
            pass

    spent_time = time.time() - starttime
    bps = 8 * len(content) / max(spent_time, 0.001)

    log.debug("HTTP got %d bytes from %r in %.2fs (= %dbps)", len(content), url, spent_time, bps)

    return error, content