forked from GaryNotGay/API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pptv.py
78 lines (68 loc) · 2.77 KB
/
pptv.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
76
77
78
import json
import requests
requests.packages.urllib3.disable_warnings()
def api_get(id_):
api_url = f"https://web-play.pptv.com/webplay3-0-{id_}.xml?o=0&version=6&type=mhpptv&appid=pptv.web.h5&appplt=web&appver=4.0.7&cb=a"
headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"}
s = requests.get(api_url, verify=False, headers=headers)
data = json.loads(s.text[2:-4])
return data
def outJSON(qua, data):
try:
name = data['childNodes'][2]['nm']
dur = data['childNodes'][2]['dur']
except Exception:
name = data['childNodes'][0]['nm']
dur = data['childNodes'][0]['dur']
json_outstr = {
"VideoName": "",
"VideoLength": "",
}
json_outstr['VideoLength'] =dur
json_outstr['VideoName'] = name
quarr = qua.split(",")
for index in range(len(quarr)):
try:
q = int(quarr[index])
vh = data['childNodes'][q*2+4]['vh']
vw = data['childNodes'][q*2+4]['vw']
hw = str(vw)+"*"+str(vh)
rid = data['childNodes'][q*2+4]['rid'][:-4]
kk = data['childNodes'][q*2+3]['childNodes'][-1]['childNodes'][0].split('%26')[0]
m3u8url = f"https://ksyun.vod.pptv.com/{rid}.m3u8?fpp.ver=1.0.0&k={kk}&type=mhpptv&o=0&sv=4.1.18"
if hw in json_outstr.keys():
hw += "_high"
json_outstr[hw]=m3u8url
except:
return json_outstr
return json_outstr
def querydata2json(data):
MUST = ["cid"]
rj = {}
da = data.split('&')
for i in range(len(da)):
daa = da[i].split("=")
rj[daa[0]] = daa[1]
for key in MUST:
if key not in rj:
return "ERR"
return rj
def handler(environ, start_response):
try:
query_data = querydata2json(environ['QUERY_STRING'])
if query_data == "ERR":
ERR_INFO = {"ERR": "PARAMS ERROR!"}
start_response('200 OK', [('Content-type', 'application/json; charset=utf-8'), ('Access-Control-Allow-Origin', '*')])
return str(ERR_INFO)
cid = query_data["cid"]
if "qua" in query_data:
qua = query_data["qua"]
else:
qua = "0,1,2,3,4"
data = api_get(cid)
outjson = outJSON(qua, data)
start_response('200 OK', [('Content-type', 'application/json; charset=utf-8'), ('Access-Control-Allow-Origin', '*')])
return json.dumps(outjson, ensure_ascii=False)
except Exception as e:
outjson = {"Status": "False", "Message": "未知错误,请参考错误信息,定位原因,或联系作者", "Info": e}
return json.dumps(outjson, ensure_ascii=False)