]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/udn.py
ee35b7227372c0ddc128dfc694577578f9fc6009
[youtubedl] / youtube_dl / extractor / udn.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 from .common import InfoExtractor
6 from ..utils import (
7 js_to_json,
8 ExtractorError,
9 )
10 from ..compat import compat_urlparse
11
12
13 class UDNEmbedIE(InfoExtractor):
14 IE_DESC = '聯合影音'
15 _PROTOCOL_RELATIVE_VALID_URL = r'//video\.udn\.com/(?:embed|play)/news/(?P<id>\d+)'
16 _VALID_URL = r'https?:' + _PROTOCOL_RELATIVE_VALID_URL
17 _TESTS = [{
18 'url': 'http://video.udn.com/embed/news/300040',
19 'md5': 'de06b4c90b042c128395a88f0384817e',
20 'info_dict': {
21 'id': '300040',
22 'ext': 'mp4',
23 'title': '生物老師男變女 全校挺"做自己"',
24 'thumbnail': 're:^https?://.*\.jpg$',
25 }
26 }, {
27 'url': 'https://video.udn.com/embed/news/300040',
28 'only_matching': True,
29 }, {
30 # From https://video.udn.com/news/303776
31 'url': 'https://video.udn.com/play/news/303776',
32 'only_matching': True,
33 }]
34
35 def _real_extract(self, url):
36 video_id = self._match_id(url)
37
38 page = self._download_webpage(url, video_id)
39
40 options = json.loads(js_to_json(self._html_search_regex(
41 r'var options\s*=\s*([^;]+);', page, 'video urls dictionary')))
42
43 video_urls = options['video']
44
45 if video_urls.get('youtube'):
46 return self.url_result(video_urls.get('youtube'), 'Youtube')
47
48 try:
49 del video_urls['youtube']
50 except KeyError:
51 pass
52
53 formats = [{
54 'url': self._download_webpage(
55 compat_urlparse.urljoin(url, api_url), video_id,
56 'retrieve url for %s video' % video_type),
57 'format_id': video_type,
58 'preference': 0 if video_type == 'mp4' else -1,
59 } for video_type, api_url in video_urls.items() if api_url]
60
61 if not formats:
62 raise ExtractorError('No videos found', expected=True)
63
64 self._sort_formats(formats)
65
66 thumbnail = None
67
68 if options.get('gallery') and len(options['gallery']):
69 thumbnail = options['gallery'][0].get('original')
70
71 return {
72 'id': video_id,
73 'formats': formats,
74 'title': options['title'],
75 'thumbnail': thumbnail
76 }