[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / noz.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import (
6     compat_urllib_parse_unquote,
7     compat_xpath,
8 )
9 from ..utils import (
10     int_or_none,
11     find_xpath_attr,
12     xpath_text,
13     update_url_query,
14 )
15
16
17 class NozIE(InfoExtractor):
18     _VALID_URL = r'https?://(?:www\.)?noz\.de/video/(?P<id>[0-9]+)/'
19     _TESTS = [{
20         'url': 'http://www.noz.de/video/25151/32-Deutschland-gewinnt-Badminton-Lnderspiel-in-Melle',
21         'info_dict': {
22             'id': '25151',
23             'ext': 'mp4',
24             'duration': 215,
25             'title': '3:2 - Deutschland gewinnt Badminton-Länderspiel in Melle',
26             'description': 'Vor rund 370 Zuschauern gewinnt die deutsche Badminton-Nationalmannschaft am Donnerstag ein EM-Vorbereitungsspiel gegen Frankreich in Melle. Video Moritz Frankenberg.',
27             'thumbnail': r're:^http://.*\.jpg',
28         },
29     }]
30
31     def _real_extract(self, url):
32         video_id = self._match_id(url)
33         webpage = self._download_webpage(url, video_id)
34         description = self._og_search_description(webpage)
35
36         edge_url = self._html_search_regex(
37             r'<script\s+(?:type="text/javascript"\s+)?src="(.*?/videojs_.*?)"',
38             webpage, 'edge URL')
39         edge_content = self._download_webpage(edge_url, 'meta configuration')
40
41         config_url_encoded = self._search_regex(
42             r'so\.addVariable\("config_url","[^,]*,(.*?)"',
43             edge_content, 'config URL'
44         )
45         config_url = compat_urllib_parse_unquote(config_url_encoded)
46
47         doc = self._download_xml(config_url, 'video configuration')
48         title = xpath_text(doc, './/title')
49         thumbnail = xpath_text(doc, './/article/thumbnail/url')
50         duration = int_or_none(xpath_text(
51             doc, './/article/movie/file/duration'))
52         formats = []
53         for qnode in doc.findall(compat_xpath('.//article/movie/file/qualities/qual')):
54             http_url_ele = find_xpath_attr(
55                 qnode, './html_urls/video_url', 'format', 'video/mp4')
56             http_url = http_url_ele.text if http_url_ele is not None else None
57             if http_url:
58                 formats.append({
59                     'url': http_url,
60                     'format_name': xpath_text(qnode, './name'),
61                     'format_id': '%s-%s' % ('http', xpath_text(qnode, './id')),
62                     'height': int_or_none(xpath_text(qnode, './height')),
63                     'width': int_or_none(xpath_text(qnode, './width')),
64                     'tbr': int_or_none(xpath_text(qnode, './bitrate'), scale=1000),
65                 })
66             else:
67                 f4m_url = xpath_text(qnode, 'url_hd2')
68                 if f4m_url:
69                     formats.extend(self._extract_f4m_formats(
70                         update_url_query(f4m_url, {'hdcore': '3.4.0'}),
71                         video_id, f4m_id='hds', fatal=False))
72                 m3u8_url_ele = find_xpath_attr(
73                     qnode, './html_urls/video_url',
74                     'format', 'application/vnd.apple.mpegurl')
75                 m3u8_url = m3u8_url_ele.text if m3u8_url_ele is not None else None
76                 if m3u8_url:
77                     formats.extend(self._extract_m3u8_formats(
78                         m3u8_url, video_id, 'mp4', 'm3u8_native',
79                         m3u8_id='hls', fatal=False))
80         self._sort_formats(formats)
81
82         return {
83             'id': video_id,
84             'formats': formats,
85             'title': title,
86             'duration': duration,
87             'description': description,
88             'thumbnail': thumbnail,
89         }