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