]>
Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/youku.py
2 from __future__
import unicode_literals
9 from .common
import InfoExtractor
10 from ..compat
import (
20 class YoukuIE(InfoExtractor
):
25 http://(?:v|player)\.youku\.com/(?:v_show/id_|player\.php/sid/)|
27 (?P<id>[A-Za-z0-9]+)(?:\.html|/v\.swf|)
32 'url': 'http://v.youku.com/v_show/id_XMTc1ODE5Njcy.html',
34 'id': 'XMTc1ODE5Njcy_part1',
35 'title': '★Smile﹗♡ Git Fresh -Booty Music舞蹈.',
39 'url': 'http://player.youku.com/player.php/sid/XNDgyMDQ2NTQw/v.swf',
40 'only_matching': True,
42 'url': 'http://v.youku.com/v_show/id_XODgxNjg1Mzk2_ev_1.html',
44 'id': 'XODgxNjg1Mzk2',
48 'skip': 'Available in China only',
50 'url': 'http://v.youku.com/v_show/id_XMTI1OTczNDM5Mg==.html',
52 'id': 'XMTI1OTczNDM5Mg',
57 'url': 'http://v.youku.com/v_show/id_XNjA1NzA2Njgw.html',
58 'note': 'Video protected with password',
60 'id': 'XNjA1NzA2Njgw',
61 'title': '邢義田复旦讲座之想象中的胡人—从“左衽孔子”说起',
65 'videopassword': '100600',
69 def construct_video_urls(self
, data
):
75 t
= (t
+ ls
[i
] + compat_ord(s1
[i
% len(s1
)])) % 256
76 ls
[i
], ls
[t
] = ls
[t
], ls
[i
]
79 for i
in range(len(s2
)):
82 ls
[x
], ls
[y
] = ls
[y
], ls
[x
]
83 s
.append(compat_ord(s2
[i
]) ^ ls
[(ls
[x
] + ls
[y
]) % 256])
87 b
'becaf9be', base64
.b64decode(data
['security']['encrypt_string'].encode('ascii'))
88 ).decode('ascii').split('_')
91 oip
= data
['security']['ip']
94 for stream
in data
['stream']:
95 format
= stream
.get('stream_type')
96 fileid
= stream
['stream_fileid']
97 fileid_dict
[format
] = fileid
99 def get_fileid(format
, n
):
100 number
= hex(int(str(n
), 10))[2:].upper()
102 number
= '0' + number
103 streamfileids
= fileid_dict
[format
]
104 fileid
= streamfileids
[0:8] + number
+ streamfileids
[10:]
108 def generate_ep(format
, n
):
109 fileid
= get_fileid(format
, n
)
112 ('%s_%s_%s' % (sid
, fileid
, token
)).encode('ascii')
114 ep
= base64
.b64encode(ep_t
).decode('ascii')
117 # generate video_urls
119 for stream
in data
['stream']:
120 format
= stream
.get('stream_type')
122 for dt
in stream
['segs']:
123 n
= str(stream
['segs'].index(dt
))
126 'hd': self
.get_hd(format
),
133 'ep': generate_ep(format
, n
)
136 'http://k.youku.com/player/getFlvPath/' + \
139 '/st/' + self
.parse_ext_l(format
) + \
140 '/fileid/' + get_fileid(format
, n
) + '?' + \
141 compat_urllib_parse
.urlencode(param
)
142 video_urls
.append(video_url
)
143 video_urls_dict
[format
] = video_urls
145 return video_urls_dict
149 return '%d%s' % (int(time
.time()), ''.join([
150 random
.choice(string
.ascii_letters
) for i
in range(3)]))
152 def get_hd(self
, fm
):
165 return hd_id_dict
[fm
]
167 def parse_ext_l(self
, fm
):
182 def get_format_name(self
, fm
):
197 def _real_extract(self
, url
):
198 video_id
= self
._match
_id
(url
)
200 self
._set
_cookie
('youku.com', '__ysuid', self
.get_ysuid())
202 def retrieve_data(req_url
, note
):
206 self
._set
_cookie
('youku.com', 'xreferrer', 'http://www.youku.com')
207 req
= sanitized_Request(req_url
, headers
=headers
)
209 cn_verification_proxy
= self
._downloader
.params
.get('cn_verification_proxy')
210 if cn_verification_proxy
:
211 req
.add_header('Ytdl-request-proxy', cn_verification_proxy
)
213 raw_data
= self
._download
_json
(req
, video_id
, note
=note
)
215 return raw_data
['data']
217 video_password
= self
._downloader
.params
.get('videopassword')
220 basic_data_url
= 'http://play.youku.com/play/get.json?vid=%s&ct=12' % video_id
222 basic_data_url
+= '&pwd=%s' % video_password
224 data
= retrieve_data(basic_data_url
, 'Downloading JSON metadata')
226 error
= data
.get('error')
228 error_note
= error
.get('note')
229 if error_note
is not None and '因版权原因无法观看此视频' in error_note
:
230 raise ExtractorError(
231 'Youku said: Sorry, this video is available in China only', expected
=True)
232 elif error_note
and '该视频被设为私密' in error_note
:
233 raise ExtractorError(
234 'Youku said: Sorry, this video is private', expected
=True)
236 msg
= 'Youku server reported error %i' % error
.get('code')
237 if error_note
is not None:
238 msg
+= ': ' + error_note
239 raise ExtractorError(msg
)
242 title
= data
['video']['title']
244 # generate video_urls_dict
245 video_urls_dict
= self
.construct_video_urls(data
)
249 'id': '%s_part%d' % (video_id
, i
+ 1),
252 # some formats are not available for all parts, we have to detect
254 } for i
in range(max(len(v
.get('segs')) for v
in data
['stream']))]
255 for stream
in data
['stream']:
256 fm
= stream
.get('stream_type')
257 video_urls
= video_urls_dict
[fm
]
258 for video_url
, seg
, entry
in zip(video_urls
, stream
['segs'], entries
):
259 entry
['formats'].append({
261 'format_id': self
.get_format_name(fm
),
262 'ext': self
.parse_ext_l(fm
),
263 'filesize': int(seg
['size']),
267 '_type': 'multi_video',