2 from __future__ 
import unicode_literals
 
  11 from .common 
import InfoExtractor
 
  12 from ..compat 
import ( 
  13     compat_urllib_parse_urlencode
, 
  18     get_element_by_attribute
, 
  22 class YoukuIE(InfoExtractor
): 
  27             http://(?:v|player)\.youku\.com/(?:v_show/id_|player\.php/sid/)| 
  29         (?P<id>[A-Za-z0-9]+)(?:\.html|/v\.swf|) 
  34         'url': 'http://v.youku.com/v_show/id_XMTc1ODE5Njcy.html', 
  36             'id': 'XMTc1ODE5Njcy_part1', 
  37             'title': '★Smile﹗♡ Git Fresh -Booty Music舞蹈.', 
  41         'url': 'http://player.youku.com/player.php/sid/XNDgyMDQ2NTQw/v.swf', 
  42         'only_matching': True, 
  44         'url': 'http://v.youku.com/v_show/id_XODgxNjg1Mzk2_ev_1.html', 
  46             'id': 'XODgxNjg1Mzk2', 
  50         'skip': 'Available in China only', 
  52         'url': 'http://v.youku.com/v_show/id_XMTI1OTczNDM5Mg==.html', 
  54             'id': 'XMTI1OTczNDM5Mg', 
  59         'url': 'http://v.youku.com/v_show/id_XNjA1NzA2Njgw.html', 
  60         'note': 'Video protected with password', 
  62             'id': 'XNjA1NzA2Njgw', 
  63             'title': '邢義田复旦讲座之想象中的胡人—从“左衽孔子”说起', 
  67             'videopassword': '100600', 
  70         # /play/get.json contains streams with "channel_type":"tail" 
  71         'url': 'http://v.youku.com/v_show/id_XOTUxMzg4NDMy.html', 
  73             'id': 'XOTUxMzg4NDMy', 
  74             'title': '我的世界☆明月庄主☆车震猎杀☆杀人艺术Minecraft', 
  79     def construct_video_urls(self
, data
): 
  85                 t 
= (t 
+ ls
[i
] + compat_ord(s1
[i 
% len(s1
)])) % 256 
  86                 ls
[i
], ls
[t
] = ls
[t
], ls
[i
] 
  89             for i 
in range(len(s2
)): 
  92                 ls
[x
], ls
[y
] = ls
[y
], ls
[x
] 
  93                 s
.append(compat_ord(s2
[i
]) ^ ls
[(ls
[x
] + ls
[y
]) % 256]) 
  97             b
'becaf9be', base64
.b64decode(data
['security']['encrypt_string'].encode('ascii')) 
  98         ).decode('ascii').split('_') 
 101         oip 
= data
['security']['ip'] 
 104         for stream 
in data
['stream']: 
 105             if stream
.get('channel_type') == 'tail': 
 107             format 
= stream
.get('stream_type') 
 108             fileid 
= stream
['stream_fileid'] 
 109             fileid_dict
[format
] = fileid
 
 111         def get_fileid(format
, n
): 
 112             number 
= hex(int(str(n
), 10))[2:].upper() 
 114                 number 
= '0' + number
 
 115             streamfileids 
= fileid_dict
[format
] 
 116             fileid 
= streamfileids
[0:8] + number 
+ streamfileids
[10:] 
 120         def generate_ep(format
, n
): 
 121             fileid 
= get_fileid(format
, n
) 
 124                 ('%s_%s_%s' % (sid
, fileid
, token
)).encode('ascii') 
 126             ep 
= base64
.b64encode(ep_t
).decode('ascii') 
 129         # generate video_urls 
 131         for stream 
in data
['stream']: 
 132             if stream
.get('channel_type') == 'tail': 
 134             format 
= stream
.get('stream_type') 
 136             for dt 
in stream
['segs']: 
 137                 n 
= str(stream
['segs'].index(dt
)) 
 140                     'hd': self
.get_hd(format
), 
 147                     'ep': generate_ep(format
, n
) 
 150                     'http://k.youku.com/player/getFlvPath/' + \
 
 153                     '/st/' + self
.parse_ext_l(format
) + \
 
 154                     '/fileid/' + get_fileid(format
, n
) + '?' + \
 
 155                     compat_urllib_parse_urlencode(param
) 
 156                 video_urls
.append(video_url
) 
 157             video_urls_dict
[format
] = video_urls
 
 159         return video_urls_dict
 
 163         return '%d%s' % (int(time
.time()), ''.join([ 
 164             random
.choice(string
.ascii_letters
) for i 
in range(3)])) 
 166     def get_hd(self
, fm
): 
 179         return hd_id_dict
[fm
] 
 181     def parse_ext_l(self
, fm
): 
 196     def get_format_name(self
, fm
): 
 211     def _real_extract(self
, url
): 
 212         video_id 
= self
._match
_id
(url
) 
 214         self
