forked from ZSAIm/iqiyi-parser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiqiyi_parse.py
207 lines (177 loc) · 6.05 KB
/
iqiyi_parse.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import cookielib
import random
import time
import re
import gzip, zlib
from io import BytesIO
import json
import urllib, urllib2
from BeautifulSoup import BeautifulSoup
import PyV8
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
def raw_decompress(data, headers_msg):
encoding = headers_msg.get('Content-Encoding')
if encoding == 'gzip':
src_stream = BytesIO(data)
data = gzip.GzipFile(fileobj=src_stream).read()
elif encoding == 'deflate':
data = zlib.decompress(data)
return data
HEADERS = {
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36',
'Accept': '*/*',
'Referer': 'http://www.iqiyi.com/',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
}
global_params = {
'tvid': '',
'bid': '',
'vid': '',
'src': '01010031010000000000',
'vt': '0',
'rs': '1',
'uid': '',
'ori': "pcw",
'ps': '0', # ps: o.switchvd ? 1 : 0,
'tm': '',
'qd_v': "1",
'k_uid': '',
'pt': '0',
'd': '0',
's': "",
'lid': "",
'cf': "",
'ct': "",
'authKey': '',
'k_tag': '1',
'ppt': '0',
'dfp': '',
'locale': 'zh_cn',
'prio': '{"ff": "mp4", "code": 2}',
'pck': '',
'k_err_retries': '0',
'ut': '0',
'bop': '{"version":"7.0","dfp":""}',
'callback': '',
}
class Iqiyi:
def __init__(self):
self.cookiejar = None
self.opener = None
self.QC005 = None
self.QP001 = '1'
# self.uid = None
self.tvid = None
self.init_opener()
def init_opener(self):
self.cookiejar = cookielib.CookieJar()
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookiejar))
self.opener.addheaders = list(HEADERS.items())
self.QC005 = self.make_random_id()
# self.uid = self.make_random_id()
self.set_cookie('QC005', self.QC005, 'iqiyi.com', '/')
self.set_cookie('QP001', self.QP001, 'iqiyi.com', '/')
def set_cookie(self, name, value, domain, path):
self.cookiejar.set_cookie(cookielib.Cookie(0, name, value, None, False, domain, True, False, path, True, False,
None, False, None, None, None))
def make_random_id(self):
chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
id = ""
for index in range(0, 32):
id += chars[random.randint(0, 15)]
return id
def parse(self, url, bids=[600]):
"""bid = [100, 200, 300, 400, 500, 600]
mean video resolution. the higher bid the higher definition
it need a list
like [600].
"""
# videoname , tvid
req = urllib2.Request(url)
res = self.opener.open(req)
raw = res.read()
text = str(raw_decompress(raw, res.info()))
soup = BeautifulSoup(text)
videoname = soup.title.string
tvid = re.search('''param\['tvid'\] = "(.+?)"''', text, re.S).group(1)
self.tvid = tvid
vid = re.search('''param\['vid'\] = "(.+?)"''', text, re.I | re.S).group(1)
uid = self.make_random_id()
videos_msg = {}
for bid in bids:
json_msg = self.__parse(tvid, vid, uid, bid)
if not json_msg:
continue
msg = json_msg['data']['program']['video']
# scrsz = ''
sel = None
for i in msg:
if 'fs' in i and i['_selected'] is True:
sel = i
# scrsz = i['scrsz']
# for j, k in i:
# fs.append(i['fs'])
videos_msg[sel['scrsz']] = {'fs': sel['fs'], 'vsize': sel['vsize'], 'ff': sel['ff'], 'bid': sel['bid'], '@json': msg}
return videoname, videos_msg
def __parse(self, tvid, vid, uid, bid):
js_context = ''
with open("ArrayBuffer.js", 'r') as f:
js_context += f.read()
with open("pcweb.js", 'r') as f:
js_context += f.read()
time_str = str(int(time.time() * 1000))
ctx = PyV8.JSContext()
with PyV8.JSLocker():
ctx.enter()
ctx.eval(js_context)
authkey = ctx.locals.authkey(ctx.locals.authkey('') + time_str + tvid)
callback = ctx.locals.callback()
params = {
'tvid': tvid,
'vid': vid,
'bid': str(bid),
'tm': time_str,
'k_uid': uid,
'callback': callback,
'authKey': authkey,
}
global_params.update(params)
params_encode = urllib.urlencode(global_params)
path_get = '/jp/dash?' + params_encode
vf = ctx.locals.vf(path_get)
path_get += '&vf=%s' % vf
ctx.leave()
req = urllib2.Request('http://cache.video.iqiyi.com/' + path_get.lstrip('/'))
res = self.opener.open(req)
raw = res.read()
text = raw_decompress(raw, res.info())
json_str = re.search('try{\w{0,}\((.+})(\s)?\);}catch', text).group(1)
if json_str:
ret = json.loads(json_str)
else:
ret = None
res.close()
return ret
def activate_path(self, path):
params = {
'cross-domain': '1',
'qyid': self.QC005,
'qypid': '%s__02020031010000000000' % self.tvid,
'rn': str(int(time.time() * 1000)),
'pv': '0.1',
}
encode_params = urllib.urlencode(params)
req = urllib2.Request('http://data.video.iqiyi.com/videos/%s' % path.lstrip('/'), encode_params)
res = self.opener.open(req)
raw = res.read()
text = raw_decompress(raw, res.info())
msg = json.loads(text)
return msg['l'], msg
if __name__ == "__main__":
a = Iqiyi()
# b = a.parse('http://www.iqiyi.com/a_19rrgxwtfh.html')
b = a.parse('https://www.iqiyi.com/a_19rrhdfh2l.html')
pass