[iqiyi] Comment out some MD5 sums
[youtube-dl] / youtube_dl / extractor / iqiyi.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import hashlib
5 import math
6 import random
7 import time
8 import uuid
9
10 from .common import InfoExtractor
11 from ..compat import compat_urllib_parse
12 from ..utils import ExtractorError
13
14
15 class IqiyiIE(InfoExtractor):
16     IE_NAME = 'iqiyi'
17     IE_DESC = '爱奇艺'
18
19     _VALID_URL = r'http://(?:www\.)iqiyi.com/v_.+?\.html'
20
21     _TESTS = [{
22         'url': 'http://www.iqiyi.com/v_19rrojlavg.html',
23         'md5': '2cb594dc2781e6c941a110d8f358118b',
24         'info_dict': {
25             'id': '9c1fb1b99d192b21c559e5a1a2cb3c73',
26             'title': '美国德州空中惊现奇异云团 酷似UFO',
27             'ext': 'f4v',
28         }
29     }, {
30         'url': 'http://www.iqiyi.com/v_19rrhnnclk.html',
31         'info_dict': {
32             'id': 'e3f585b550a280af23c98b6cb2be19fb',
33             'title': '名侦探柯南第752集',
34         },
35         'playlist': [{
36             'info_dict': {
37                 'id': 'e3f585b550a280af23c98b6cb2be19fb_part1',
38                 'ext': 'f4v',
39                 'title': '名侦探柯南第752集',
40             },
41         }, {
42             'info_dict': {
43                 'id': 'e3f585b550a280af23c98b6cb2be19fb_part2',
44                 'ext': 'f4v',
45                 'title': '名侦探柯南第752集',
46             },
47         }, {
48             'info_dict': {
49                 'id': 'e3f585b550a280af23c98b6cb2be19fb_part3',
50                 'ext': 'f4v',
51                 'title': '名侦探柯南第752集',
52             },
53         }, {
54             'info_dict': {
55                 'id': 'e3f585b550a280af23c98b6cb2be19fb_part4',
56                 'ext': 'f4v',
57                 'title': '名侦探柯南第752集',
58             },
59         }, {
60             'info_dict': {
61                 'id': 'e3f585b550a280af23c98b6cb2be19fb_part5',
62                 'ext': 'f4v',
63                 'title': '名侦探柯南第752集',
64             },
65         }, {
66             'info_dict': {
67                 'id': 'e3f585b550a280af23c98b6cb2be19fb_part6',
68                 'ext': 'f4v',
69                 'title': '名侦探柯南第752集',
70             },
71         }, {
72             'info_dict': {
73                 'id': 'e3f585b550a280af23c98b6cb2be19fb_part7',
74                 'ext': 'f4v',
75                 'title': '名侦探柯南第752集',
76             },
77         }, {
78             'info_dict': {
79                 'id': 'e3f585b550a280af23c98b6cb2be19fb_part8',
80                 'ext': 'f4v',
81                 'title': '名侦探柯南第752集',
82             },
83         }],
84     }]
85
86     _FORMATS_MAP = [
87         ('1', 'h6'),
88         ('2', 'h5'),
89         ('3', 'h4'),
90         ('4', 'h3'),
91         ('5', 'h2'),
92         ('10', 'h1'),
93     ]
94
95     def construct_video_urls(self, data, video_id, _uuid):
96         def do_xor(x, y):
97             a = y % 3
98             if a == 1:
99                 return x ^ 121
100             if a == 2:
101                 return x ^ 72
102             return x ^ 103
103
104         def get_encode_code(l):
105             a = 0
106             b = l.split('-')
107             c = len(b)
108             s = ''
109             for i in range(c - 1, -1, -1):
110                 a = do_xor(int(b[c - i - 1], 16), i)
111                 s += chr(a)
112             return s[::-1]
113
114         def get_path_key(x, format_id, segment_index):
115             mg = ')(*&^flash@#$%a'
116             tm = self._download_json(
117                 'http://data.video.qiyi.com/t?tn=' + str(random.random()), video_id,
118                 note='Download path key of segment %d for format %s' % (segment_index + 1, format_id)
119             )['t']
120             t = str(int(math.floor(int(tm) / (600.0))))
121             return hashlib.md5((t + mg + x).encode('utf8')).hexdigest()
122
123         video_urls_dict = {}
124         for format_item in data['vp']['tkl'][0]['vs']:
125             if 0 < int(format_item['bid']) <= 10:
126                 format_id = self.get_format(format_item['bid'])
127             else:
128                 continue
129
130             video_urls = []
131
132             video_urls_info = format_item['fs']
133             if not format_item['fs'][0]['l'].startswith('/'):
134                 t = get_encode_code(format_item['fs'][0]['l'])
135                 if t.endswith('mp4'):
136                     video_urls_info = format_item['flvs']
137
138             for segment_index, segment in enumerate(video_urls_info):
139                 vl = segment['l']
140                 if not vl.startswith('/'):
141                     vl = get_encode_code(vl)
142                 key = get_path_key(
143                     vl.split('/')[-1].split('.')[0], format_id, segment_index)
144                 filesize = segment['b']
145                 base_url = data['vp']['du'].split('/')
146                 base_url.insert(-1, key)
147                 base_url = '/'.join(base_url)
148                 param = {
149                     'su': _uuid,
150                     'qyid': uuid.uuid4().hex,
151                     'client': '',
152                     'z': '',
153                     'bt': '',
154                     'ct': '',
155                     'tn': str(int(time.time()))
156                 }
157                 api_video_url = base_url + vl + '?' + \
158                     compat_urllib_parse.urlencode(param)
159                 js = self._download_json(
160                     api_video_url, video_id,
161                     note='Download video info of segment %d for format %s' % (segment_index + 1, format_id))
162                 video_url = js['l']
163                 video_urls.append(
164                     (video_url, filesize))
165
166             video_urls_dict[format_id] = video_urls
167         return video_urls_dict
168
169     def get_format(self, bid):
170         matched_format_ids = [_format_id for _bid, _format_id in self._FORMATS_MAP if _bid == str(bid)]
171         return matched_format_ids[0] if len(matched_format_ids) else None
172
173     def get_bid(self, format_id):
174         matched_bids = [_bid for _bid, _format_id in self._FORMATS_MAP if _format_id == format_id]
175         return matched_bids[0] if len(matched_bids) else None
176
177     def get_raw_data(self, tvid, video_id, enc_key, _uuid):
178         tm = str(int(time.time()))
179         param = {
180             'key': 'fvip',
181             'src': hashlib.md5(b'youtube-dl').hexdigest(),
182             'tvId': tvid,
183             'vid': video_id,
184             'vinfo': 1,
185             'tm': tm,
186             'enc': hashlib.md5(
187                 (enc_key + tm + tvid).encode('utf8')).hexdigest(),
188             'qyid': _uuid,
189             'tn': random.random(),
190             'um': 0,
191             'authkey': hashlib.md5(
192                 (tm + tvid).encode('utf8')).hexdigest()
193         }
194
195         api_url = 'http://cache.video.qiyi.com/vms' + '?' + \
196             compat_urllib_parse.urlencode(param)
197         raw_data = self._download_json(api_url, video_id)
198         return raw_data
199
200     def get_enc_key(self, swf_url, video_id):
201         enc_key = '8e29ab5666d041c3a1ea76e06dabdffb'
202         return enc_key
203
204     def _real_extract(self, url):
205         webpage = self._download_webpage(
206             url, 'temp_id', note='download video page')
207         tvid = self._search_regex(
208             r'data-player-tvid\s*=\s*[\'"](\d+)', webpage, 'tvid')
209         video_id = self._search_regex(
210             r'data-player-videoid\s*=\s*[\'"]([a-f\d]+)', webpage, 'video_id')
211         swf_url = self._search_regex(
212             r'(http://[^\'"]+MainPlayer[^.]+\.swf)', webpage, 'swf player URL')
213         _uuid = uuid.uuid4().hex
214
215         enc_key = self.get_enc_key(swf_url, video_id)
216
217         raw_data = self.get_raw_data(tvid, video_id, enc_key, _uuid)
218
219         if raw_data['code'] != 'A000000':
220             raise ExtractorError('Unable to load data. Error code: ' + raw_data['code'])
221
222         if not raw_data['data']['vp']['tkl']:
223             raise ExtractorError('No support iQiqy VIP video')
224
225         data = raw_data['data']
226
227         title = data['vi']['vn']
228
229         # generate video_urls_dict
230         video_urls_dict = self.construct_video_urls(
231             data, video_id, _uuid)
232
233         # construct info
234         entries = []
235         for format_id in video_urls_dict:
236             video_urls = video_urls_dict[format_id]
237             for i, video_url_info in enumerate(video_urls):
238                 if len(entries) < i + 1:
239                     entries.append({'formats': []})
240                 entries[i]['formats'].append(
241                     {
242                         'url': video_url_info[0],
243                         'filesize': video_url_info[-1],
244                         'format_id': format_id,
245                         'preference': int(self.get_bid(format_id))
246                     }
247                 )
248
249         for i in range(len(entries)):
250             self._sort_formats(entries[i]['formats'])
251             entries[i].update(
252                 {
253                     'id': '%s_part%d' % (video_id, i + 1),
254                     'title': title,
255                 }
256             )
257
258         if len(entries) > 1:
259             info = {
260                 '_type': 'multi_video',
261                 'id': video_id,
262                 'title': title,
263                 'entries': entries,
264             }
265         else:
266             info = entries[0]
267             info['id'] = video_id
268             info['title'] = title
269
270         return info