[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / umg.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     int_or_none,
7     parse_filesize,
8     parse_iso8601,
9 )
10
11
12 class UMGDeIE(InfoExtractor):
13     IE_NAME = 'umg:de'
14     IE_DESC = 'Universal Music Deutschland'
15     _VALID_URL = r'https?://(?:www\.)?universal-music\.de/[^/]+/videos/[^/?#]+-(?P<id>\d+)'
16     _TEST = {
17         'url': 'https://www.universal-music.de/sido/videos/jedes-wort-ist-gold-wert-457803',
18         'md5': 'ebd90f48c80dcc82f77251eb1902634f',
19         'info_dict': {
20             'id': '457803',
21             'ext': 'mp4',
22             'title': 'Jedes Wort ist Gold wert',
23             'timestamp': 1513591800,
24             'upload_date': '20171218',
25         }
26     }
27
28     def _real_extract(self, url):
29         video_id = self._match_id(url)
30         video_data = self._download_json(
31             'https://api.universal-music.de/graphql',
32             video_id, query={
33                 'query': '''{
34   universalMusic(channel:16) {
35     video(id:%s) {
36       headline
37       formats {
38         formatId
39         url
40         type
41         width
42         height
43         mimeType
44         fileSize
45       }
46       duration
47       createdDate
48     }
49   }
50 }''' % video_id})['data']['universalMusic']['video']
51
52         title = video_data['headline']
53         hls_url_template = 'http://mediadelivery.universal-music-services.de/vod/mp4:autofill/storage/' + '/'.join(list(video_id)) + '/content/%s/file/playlist.m3u8'
54
55         thumbnails = []
56         formats = []
57
58         def add_m3u8_format(format_id):
59             m3u8_formats = self._extract_m3u8_formats(
60                 hls_url_template % format_id, video_id, 'mp4',
61                 'm3u8_native', m3u8_id='hls', fatal='False')
62             if m3u8_formats and m3u8_formats[0].get('height'):
63                 formats.extend(m3u8_formats)
64
65         for f in video_data.get('formats', []):
66             f_url = f.get('url')
67             mime_type = f.get('mimeType')
68             if not f_url or mime_type == 'application/mxf':
69                 continue
70             fmt = {
71                 'url': f_url,
72                 'width': int_or_none(f.get('width')),
73                 'height': int_or_none(f.get('height')),
74                 'filesize': parse_filesize(f.get('fileSize')),
75             }
76             f_type = f.get('type')
77             if f_type == 'Image':
78                 thumbnails.append(fmt)
79             elif f_type == 'Video':
80                 format_id = f.get('formatId')
81                 if format_id:
82                     fmt['format_id'] = format_id
83                     if mime_type == 'video/mp4':
84                         add_m3u8_format(format_id)
85                 urlh = self._request_webpage(f_url, video_id, fatal=False)
86                 if urlh:
87                     first_byte = urlh.read(1)
88                     if first_byte not in (b'F', b'\x00'):
89                         continue
90                     formats.append(fmt)
91         if not formats:
92             for format_id in (867, 836, 940):
93                 add_m3u8_format(format_id)
94         self._sort_formats(formats, ('width', 'height', 'filesize', 'tbr'))
95
96         return {
97             'id': video_id,
98             'title': title,
99             'duration': int_or_none(video_data.get('duration')),
100             'timestamp': parse_iso8601(video_data.get('createdDate'), ' '),
101             'thumbnails': thumbnails,
102             'formats': formats,
103         }