[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / swrmediathek.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     parse_duration,
7     int_or_none,
8     determine_protocol,
9 )
10
11
12 class SWRMediathekIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:www\.)?swrmediathek\.de/(?:content/)?player\.htm\?show=(?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})'
14
15     _TESTS = [{
16         'url': 'http://swrmediathek.de/player.htm?show=849790d0-dab8-11e3-a953-0026b975f2e6',
17         'md5': '8c5f6f0172753368547ca8413a7768ac',
18         'info_dict': {
19             'id': '849790d0-dab8-11e3-a953-0026b975f2e6',
20             'ext': 'mp4',
21             'title': 'SWR odysso',
22             'description': 'md5:2012e31baad36162e97ce9eb3f157b8a',
23             'thumbnail': r're:^http:.*\.jpg$',
24             'duration': 2602,
25             'upload_date': '20140515',
26             'uploader': 'SWR Fernsehen',
27             'uploader_id': '990030',
28         },
29     }, {
30         'url': 'http://swrmediathek.de/player.htm?show=0e1a8510-ddf2-11e3-9be3-0026b975f2e6',
31         'md5': 'b10ab854f912eecc5a6b55cd6fc1f545',
32         'info_dict': {
33             'id': '0e1a8510-ddf2-11e3-9be3-0026b975f2e6',
34             'ext': 'mp4',
35             'title': 'Nachtcafé - Alltagsdroge Alkohol - zwischen Sektempfang und Komasaufen',
36             'description': 'md5:e0a3adc17e47db2c23aab9ebc36dbee2',
37             'thumbnail': r're:http://.*\.jpg',
38             'duration': 5305,
39             'upload_date': '20140516',
40             'uploader': 'SWR Fernsehen',
41             'uploader_id': '990030',
42         },
43         'skip': 'redirect to http://swrmediathek.de/index.htm?hinweis=swrlink',
44     }, {
45         'url': 'http://swrmediathek.de/player.htm?show=bba23e10-cb93-11e3-bf7f-0026b975f2e6',
46         'md5': '4382e4ef2c9d7ce6852535fa867a0dd3',
47         'info_dict': {
48             'id': 'bba23e10-cb93-11e3-bf7f-0026b975f2e6',
49             'ext': 'mp3',
50             'title': 'Saša Stanišic: Vor dem Fest',
51             'description': 'md5:5b792387dc3fbb171eb709060654e8c9',
52             'thumbnail': r're:http://.*\.jpg',
53             'duration': 3366,
54             'upload_date': '20140520',
55             'uploader': 'SWR 2',
56             'uploader_id': '284670',
57         },
58         'skip': 'redirect to http://swrmediathek.de/index.htm?hinweis=swrlink',
59     }]
60
61     def _real_extract(self, url):
62         video_id = self._match_id(url)
63
64         video = self._download_json(
65             'http://swrmediathek.de/AjaxEntry?ekey=%s' % video_id,
66             video_id, 'Downloading video JSON')
67
68         attr = video['attr']
69         title = attr['entry_title']
70         media_type = attr.get('entry_etype')
71
72         formats = []
73         for entry in video.get('sub', []):
74             if entry.get('name') != 'entry_media':
75                 continue
76
77             entry_attr = entry.get('attr', {})
78             f_url = entry_attr.get('val2')
79             if not f_url:
80                 continue
81             codec = entry_attr.get('val0')
82             if codec == 'm3u8':
83                 formats.extend(self._extract_m3u8_formats(
84                     f_url, video_id, 'mp4', 'm3u8_native',
85                     m3u8_id='hls', fatal=False))
86             elif codec == 'f4m':
87                 formats.extend(self._extract_f4m_formats(
88                     f_url + '?hdcore=3.7.0', video_id,
89                     f4m_id='hds', fatal=False))
90             else:
91                 formats.append({
92                     'format_id': determine_protocol({'url': f_url}),
93                     'url': f_url,
94                     'quality': int_or_none(entry_attr.get('val1')),
95                     'vcodec': codec if media_type == 'Video' else 'none',
96                     'acodec': codec if media_type == 'Audio' else None,
97                 })
98         self._sort_formats(formats)
99
100         upload_date = None
101         entry_pdatet = attr.get('entry_pdatet')
102         if entry_pdatet:
103             upload_date = entry_pdatet[:-4]
104
105         return {
106             'id': video_id,
107             'title': title,
108             'description': attr.get('entry_descl'),
109             'thumbnail': attr.get('entry_image_16_9'),
110             'duration': parse_duration(attr.get('entry_durat')),
111             'upload_date': upload_date,
112             'uploader': attr.get('channel_title'),
113             'uploader_id': attr.get('channel_idkey'),
114             'formats': formats,
115         }