Merge remote-tracking branch 'drags/yt-feed-loadmore'
[youtube-dl] / youtube_dl / extractor / spiegel.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import compat_urlparse
8
9
10 class SpiegelIE(InfoExtractor):
11     _VALID_URL = r'https?://(?:www\.)?spiegel\.de/video/[^/]*-(?P<videoID>[0-9]+)(?:\.html)?(?:#.*)?$'
12     _TESTS = [{
13         'url': 'http://www.spiegel.de/video/vulkan-tungurahua-in-ecuador-ist-wieder-aktiv-video-1259285.html',
14         'md5': '2c2754212136f35fb4b19767d242f66e',
15         'info_dict': {
16             'id': '1259285',
17             'ext': 'mp4',
18             'title': 'Vulkanausbruch in Ecuador: Der "Feuerschlund" ist wieder aktiv',
19             'description': 'md5:8029d8310232196eb235d27575a8b9f4',
20             'duration': 49,
21         },
22     }, {
23         'url': 'http://www.spiegel.de/video/schach-wm-videoanalyse-des-fuenften-spiels-video-1309159.html',
24         'md5': 'f2cdf638d7aa47654e251e1aee360af1',
25         'info_dict': {
26             'id': '1309159',
27             'ext': 'mp4',
28             'title': 'Schach-WM in der Videoanalyse: Carlsen nutzt die Fehlgriffe des Titelverteidigers',
29             'description': 'md5:c2322b65e58f385a820c10fa03b2d088',
30             'duration': 983,
31         },
32     }, {
33         'url': 'http://www.spiegel.de/video/johann-westhauser-videobotschaft-des-hoehlenforschers-video-1502367.html',
34         'md5': '54f58ba0e752e3c07bc2a26222dd0acf',
35         'info_dict': {
36             'id': '1502367',
37             'ext': 'mp4',
38             'title': 'Videobotschaft: Höhlenforscher Westhauser dankt seinen Rettern',
39             'description': 'md5:c6f1ec11413ebd1088b6813943e5fc91',
40             'duration': 42,
41         },
42     }]
43
44     def _real_extract(self, url):
45         m = re.match(self._VALID_URL, url)
46         video_id = m.group('videoID')
47
48         webpage = self._download_webpage(url, video_id)
49
50         title = self._html_search_regex(
51             r'<div class="module-title">(.*?)</div>', webpage, 'title')
52         description = self._html_search_meta('description', webpage, 'description')
53
54         base_url = self._search_regex(
55             r'var\s+server\s*=\s*"([^"]+)\"', webpage, 'server URL')
56
57         xml_url = base_url + video_id + '.xml'
58         idoc = self._download_xml(xml_url, video_id)
59
60         formats = [
61             {
62                 'format_id': n.tag.rpartition('type')[2],
63                 'url': base_url + n.find('./filename').text,
64                 'width': int(n.find('./width').text),
65                 'height': int(n.find('./height').text),
66                 'abr': int(n.find('./audiobitrate').text),
67                 'vbr': int(n.find('./videobitrate').text),
68                 'vcodec': n.find('./codec').text,
69                 'acodec': 'MP4A',
70             }
71             for n in list(idoc)
72             # Blacklist type 6, it's extremely LQ and not available on the same server
73             if n.tag.startswith('type') and n.tag != 'type6'
74         ]
75         duration = float(idoc[0].findall('./duration')[0].text)
76
77         self._sort_formats(formats)
78
79         return {
80             'id': video_id,
81             'title': title,
82             'description': description,
83             'duration': duration,
84             'formats': formats,
85         }
86
87
88 class SpiegelArticleIE(InfoExtractor):
89     _VALID_URL = 'https?://www\.spiegel\.de/(?!video/)[^?#]*?-(?P<id>[0-9]+)\.html'
90     IE_NAME = 'Spiegel:Article'
91     IE_DESC = 'Articles on spiegel.de'
92     _TEST = {
93         'url': 'http://www.spiegel.de/sport/sonst/badminton-wm-die-randsportart-soll-populaerer-werden-a-987092.html',
94         'info_dict': {
95             'id': '1516455',
96             'ext': 'mp4',
97             'title': 'Faszination Badminton: Nennt es bloß nicht Federball',
98             'description': 're:^Patrick Kämnitz gehört.{100,}',
99         },
100     }
101
102     def _real_extract(self, url):
103         m = re.match(self._VALID_URL, url)
104         video_id = m.group('id')
105
106         webpage = self._download_webpage(url, video_id)
107         video_link = self._search_regex(
108             r'<a href="([^"]+)" onclick="return spOpenVideo\(this,', webpage,
109             'video page URL')
110         video_url = compat_urlparse.urljoin(
111             self.http_scheme() + '//spiegel.de/', video_link)
112
113         return {
114             '_type': 'url',
115             'url': video_url,
116         }