forked from wbt5/real-url
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zhanqi.py
75 lines (62 loc) · 2.53 KB
/
zhanqi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 获取战旗直播(战旗TV)的真实流媒体地址。https://www.zhanqi.tv/lives
# 默认最高画质
import json
import re
import requests
class ZhanQi:
def __init__(self, rid):
"""
战旗直播间有两种:一种是普通直播间号为数字或字幕;另一种是官方的主题直播间,链接带topic。
所以先判断一次后从网页源代码里获取数字直播间号
Args:
rid:直播间链接,从网页源码里获取真实数字rid
"""
self.s = requests.Session()
res = self.s.get(rid).text
self.rid = re.search(r'"code":"(\d+)"', res).group(1)
def get_real_url(self):
res = self.s.get(f'https://m.zhanqi.tv/api/static/v2.1/room/domain/{self.rid}.json')
try:
res = res.json()
videoid = res['data']['videoId']
status = res['data']['status']
except (KeyError, json.decoder.JSONDecodeError):
raise Exception('Incorrect rid')
if status == '4':
# 获取gid
res = self.s.get('https://www.zhanqi.tv/api/public/room.viewer')
try:
res = res.json()
gid = res['data']['gid']
except KeyError:
raise Exception('Getting gid incorrectly')
# 获取cdn_host
res = self.s.get('https://umc.danuoyi.alicdn.com/dns_resolve_https?app=zqlive&host_key=alhdl-cdn.zhanqi.tv')
cdn_host, *_ = res.json().get('redirect_domain')
# 获取chain_key
data = {
'stream': f'{videoid}.flv',
'cdnKey': 202,
'platform': 128,
}
headers = {
'cookie': f'gid={gid}',
}
res = self.s.post('https://www.zhanqi.tv/api/public/burglar/chain', data=data, headers=headers).json()
chain_key = res['data']['key']
url = f'https://{cdn_host}/alhdl-cdn.zhanqi.tv/zqlive/{videoid}.flv?{chain_key}&playNum=68072487067' \
f'&gId={gid}&ipFrom=1&clientIp=&fhost=h5&platform=128'
return url
else:
raise Exception('No streaming')
def get_real_url(rid):
try:
zq = ZhanQi(rid)
return zq.get_real_url()
except Exception as e:
print('Exception:', e)
return False
if __name__ == '__main__':
# 直播间链接类似:https://www.zhanqi.tv/topic/owl 或 https://www.zhanqi.tv/152600919
r = input('输入战旗直播间的链接:\n')
print(get_real_url(r))