2012-08-31 19:20:38 +04:00
|
|
|
|
#!/usr/bin/env python
|
2012-08-20 19:54:03 +04:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
2012-08-31 19:20:38 +04:00
|
|
|
|
from ..common import *
|
2014-07-21 04:39:40 +04:00
|
|
|
|
from ..extractor import VideoExtractor
|
2012-08-20 19:54:03 +04:00
|
|
|
|
|
2014-08-07 23:21:35 +04:00
|
|
|
|
import base64
|
|
|
|
|
import time
|
|
|
|
|
|
2014-06-24 05:59:47 +04:00
|
|
|
|
class Youku(VideoExtractor):
|
|
|
|
|
name = "优酷 (Youku)"
|
|
|
|
|
|
|
|
|
|
stream_types = [
|
2014-08-07 23:31:11 +04:00
|
|
|
|
{'id': 'hd3', 'container': 'flv', 'video_profile': '1080P'},
|
2014-06-24 05:59:47 +04:00
|
|
|
|
{'id': 'hd2', 'container': 'flv', 'video_profile': '超清'},
|
|
|
|
|
{'id': 'mp4', 'container': 'mp4', 'video_profile': '高清'},
|
2014-06-28 15:22:50 +04:00
|
|
|
|
{'id': 'flvhd', 'container': 'flv', 'video_profile': '高清'},
|
2014-06-24 05:59:47 +04:00
|
|
|
|
{'id': 'flv', 'container': 'flv', 'video_profile': '标清'},
|
|
|
|
|
{'id': '3gphd', 'container': '3gp', 'video_profile': '高清(3GP)'},
|
|
|
|
|
]
|
|
|
|
|
|
2014-08-07 23:21:35 +04:00
|
|
|
|
def generate_ep(vid, ep):
|
|
|
|
|
f_code_1 = 'becaf9be'
|
|
|
|
|
f_code_2 = 'bf7e5f01'
|
|
|
|
|
|
|
|
|
|
def trans_e(a, c):
|
|
|
|
|
f = h = 0
|
|
|
|
|
b = list(range(256))
|
|
|
|
|
result = ''
|
|
|
|
|
while h < 256:
|
|
|
|
|
f = (f + b[h] + ord(a[h % len(a)])) % 256
|
|
|
|
|
b[h], b[f] = b[f], b[h]
|
|
|
|
|
h += 1
|
|
|
|
|
q = f = h = 0
|
|
|
|
|
while q < len(c):
|
|
|
|
|
h = (h + 1) % 256
|
|
|
|
|
f = (f + b[h]) % 256
|
|
|
|
|
b[h], b[f] = b[f], b[h]
|
|
|
|
|
if isinstance(c[q], int):
|
|
|
|
|
result += chr(c[q] ^ b[(b[h] + b[f]) % 256])
|
|
|
|
|
else:
|
|
|
|
|
result += chr(ord(c[q]) ^ b[(b[h] + b[f]) % 256])
|
|
|
|
|
q += 1
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
2014-09-07 04:34:09 +04:00
|
|
|
|
e_code = trans_e(f_code_1, base64.b64decode(bytes(ep, 'ascii')))
|
2014-08-07 23:21:35 +04:00
|
|
|
|
sid, token = e_code.split('_')
|
|
|
|
|
new_ep = trans_e(f_code_2, '%s_%s_%s' % (sid, vid, token))
|
|
|
|
|
return base64.b64encode(bytes(new_ep, 'latin')), sid, token
|
|
|
|
|
|
2014-07-17 10:46:11 +04:00
|
|
|
|
def parse_m3u8(m3u8):
|
|
|
|
|
return re.findall(r'(http://[^?]+)\?ts_start=0', m3u8)
|
|
|
|
|
|
2014-06-24 05:59:47 +04:00
|
|
|
|
def get_vid_from_url(url):
|
|
|
|
|
"""Extracts video ID from URL.
|
|
|
|
|
"""
|
2014-11-13 23:17:18 +03:00
|
|
|
|
return match1(url, r'youku\.com/v_show/id_([a-zA-Z0-9=]+)') or \
|
|
|
|
|
match1(url, r'player\.youku\.com/player\.php/sid/([a-zA-Z0-9=]+)/v\.swf') or \
|
2015-09-03 06:28:43 +03:00
|
|
|
|
match1(url, r'loader\.swf\?VideoIDS=([a-zA-Z0-9=]+)') or \
|
|
|
|
|
match1(url, r'player\.youku\.com/embed/([a-zA-Z0-9=]+)')
|
2012-08-20 19:54:03 +04:00
|
|
|
|
|
2014-07-17 10:46:11 +04:00
|
|
|
|
def get_playlist_id_from_url(url):
|
|
|
|
|
"""Extracts playlist ID from URL.
|
|
|
|
|
"""
|
2014-11-13 23:17:18 +03:00
|
|
|
|
return match1(url, r'youku\.com/playlist_show/id_([a-zA-Z0-9=]+)')
|
2014-07-17 10:46:11 +04:00
|
|
|
|
|
|
|
|
|
def download_playlist_by_url(self, url, **kwargs):
|
|
|
|
|
self.url = url
|
|
|
|
|
|
2014-09-21 00:57:26 +04:00
|
|
|
|
playlist_id = self.__class__.get_playlist_id_from_url(self.url)
|
2014-07-17 10:46:11 +04:00
|
|
|
|
if playlist_id is None:
|
|
|
|
|
log.wtf('[Failed] Unsupported URL pattern.')
|
|
|
|
|
|
|
|
|
|
video_page = get_content('http://www.youku.com/playlist_show/id_%s' % playlist_id)
|
|
|
|
|
videos = set(re.findall(r'href="(http://v\.youku\.com/[^?"]+)', video_page))
|
|
|
|
|
self.title = re.search(r'<meta name="title" content="([^"]+)"', video_page).group(1)
|
|
|
|
|
self.p_playlist()
|
|
|
|
|
for video in videos:
|
|
|
|
|
index = parse_query_param(video, 'f')
|
2014-09-21 00:57:26 +04:00
|
|
|
|
self.__class__().download_by_url(video, index=index, **kwargs)
|
2012-08-20 19:54:03 +04:00
|
|
|
|
|
2014-06-24 05:59:47 +04:00
|
|
|
|
def prepare(self, **kwargs):
|
|
|
|
|
assert self.url or self.vid
|
2014-07-17 10:46:11 +04:00
|
|
|
|
|
2014-06-24 05:59:47 +04:00
|
|
|
|
if self.url and not self.vid:
|
2014-09-21 00:57:26 +04:00
|
|
|
|
self.vid = self.__class__.get_vid_from_url(self.url)
|
2012-08-20 19:54:03 +04:00
|
|
|
|
|
2014-07-17 10:46:11 +04:00
|
|
|
|
if self.vid is None:
|
|
|
|
|
self.download_playlist_by_url(self.url, **kwargs)
|
|
|
|
|
exit(0)
|
|
|
|
|
|
2014-08-07 23:21:35 +04:00
|
|
|
|
meta = json.loads(get_html('http://v.youku.com/player/getPlayList/VideoIDS/%s/Pf/4/ctype/12/ev/1' % self.vid))
|
2014-06-29 22:03:32 +04:00
|
|
|
|
if not meta['data']:
|
2014-07-16 20:16:03 +04:00
|
|
|
|
log.wtf('[Failed] Video not found.')
|
2014-06-24 05:59:47 +04:00
|
|
|
|
metadata0 = meta['data'][0]
|
2012-08-31 02:19:22 +04:00
|
|
|
|
|
2014-07-30 06:09:47 +04:00
|
|
|
|
if 'error_code' in metadata0 and metadata0['error_code']:
|
|
|
|
|
if metadata0['error_code'] == -8:
|
|
|
|
|
log.w('[Warning] This video can only be streamed within Mainland China!')
|
|
|
|
|
log.w('Use \'-y\' to specify a proxy server for extracting stream data.\n')
|
2014-07-30 06:52:45 +04:00
|
|
|
|
elif metadata0['error_code'] == -6:
|
|
|
|
|
log.w('[Warning] This video is password protected.')
|
|
|
|
|
self.password_protected = True
|
2014-07-30 06:09:47 +04:00
|
|
|
|
|
2014-06-24 05:59:47 +04:00
|
|
|
|
self.title = metadata0['title']
|
2012-08-31 02:19:22 +04:00
|
|
|
|
|
2014-08-07 23:21:35 +04:00
|
|
|
|
self.ep = metadata0['ep']
|
|
|
|
|
self.ip = metadata0['ip']
|
|
|
|
|
|
2014-07-20 17:15:33 +04:00
|
|
|
|
if 'dvd' in metadata0 and 'audiolang' in metadata0['dvd']:
|
|
|
|
|
self.audiolang = metadata0['dvd']['audiolang']
|
|
|
|
|
for i in self.audiolang:
|
|
|
|
|
i['url'] = 'http://v.youku.com/v_show/id_{}'.format(i['vid'])
|
|
|
|
|
|
2014-06-24 05:59:47 +04:00
|
|
|
|
for stream_type in self.stream_types:
|
|
|
|
|
if stream_type['id'] in metadata0['streamsizes']:
|
|
|
|
|
stream_id = stream_type['id']
|
|
|
|
|
stream_size = int(metadata0['streamsizes'][stream_id])
|
|
|
|
|
self.streams[stream_id] = {'container': stream_type['container'], 'video_profile': stream_type['video_profile'], 'size': stream_size}
|
2012-08-20 19:54:03 +04:00
|
|
|
|
|
2014-06-28 15:22:50 +04:00
|
|
|
|
if not self.streams:
|
|
|
|
|
for stream_type in self.stream_types:
|
|
|
|
|
if stream_type['id'] in metadata0['streamtypes_o']:
|
|
|
|
|
stream_id = stream_type['id']
|
|
|
|
|
self.streams[stream_id] = {'container': stream_type['container'], 'video_profile': stream_type['video_profile']}
|
|
|
|
|
|
2014-06-24 05:59:47 +04:00
|
|
|
|
def extract(self, **kwargs):
|
|
|
|
|
if 'stream_id' in kwargs and kwargs['stream_id']:
|
|
|
|
|
# Extract the stream
|
|
|
|
|
stream_id = kwargs['stream_id']
|
2014-06-28 20:10:29 +04:00
|
|
|
|
|
|
|
|
|
if stream_id not in self.streams:
|
2014-07-17 04:24:49 +04:00
|
|
|
|
log.e('[Error] Invalid video format.')
|
2014-07-30 05:48:26 +04:00
|
|
|
|
log.e('Run \'-i\' command with no specific video format to view all available formats.')
|
2014-06-28 20:10:29 +04:00
|
|
|
|
exit(2)
|
2012-08-20 19:54:03 +04:00
|
|
|
|
else:
|
2014-06-24 05:59:47 +04:00
|
|
|
|
# Extract stream with the best quality
|
|
|
|
|
stream_id = self.streams_sorted[0]['id']
|
2012-08-20 19:54:03 +04:00
|
|
|
|
|
2014-09-21 00:57:26 +04:00
|
|
|
|
new_ep, sid, token = self.__class__.generate_ep(self.vid, self.ep)
|
2014-08-07 23:21:35 +04:00
|
|
|
|
m3u8_query = parse.urlencode(dict(
|
|
|
|
|
ctype=12,
|
|
|
|
|
ep=new_ep,
|
|
|
|
|
ev=1,
|
|
|
|
|
keyframe=1,
|
|
|
|
|
oip=self.ip,
|
|
|
|
|
sid=sid,
|
|
|
|
|
token=token,
|
|
|
|
|
ts=int(time.time()),
|
|
|
|
|
type=stream_id,
|
|
|
|
|
vid=self.vid,
|
|
|
|
|
))
|
|
|
|
|
m3u8_url = 'http://pl.youku.com/playlist/m3u8?' + m3u8_query
|
2014-07-30 06:52:45 +04:00
|
|
|
|
|
|
|
|
|
if not kwargs['info_only']:
|
|
|
|
|
if self.password_protected:
|
|
|
|
|
password = input(log.sprint('Password: ', log.YELLOW))
|
2014-08-10 02:50:26 +04:00
|
|
|
|
m3u8_url += '&password={}'.format(password)
|
2014-07-30 06:52:45 +04:00
|
|
|
|
|
|
|
|
|
m3u8 = get_html(m3u8_url)
|
|
|
|
|
|
2014-09-21 00:57:26 +04:00
|
|
|
|
self.streams[stream_id]['src'] = self.__class__.parse_m3u8(m3u8)
|
2014-09-12 21:15:55 +04:00
|
|
|
|
if not self.streams[stream_id]['src'] and self.password_protected:
|
|
|
|
|
log.e('[Failed] Wrong password.')
|
2012-08-20 19:54:03 +04:00
|
|
|
|
|
2014-06-24 05:59:47 +04:00
|
|
|
|
site = Youku()
|
|
|
|
|
download = site.download_by_url
|
2014-07-17 10:46:11 +04:00
|
|
|
|
download_playlist = site.download_playlist_by_url
|
2013-07-16 06:58:06 +04:00
|
|
|
|
|
2014-06-24 05:59:47 +04:00
|
|
|
|
youku_download_by_vid = site.download_by_vid
|
|
|
|
|
# Used by: acfun.py bilibili.py miomio.py tudou.py
|