Last active
August 25, 2021 16:58
-
-
Save Justsoos/bd54b9182453849c362e187044e288a9 to your computer and use it in GitHub Desktop.
huya.com 流提取,不用 api,直接页面抓
This file contains 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 sys | |
import json | |
import logging | |
import requests | |
import argparse | |
import html | |
import base64 | |
sess = requests.Session() | |
logging.getLogger().setLevel(logging.DEBUG) | |
def get_api(url, num): | |
logging.debug('') | |
FAKE_HEADERS = { | |
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', | |
'Accept-Charset': 'UTF-8,*;q=0.5', | |
'Accept-Encoding': 'gzip,deflate,sdch', | |
'Accept-Language': 'en-US,en;q=0.8', | |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0', | |
} | |
sess.headers = FAKE_HEADERS | |
sess.headers.update({'referer': url}) | |
url=re.sub(r'.*huya.com','https://www.huya.com',url) | |
try: | |
r = sess.get(url, allow_redirects=True, verify=True) | |
r = html.unescape(r.text) | |
#logging.debug(r) | |
m = re.search(r'直播间地址已更换.*?href=\"(http.*?)\"', r) | |
if m: | |
r = sess.get(m.group(1), verify=True) | |
r = html.unescape(r.text) | |
logging.info("主播直播间地址已更换: %s",m.group(1)) | |
p = re.compile(r'hyPlayerConfig = ({.*?});', flags=re.S|re.M) | |
s = re.search(p, r) | |
assert s, "not Found the hyPlayerConfig" | |
s = json.loads(s.group(1)) | |
if s.get('stream') == None: | |
print("Offline!!!") | |
sys.exit() | |
if isinstance(s.get('stream'), str): | |
ss = json.loads(b64decode_u(s.get('stream'))) | |
else: | |
ss = s.get('stream') | |
info = "\n\n" + ss['data'][0]['gameLiveInfo']['nick'] + ": " + ss['data'][0]['gameLiveInfo']['introduction'] | |
logging.info(info) | |
except AttributeError: | |
raise AttributeError('May wrong URL, pls check ...') | |
s_info = ss['data'][0]['gameStreamInfoList'] | |
i = 1 | |
for j in s_info: | |
sFlvUrl = j.get('sFlvUrl') | |
sStreamName = j.get('sStreamName') | |
sFlvAntiCode = html.unescape(j.get('sFlvAntiCode')) | |
flv_url = '{}/{}.flv?{}'.format(sFlvUrl, sStreamName, sFlvAntiCode) | |
if rat: | |
flv_url = flv_url + "&ratio=" + rat | |
if i == num: | |
return(flv_url) | |
else: | |
i = i+1 | |
def b64decode_u(data): | |
if type(data) == str: | |
return to_str(base64.urlsafe_b64decode(data+'='*(4-len(data)%4))) | |
return dat | |
def to_str(s): | |
if type(s) == bytes: | |
return s.decode('utf-8') | |
return s | |
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 | |
num = 2 | |
flv_url = get_api(rid, num) | |
print(flv_url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage:
#huya_api.py http://www.huya.com/lck 4000
4000 is freely custom stream rate, not very certain, like 1500, 2000, 3000, etc all works
also see: https://gist.github.com/Justsoos/a3a583bcf6d8e775da449e5641e99a2a