def _check_vod_key(self, nodeip, nodeid, userno, userip): try: conn = socket.create_connection((nodeip, self.KeyCheckPort), timeout=15) except socket.error as err: raise PluginError( ("Failed to connect to key check server: {0}").format( str(err))) msg = "Login,0,{userno},{nodeid},{userip}\n".format(nodeid=nodeid, userno=userno, userip=userip) try: conn.sendall(bytes(msg, "ascii")) res = conn.recv(4096) except IOError as err: raise PluginError( ("Failed to communicate with key check server: {0}").format( str(err))) if len(res) == 0: raise PluginError("Empty response from key check server") conn.close() res = str(res, "ascii").strip().split(",") return res[-1]
def _create_gox_params(self, flashvars, level): flashvars["adstate"] = "0" flashvars["goxkey"] = self.GOXHashKey flashvars["level"] = str(level) keys = [ "leagueid", "conid", "goxkey", "level", "uno", "uip", "adstate" ] if "playmode" in flashvars and flashvars["playmode"] == "vod": keys += ["vjoinid", "nid"] goxkey = hashlib.md5() params = {} for key in keys: if not key in flashvars: raise PluginError( ("Missing key '{0}' in flashvars").format(key)) goxkey.update(bytes(flashvars[key], "ascii")) params[key] = flashvars[key] params["goxkey"] = goxkey.hexdigest() return params
def _get_hls_streams(self): url = self.HLSStreamTokenURL.format(self.channelname) try: res = urlget(url, params=dict(type="any", connection="wifi"), exception=IOError) except IOError: self.logger.debug("HLS streams not available") return {} json = res_json(res, "stream token JSON") if not isinstance(json, list): raise PluginError("Invalid JSON response") if len(json) == 0: raise PluginError("No stream token in JSON") streams = {} token = verifyjson(json[0], "token") hashed = hmac.new(self.HLSStreamTokenKey, bytes(token, "utf8"), sha1) fulltoken = hashed.hexdigest() + ":" + token url = self.HLSSPlaylistURL.format(self.channelname) try: params = dict(token=fulltoken, hd="true") playlist = HLSStream.parse_variant_playlist(self.session, url, params=params) except IOError as err: raise PluginError(err) return playlist
def _get_hls_streams(self): url = self.HLSStreamTokenURL.format(self.channelname) try: res = urlget(url, params=dict(type="any", connection="wifi"), exception=IOError) except IOError: return {} if not isinstance(res.json, list): raise PluginError("Stream info response is not JSON") if len(res.json) == 0: raise PluginError("No stream token in JSON") streams = {} token = verifyjson(res.json[0], "token") hashed = hmac.new(self.HLSStreamTokenKey, bytes(token, "utf8"), sha1) fulltoken = hashed.hexdigest() + ":" + token url = self.HLSSPlaylistURL.format(self.channelname) try: params = dict(token=fulltoken, hd="true") playlist = HLSStream.parse_variant_playlist(self.session, url, params=params) except IOError as err: raise PluginError(err) return playlist
def _check_vod_key(self, nodeip, nodeid, userno, userip): try: conn = socket.create_connection((nodeip, self.KeyCheckPort), timeout=30) except socket.error as err: raise PluginError(("Failed to connect to key check server: {0}").format(str(err))) msg = "Login,0,{userno},{nodeid},{userip}\n".format(nodeid=nodeid, userno=userno, userip=userip) try: conn.sendall(bytes(msg, "ascii")) res = conn.recv(4096) except IOError as err: raise PluginError(("Failed to communicate with key check server: {0}").format(str(err))) if len(res) == 0: raise PluginError("Empty response from key check server") conn.close() res = str(res, "ascii").strip().split(",") return res[-1]
def _get_hls_streams(self): url = self.HLSStreamTokenURL.format(self.channelname) try: res = urlget(url, params=dict(type="iphone", connection="wifi", allow_cdn="true"), exception=IOError) except IOError: self.logger.debug("HLS streams not available") return {} json = res_json(res, "stream token JSON") if not isinstance(json, list): raise PluginError("Invalid JSON response") if len(json) == 0: raise PluginError("No stream token in JSON") token = verifyjson(json[0], "token") hashed = hmac.new(self.HLSStreamTokenKey, bytes(token, "utf8"), sha1) fulltoken = hashed.hexdigest() + ":" + token url = self.HLSSPlaylistURL.format(self.channelname) try: params = dict(token=fulltoken, hd="true", allow_cdn="true") playlist = HLSStream.parse_variant_playlist(self.session, url, params=params) except IOError as err: if "404" in err: raise PluginError(err) else: self.logger.debug("Requesting mobile transcode") payload = dict(channel=self.channelname, type="iphone") urlopen("http://usher.twitch.tv/stream/transcode_iphone.json", data=payload) return {} return playlist
def create_playlist_token(self, channel): url = self.url(HLS_TOKEN_PATH, channel) params = dict(type="iphone", allow_cdn="true") try: res = urlget(url, params=params, exception=IOError) except IOError: return None json = res_json(res, "stream token JSON") if not (isinstance(json, list) and json): raise PluginError("Invalid JSON response") token = verifyjson(json[0], "token") hashed = hmac.new(HLS_TOKEN_KEY, bytes(token, "utf8"), sha1) return "{0}:{1}".format(hashed.hexdigest(), token)
def _create_stream_key(self, url): parsed = urlparse(url) params = parse_qsd(parsed.query) keys = ["uno", "nodeid"] for key in keys: if not key in params: raise PluginError( ("Missing key '{0}' in key check params").format(key)) userip = self._get_user_ip() nodeip = parsed.netloc try: conn = socket.create_connection( (nodeip, self.VODStreamKeyCheckPort), timeout=30) except socket.error as err: raise PluginError( ("Failed to connect to key check server: {0}").format( str(err))) msg = "Login,0,{userno},{nodeid},{userip}\n".format( nodeid=params["nodeid"], userno=params["uno"], userip=userip) try: conn.sendall(bytes(msg, "ascii")) res = conn.recv(4096) except IOError as err: raise PluginError( ("Failed to communicate with key check server: {0}").format( str(err))) if len(res) == 0: raise PluginError("Empty response from key check server") conn.close() res = str(res, "ascii").strip().split(",") return res[-1]
def _get_hls_streams(self): url = self.HLSStreamTokenURL.format(self.channelname) try: res = urlget(url, params=dict(type="iphone", connection="wifi", allow_cdn="true"), exception=IOError) except IOError: self.logger.debug("HLS streams not available") return {} json = res_json(res, "stream token JSON") if not isinstance(json, list): raise PluginError("Invalid JSON response") if len(json) == 0: raise PluginError("No stream token in JSON") token = verifyjson(json[0], "token") hashed = hmac.new(self.HLSStreamTokenKey, bytes(token, "utf8"), sha1) fulltoken = hashed.hexdigest() + ":" + token url = self.HLSPlaylistURL.format(self.channelname) try: params = dict(token=fulltoken, hd="true", allow_cdn="true") playlist = HLSStream.parse_variant_playlist(self.session, url, nameprefix="mobile_", params=params) except IOError as err: if "404" not in str(err): raise PluginError(err) else: self.logger.debug("Requesting mobile transcode") payload = dict(channel=self.channelname, type="iphone") urlopen(self.HLSTranscodeRequest, data=payload) return {} return playlist
def _create_gox_params(self, flashvars, level): flashvars["adstate"] = "0" flashvars["goxkey"] = self.GOXHashKey flashvars["level"] = str(level) keys = ["leagueid", "conid", "goxkey", "level", "uno", "uip", "adstate"] if "playmode" in flashvars and flashvars["playmode"] == "vod": keys += ["vjoinid", "nid"] goxkey = hashlib.md5() params = {} for key in keys: if not key in flashvars: raise PluginError(("Missing key '{0}' in flashvars").format(key)) goxkey.update(bytes(flashvars[key], "ascii")) params[key] = flashvars[key] params["goxkey"] = goxkey.hexdigest() return params
def _create_stream_key(self, url): parsed = urlparse(url) params = parse_qsd(parsed.query) keys = ["uno", "nodeid"] for key in keys: if not key in params: raise PluginError(("Missing key '{0}' in key check params").format(key)) userip = self._get_user_ip() nodeip = parsed.netloc try: conn = socket.create_connection((nodeip, self.VODStreamKeyCheckPort), timeout=30) except socket.error as err: raise PluginError(("Failed to connect to key check server: {0}").format(str(err))) msg = "Login,0,{userno},{nodeid},{userip}\n".format(nodeid=params["nodeid"], userno=params["uno"], userip=userip) try: conn.sendall(bytes(msg, "ascii")) res = conn.recv(4096) except IOError as err: raise PluginError(("Failed to communicate with key check server: {0}").format(str(err))) if len(res) == 0: raise PluginError("Empty response from key check server") conn.close() res = str(res, "ascii").strip().split(",") return res[-1]
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")