you-get/src/you_get/extractor/youku.py

87 lines
3.3 KiB
Python
Raw Normal View History

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 *
2012-08-20 19:54:03 +04:00
2014-06-24 05:59:47 +04:00
class Youku(VideoExtractor):
name = "优酷 (Youku)"
stream_types = [
# FIXME: Does not work for 1080P
# {'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'},
]
def get_vid_from_url(url):
"""Extracts video ID from URL.
"""
patterns = [
'youku.com/v_show/id_([\w=]+)',
'player.youku.com/player.php/sid/([\w=]+)/v.swf',
'loader\.swf\?VideoIDS=([\w=]+)',
]
matches = match1(url, *patterns)
if matches:
return matches[0]
else:
return None
2012-08-20 19:54:03 +04:00
2014-06-24 05:59:47 +04:00
def parse_m3u8(m3u8):
return re.findall(r'(http://[^?]+)\?ts_start=0', m3u8)
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
if self.url and not self.vid:
self.vid = __class__.get_vid_from_url(self.url)
2012-08-20 19:54:03 +04:00
2014-06-24 05:59:47 +04:00
meta = json.loads(get_html('http://v.youku.com/player/getPlayList/VideoIDS/%s' % 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]
2014-06-24 05:59:47 +04:00
self.title = metadata0['title']
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:
log.e('[Failed] Invalid video format.')
log.e('Use without specifying any video format to check all available formats.')
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-06-24 05:59:47 +04:00
m3u8_url = "http://v.youku.com/player/getM3U8/vid/{vid}/type/{stream_id}/video.m3u8".format(vid=self.vid, stream_id=stream_id)
m3u8 = get_html(m3u8_url)
if not m3u8:
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')
2012-08-20 19:54:03 +04:00
2014-06-24 05:59:47 +04:00
self.streams[stream_id]['src'] = __class__.parse_m3u8(m3u8)
2012-08-20 19:54:03 +04:00
2014-06-24 05:59:47 +04:00
site = Youku()
download = site.download_by_url
download_playlist = playlist_not_supported('youku')
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