-
-
Save clive2000/8dfaa1ae14b317e94b904d09179cfe7f to your computer and use it in GitHub Desktop.
huya.com api,直播流URL提取
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
import re | |
import requests | |
import argparse | |
sess = requests.Session() | |
def get_api(url): | |
headers = {"User-Agent":"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Mobile Safari/537.36"} | |
url = re.sub(r'http.*huya.com','https://m.huya.com',url) | |
r = sess.get(url,headers=headers) | |
r = r.text | |
try: | |
islive = re.search(r'ISLIVE\s*=\s*([a-z]+)\s*;',r).group(1) | |
except: | |
raise ValueError("Can not find the ISLIVE value!!") | |
if 'false' in islive: | |
raise Exception('Offline') | |
try: | |
channel = re.search(r'TOPSID\s*=\s*\'(\d+)\'',r).group(1) | |
sid = re.search(r'SUBSID\s*=\s*\'(\d+)\'',r).group(1) | |
except: | |
raise ValueError("Can not find the SID value!!") | |
_hex = '0000009E10032C3C4C56066C6976657569660D6765744C6976696E67496E666F7D0000750800010604745265711D0000680A0A0300000000000000001620FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2600361777656226323031377633322E313131302E33266875796146005C0B1300000000{}2300000000{}3300000000000000000B8C980CA80C' | |
hex_content = _hex.format( format(int(channel), 'X').zfill(8), format(int(sid), 'X').zfill(8) ) | |
for i in range(10): | |
r = sess.post('http://cdn.wup.huya.com/', data = bytearray.fromhex(hex_content)) | |
if r.status_code == 200: | |
break | |
r = r.content.decode('ISO-8859-1') | |
if channel not in r: | |
raise Exception('Offline!!') | |
vid = re.search(r'%s-%s[^f]+'%(channel, sid), r).group(0) | |
wsSecret = re.search(r'wsSecret=([0-9a-z]{32})', r).group(1) | |
wsTime = re.search(r'wsTime=([0-9a-z]{8})', r).group(1) | |
line = re.search(r'://(\w+\.(flv|stream)\.huya\.com/(hqlive|huyalive))', r).group(1) | |
flv_url = 'http://{}/{}.flv?wsSecret={}&wsTime={}'.format(line, vid, wsSecret, wsTime) | |
return (flv_url) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('url') | |
parser.add_argument('ratio',nargs='?',default='') | |
args = parser.parse_args() | |
rid = args.url | |
rat = args.ratio | |
flv_url = get_api(rid) | |
if rat: | |
flv_url = flv_url + "&ratio=" + rat | |
print (flv_url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment