]>
Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/iqiyi.py
   2 from __future__ 
import unicode_literals
 
  10 from .common 
import InfoExtractor
 
  11 from ..compat 
import compat_urllib_parse
 
  12 from ..utils 
import ExtractorError
 
  15 class IqiyiIE(InfoExtractor
): 
  19     _VALID_URL 
= r
'http://(?:www\.)iqiyi.com/v_.+?\.html' 
  22         'url': 'http://www.iqiyi.com/v_19rrojlavg.html', 
  23         'md5': '2cb594dc2781e6c941a110d8f358118b', 
  25             'id': '9c1fb1b99d192b21c559e5a1a2cb3c73', 
  26             'title': '美国德州空中惊现奇异云团 酷似UFO', 
  30         'url': 'http://www.iqiyi.com/v_19rrhnnclk.html', 
  32             'id': 'e3f585b550a280af23c98b6cb2be19fb', 
  33             'title': '名侦探柯南第752集', 
  37                 'id': 'e3f585b550a280af23c98b6cb2be19fb_part1', 
  39                 'title': '名侦探柯南第752集', 
  43                 'id': 'e3f585b550a280af23c98b6cb2be19fb_part2', 
  45                 'title': '名侦探柯南第752集', 
  49                 'id': 'e3f585b550a280af23c98b6cb2be19fb_part3', 
  51                 'title': '名侦探柯南第752集', 
  55                 'id': 'e3f585b550a280af23c98b6cb2be19fb_part4', 
  57                 'title': '名侦探柯南第752集', 
  61                 'id': 'e3f585b550a280af23c98b6cb2be19fb_part5', 
  63                 'title': '名侦探柯南第752集', 
  67                 'id': 'e3f585b550a280af23c98b6cb2be19fb_part6', 
  69                 'title': '名侦探柯南第752集', 
  73                 'id': 'e3f585b550a280af23c98b6cb2be19fb_part7', 
  75                 'title': '名侦探柯南第752集', 
  79                 'id': 'e3f585b550a280af23c98b6cb2be19fb_part8', 
  81                 'title': '名侦探柯南第752集', 
  85             'skip_download': True, 
  98     def construct_video_urls(self
, data
, video_id
, _uuid
): 
 107         def get_encode_code(l
): 
 112             for i 
in range(c 
- 1, -1, -1): 
 113                 a 
= do_xor(int(b
[c 
- i 
- 1], 16), i
) 
 117         def get_path_key(x
, format_id
, segment_index
): 
 118             mg 
= ')(*&^flash@#$%a' 
 119             tm 
= self
._download
_json
( 
 120                 'http://data.video.qiyi.com/t?tn=' + str(random
.random()), video_id
, 
 121                 note
='Download path key of segment %d for format %s' % (segment_index 
+ 1, format_id
) 
 123             t 
= str(int(math
.floor(int(tm
) / (600.0)))) 
 124             return hashlib
.md5((t 
+ mg 
+ x
).encode('utf8')).hexdigest() 
 127         for format_item 
in data
['vp']['tkl'][0]['vs']: 
 128             if 0 < int(format_item
['bid']) <= 10: 
 129                 format_id 
= self
.get_format(format_item
['bid']) 
 135             video_urls_info 
= format_item
['fs'] 
 136             if not format_item
['fs'][0]['l'].startswith('/'): 
 137                 t 
= get_encode_code(format_item
['fs'][0]['l']) 
 138                 if t
.endswith('mp4'): 
 139                     video_urls_info 
= format_item
['flvs'] 
 141             for segment_index
, segment 
in enumerate(video_urls_info
): 
 143                 if not vl
.startswith('/'): 
 144                     vl 
= get_encode_code(vl
) 
 146                     vl
.split('/')[-1].split('.')[0], format_id
, segment_index
) 
 147                 filesize 
= segment
['b'] 
 148                 base_url 
= data
['vp']['du'].split('/') 
 149                 base_url
.insert(-1, key
) 
 150                 base_url 
= '/'.join(base_url
) 
 153                     'qyid': uuid
.uuid4().hex, 
 158                     'tn': str(int(time
.time())) 
 160                 api_video_url 
= base_url 
+ vl 
+ '?' + \
 
 161                     compat_urllib_parse
.urlencode(param
) 
 162                 js 
= self
._download
_json
( 
 163                     api_video_url
, video_id
, 
 164                     note
='Download video info of segment %d for format %s' % (segment_index 
+ 1, format_id
)) 
 167                     (video_url
, filesize
)) 
 169             video_urls_dict
[format_id
] = video_urls
 
 170         return video_urls_dict
 
 172     def get_format(self
, bid
): 
 173         matched_format_ids 
= [_format_id 
for _bid
, _format_id 
in self
._FORMATS
_MAP 
if _bid 
== str(bid
)] 
 174         return matched_format_ids
[0] if len(matched_format_ids
) else None 
 176     def get_bid(self
, format_id
): 
 177         matched_bids 
= [_bid 
for _bid
, _format_id 
in self
._FORMATS
_MAP 
if _format_id 
== format_id
] 
 178         return matched_bids
[0] if len(matched_bids
) else None 
 180     def get_raw_data(self
, tvid
, video_id
, enc_key
, _uuid
): 
 181         tm 
= str(int(time
.time())) 
 184             'src': hashlib
.md5(b
'youtube-dl').hexdigest(), 
 190                 (enc_key 
+ tm 
+ tvid
).encode('utf8')).hexdigest(), 
 192             'tn': random
.random(), 
 194             'authkey': hashlib
.md5( 
 195                 (tm 
+ tvid
).encode('utf8')).hexdigest() 
 198         api_url 
= 'http://cache.video.qiyi.com/vms' + '?' + \
 
 199             compat_urllib_parse
.urlencode(param
) 
 200         raw_data 
= self
._download
_json
(api_url
, video_id
) 
 203     def get_enc_key(self
, swf_url
, video_id
): 
 204         enc_key 
= '8e29ab5666d041c3a1ea76e06dabdffb' 
 207     def _real_extract(self
, url
): 
 208         webpage 
= self
._download
_webpage
( 
 209             url
, 'temp_id', note
='download video page') 
 210         tvid 
= self
._search
_regex
( 
 211             r
'data-player-tvid\s*=\s*[\'"](\d+)', webpage, 'tvid') 
 212         video_id = self._search_regex( 
 213             r'data-player-videoid\s*=\s*[\'"]([a
-f\d
]+)', webpage, 'video_id
') 
 214         swf_url = self._search_regex( 
 215             r'(http
://[^
\'"]+MainPlayer[^.]+\.swf)', webpage, 'swf player URL') 
 216         _uuid = uuid.uuid4().hex 
 218         enc_key = self.get_enc_key(swf_url, video_id) 
 220         raw_data = self.get_raw_data(tvid, video_id, enc_key, _uuid) 
 222         if raw_data['code'] != 'A000000': 
 223             raise ExtractorError('Unable to load data. Error code: ' + raw_data['code']) 
 225         if not raw_data['data']['vp']['tkl']: 
 226             raise ExtractorError('No support iQiqy VIP video') 
 228         data = raw_data['data'] 
 230         title = data['vi']['vn'] 
 232         # generate video_urls_dict 
 233         video_urls_dict = self.construct_video_urls( 
 234             data, video_id, _uuid) 
 238         for format_id in video_urls_dict: 
 239             video_urls = video_urls_dict[format_id] 
 240             for i, video_url_info in enumerate(video_urls): 
 241                 if len(entries) < i + 1: 
 242                     entries.append({'formats': []}) 
 243                 entries[i]['formats'].append( 
 245                         'url': video_url_info[0], 
 246                         'filesize': video_url_info[-1], 
 247                         'format_id': format_id, 
 248                         'preference': int(self.get_bid(format_id)) 
 252         for i in range(len(entries)): 
 253             self._sort_formats(entries[i]['formats']) 
 256                     'id': '%s_part%d' % (video_id, i + 1), 
 263                 '_type': 'multi_video', 
 270             info['id'] = video_id 
 271             info['title'] = title