]> git.bitcoin.ninja Git - youtube-dl/blob - udn.py
c08428acfab446dff6157035ef032ae326199ebf
[youtube-dl] / 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     _VALID_URL = r'https?://video\.udn\.com/(?:embed|play)/news/(?P<id>\d+)'
15     _TESTS = [{
16         'url': 'http://video.udn.com/embed/news/300040',
17         'md5': 'de06b4c90b042c128395a88f0384817e',
18         'info_dict': {
19             'id': '300040',
20             'ext': 'mp4',
21             'title': '生物老師男變女 全校挺"做自己"',
22             'thumbnail': 're:^https?://.*\.jpg$',
23         }
24     }, {
25         'url': 'https://video.udn.com/embed/news/300040',
26         'only_matching': True,
27     }, {
28         # From https://video.udn.com/news/303776
29         'url': 'https://video.udn.com/play/news/303776',
30         'only_matching': True,
31     }]
32
33     def _real_extract(self, url):
34         video_id = self._match_id(url)
35
36         page = self._download_webpage(url, video_id)
37
38         options = json.loads(js_to_json(self._html_search_regex(
39             r'var options\s*=\s*([^;]+);', page, 'video urls dictionary')))
40
41         video_urls = options['video']
42
43         if video_urls.get('youtube'):
44             return self.url_result(video_urls.get('youtube'), 'Youtube')
45
46         try:
47             del video_urls['youtube']
48         except KeyError:
49             pass
50
51         formats = [{
52             'url': self._download_webpage(
53                 compat_urlparse.urljoin(url, api_url), video_id,
54                 'retrieve url for %s video' % video_type),
55             'format_id': video_type,
56             'preference': 0 if video_type == 'mp4' else -1,
57         } for video_type, api_url in video_urls.items() if api_url]
58
59         if not formats:
60             raise ExtractorError('No videos found', expected=True)
61
62         self._sort_formats(formats)
63
64         thumbnail = None
65
66         if options.get('gallery') and len(options['gallery']):
67             thumbnail = options['gallery'][0].get('original')
68
69         return {
70             'id': video_id,
71             'formats': formats,
72             'title': options['title'],
73             'thumbnail': thumbnail
74         }