Skip to content

Instantly share code, notes, and snippets.

@Justsoos
Last active August 25, 2021 16:58
Show Gist options
  • Select an option

  • Save Justsoos/a3a583bcf6d8e775da449e5641e99a2a to your computer and use it in GitHub Desktop.

Select an option

Save Justsoos/a3a583bcf6d8e775da449e5641e99a2a to your computer and use it in GitHub Desktop.
huya.com api,直播流URL提取
#! /usr/bin/env python3
'''
import time
import hashlib
import random
import sys
import subprocess
'''
import re
import requests
import argparse
def get_api(url):
#r = requests.get('http://www.huya.com/{}'.format(room_id))
r = requests.get(url)
channel = re.search(r'"channel":"(\d+)"*,', r.text).group(1)
sid = re.search(r'"sid":"(\d+)"*,', r.text).group(1)
if channel == '0' or sid == '0':
print('Offline')
raise Exception('Offline')
_hex = '0000009E10032C3C4C56066C6976657569660D6765744C6976696E67496E666F7D0000750800010604745265711D0000680A0A0300000000000000001620FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2600361777656226323031377633322E313131302E33266875796146005C0B1300000000{}2300000000{}3300000000000000000B8C980CA80C'
hex_content = _hex.format( format(int(channel), 'X').zfill(8), format(int(sid), 'X').zfill(8) )
r = requests.post('http://cdn.wup.huya.com/', data = bytearray.fromhex(hex_content))
if channel not in r.text:
print('Offline')
raise Exception('Offline')
vid = re.search(r'%s-%s[^f]+'%(channel, sid), r.text).group(0)
wsSecret = re.search(r'wsSecret=([0-9a-z]{32})', r.text).group(1)
wsTime = re.search(r'wsTime=([0-9a-z]{8})', r.text).group(1)
line = re.search(r'://(.+\.(flv|stream)\.huya\.com/(hqlive|huyalive))', r.text).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)
@Justsoos
Copy link
Author

@Justsoos
Copy link
Author

Justsoos commented Jan 24, 2018

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/bd54b9182453849c362e187044e288a9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment