1
0
mirror of https://github.com/ytdl-org/youtube-dl.git synced 2024-12-15 09:11:22 +01:00
youtube-dl/youtube_dl/extractor/udn.py

77 lines
2.2 KiB
Python
Raw Normal View History

2015-04-08 11:26:51 +02:00
# coding: utf-8
from __future__ import unicode_literals
import json
from .common import InfoExtractor
from ..utils import (
js_to_json,
ExtractorError,
)
2015-04-08 15:39:34 +02:00
from ..compat import compat_urlparse
2015-04-08 11:26:51 +02:00
class UDNEmbedIE(InfoExtractor):
2015-07-10 18:52:19 +02:00
IE_DESC = '聯合影音'
_PROTOCOL_RELATIVE_VALID_URL = r'//video\.udn\.com/(?:embed|play)/news/(?P<id>\d+)'
_VALID_URL = r'https?:' + _PROTOCOL_RELATIVE_VALID_URL
2015-04-08 11:26:51 +02:00
_TESTS = [{
'url': 'http://video.udn.com/embed/news/300040',
'md5': 'de06b4c90b042c128395a88f0384817e',
'info_dict': {
'id': '300040',
'ext': 'mp4',
'title': '生物老師男變女 全校挺"做自己"',
'thumbnail': 're:^https?://.*\.jpg$',
}
}, {
'url': 'https://video.udn.com/embed/news/300040',
'only_matching': True,
}, {
# From https://video.udn.com/news/303776
'url': 'https://video.udn.com/play/news/303776',
2015-04-08 11:26:51 +02:00
'only_matching': True,
}]
def _real_extract(self, url):
video_id = self._match_id(url)
page = self._download_webpage(url, video_id)
options = json.loads(js_to_json(self._html_search_regex(
r'var options\s*=\s*([^;]+);', page, 'video urls dictionary')))
video_urls = options['video']
if video_urls.get('youtube'):
return self.url_result(video_urls.get('youtube'), 'Youtube')
try:
del video_urls['youtube']
except KeyError:
pass
formats = [{
'url': self._download_webpage(
2015-04-08 15:39:34 +02:00
compat_urlparse.urljoin(url, api_url), video_id,
2015-04-08 11:26:51 +02:00
'retrieve url for %s video' % video_type),
'format_id': video_type,
'preference': 0 if video_type == 'mp4' else -1,
} for video_type, api_url in video_urls.items() if api_url]
if not formats:
raise ExtractorError('No videos found', expected=True)
2015-04-08 11:26:51 +02:00
self._sort_formats(formats)
thumbnail = None
if options.get('gallery') and len(options['gallery']):
thumbnail = options['gallery'][0].get('original')
return {
'id': video_id,
'formats': formats,
'title': options['title'],
'thumbnail': thumbnail
}