._set
_cookie
('youku.com', '__ysuid', self
.get_ysuid()) 
 216         def retrieve_data(req_url
, note
): 
 220             headers
.update(self
.geo_verification_headers()) 
 221             self
._set
_cookie
('youku.com', 'xreferrer', 'http://www.youku.com') 
 223             raw_data 
= self
._download
_json
(req_url
, video_id
, note
=note
, headers
=headers
) 
 225             return raw_data
['data'] 
 227         video_password 
= self
._downloader
.params
.get('videopassword') 
 230         basic_data_url 
= 'http://play.youku.com/play/get.json?vid=%s&ct=12' % video_id
 
 232             basic_data_url 
+= '&pwd=%s' % video_password
 
 234         data 
= retrieve_data(basic_data_url
, 'Downloading JSON metadata') 
 236         error 
= data
.get('error') 
 238             error_note 
= error
.get('note') 
 239             if error_note 
is not None and '因版权原因无法观看此视频' in error_note
: 
 240                 raise ExtractorError( 
 241                     'Youku said: Sorry, this video is available in China only', expected
=True) 
 242             elif error_note 
and '该视频被设为私密' in error_note
: 
 243                 raise ExtractorError( 
 244                     'Youku said: Sorry, this video is private', expected
=True) 
 246                 msg 
= 'Youku server reported error %i' % error
.get('code') 
 247                 if error_note 
is not None: 
 248                     msg 
+= ': ' + error_note
 
 249                 raise ExtractorError(msg
) 
 252         title 
= data
['video']['title'] 
 254         # generate video_urls_dict 
 255         video_urls_dict 
= self
.construct_video_urls(data
) 
 259             'id': '%s_part%d' % (video_id
, i 
+ 1), 
 262             # some formats are not available for all parts, we have to detect 
 264         } for i 
in range(max(len(v
.get('segs')) for v 
in data
['stream']))] 
 265         for stream 
in data
['stream']: 
 266             if stream
.get('channel_type') == 'tail': 
 268             fm 
= stream
.get('stream_type') 
 269             video_urls 
= video_urls_dict
[fm
] 
 270             for video_url
, seg
, entry 
in zip(video_urls
, stream
['segs'], entries
): 
 271                 entry
['formats'].append({ 
 273                     'format_id': self
.get_format_name(fm
), 
 274                     'ext': self
.parse_ext_l(fm
), 
 275                     'filesize': int(seg
['size']), 
 276                     'width': stream
.get('width'), 
 277                     'height': stream
.get('height'), 
 281             '_type': 'multi_video', 
 288 class YoukuShowIE(InfoExtractor
): 
 289     _VALID_URL 
= r
'https?://(?:www\.)?youku\.com/show_page/id_(?P<id>[0-9a-z]+)\.html' 
 290     IE_NAME 
= 'youku:show' 
 293         'url': 'http://www.youku.com/show_page/id_zc7c670be07ff11e48b3f.html', 
 295             'id': 'zc7c670be07ff11e48b3f', 
 297             'description': 'md5:578d4f2145ae3f9128d9d4d863312910', 
 299         'playlist_count': 50, 
 304     def _find_videos_in_page(self
, webpage
): 
 306             r
'<li><a[^>]+href="(?P<url>https?://v\.youku\.com/[^"]+)"[^>]+title="(?P<title>[^"]+)"', webpage
) 
 308             self
.url_result(video_url
, YoukuIE
.ie_key(), title
) 
 309             for video_url
, title 
in videos
] 
 311     def _real_extract(self
, url
): 
 312         show_id 
= self
._match
_id
(url
) 
 313         webpage 
= self
._download
_webpage
(url
, show_id
) 
 315         entries 
= self
._find
_videos
_in
_page
(webpage
) 
 317         playlist_title 
= self
._html
_search
_regex
( 
 318             r
'<span[^>]+class="name">([^<]+)</span>', webpage
, 'playlist title', fatal
=False) 
 319         detail_div 
= get_element_by_attribute('class', 'detail', webpage
) or '' 
 320         playlist_description 
= self
._html
_search
_regex
( 
 321             r
'<span[^>]+style="display:none"[^>]*>([^<]+)</span>', 
 322             detail_div
, 'playlist description', fatal
=False) 
 324         for idx 
in itertools
.count(1): 
 325             episodes_page 
= self
._download
_webpage
( 
 326                 'http://www.youku.com/show_episode/id_%s.html' % show_id
, 
 327                 show_id
, query
={'divid': 'reload_%d' % (idx 
* self
._PAGE
_SIZE 
+ 1)}, 
 328                 note
='Downloading episodes page %d' % idx
) 
 329             new_entries 
= self
._find
_videos
_in
_page
(episodes_page
) 
 330             entries
.extend(new_entries
) 
 331             if len(new_entries
) < self
._PAGE
_SIZE
: 
 334         return self
.playlist_result(entries
, show_id
, playlist_title
, playlist_description
)