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