[shahid] add default fallbacks for extracting api vars
[youtube-dl] / youtube_dl / extractor / nowtv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_str
8 from ..utils import (
9     ExtractorError,
10     int_or_none,
11     parse_iso8601,
12     parse_duration,
13     remove_start,
14 )
15
16
17 class NowTVIE(InfoExtractor):
18     _VALID_URL = r'https?://(?:www\.)?nowtv\.de/(?P<station>rtl|rtl2|rtlnitro|superrtl|ntv|vox)/(?P<id>.+?)/player'
19
20     _TESTS = [{
21         # rtl
22         'url': 'http://www.nowtv.de/rtl/bauer-sucht-frau/die-neuen-bauern-und-eine-hochzeit/player',
23         'info_dict': {
24             'id': '203519',
25             'display_id': 'bauer-sucht-frau/die-neuen-bauern-und-eine-hochzeit',
26             'ext': 'mp4',
27             'title': 'Die neuen Bauern und eine Hochzeit',
28             'description': 'md5:e234e1ed6d63cf06be5c070442612e7e',
29             'thumbnail': 're:^https?://.*\.jpg$',
30             'timestamp': 1432580700,
31             'upload_date': '20150525',
32             'duration': 2786,
33         },
34         'params': {
35             # m3u8 download
36             'skip_download': True,
37         },
38     }, {
39         # rtl2
40         'url': 'http://www.nowtv.de/rtl2/berlin-tag-nacht/berlin-tag-nacht-folge-934/player',
41         'info_dict': {
42             'id': '203481',
43             'display_id': 'berlin-tag-nacht/berlin-tag-nacht-folge-934',
44             'ext': 'mp4',
45             'title': 'Berlin - Tag & Nacht (Folge 934)',
46             'description': 'md5:c85e88c2e36c552dfe63433bc9506dd0',
47             'thumbnail': 're:^https?://.*\.jpg$',
48             'timestamp': 1432666800,
49             'upload_date': '20150526',
50             'duration': 2641,
51         },
52         'params': {
53             # m3u8 download
54             'skip_download': True,
55         },
56     }, {
57         # rtlnitro
58         'url': 'http://www.nowtv.de/rtlnitro/alarm-fuer-cobra-11-die-autobahnpolizei/hals-und-beinbruch-2014-08-23-21-10-00/player',
59         'info_dict': {
60             'id': '165780',
61             'display_id': 'alarm-fuer-cobra-11-die-autobahnpolizei/hals-und-beinbruch-2014-08-23-21-10-00',
62             'ext': 'mp4',
63             'title': 'Hals- und Beinbruch',
64             'description': 'md5:b50d248efffe244e6f56737f0911ca57',
65             'thumbnail': 're:^https?://.*\.jpg$',
66             'timestamp': 1432415400,
67             'upload_date': '20150523',
68             'duration': 2742,
69         },
70         'params': {
71             # m3u8 download
72             'skip_download': True,
73         },
74     }, {
75         # superrtl
76         'url': 'http://www.nowtv.de/superrtl/medicopter-117/angst/player',
77         'info_dict': {
78             'id': '99205',
79             'display_id': 'medicopter-117/angst',
80             'ext': 'mp4',
81             'title': 'Angst!',
82             'description': 'md5:30cbc4c0b73ec98bcd73c9f2a8c17c4e',
83             'thumbnail': 're:^https?://.*\.jpg$',
84             'timestamp': 1222632900,
85             'upload_date': '20080928',
86             'duration': 3025,
87         },
88         'params': {
89             # m3u8 download
90             'skip_download': True,
91         },
92     }, {
93         # ntv
94         'url': 'http://www.nowtv.de/ntv/ratgeber-geld/thema-ua-der-erste-blick-die-apple-watch/player',
95         'info_dict': {
96             'id': '203521',
97             'display_id': 'ratgeber-geld/thema-ua-der-erste-blick-die-apple-watch',
98             'ext': 'mp4',
99             'title': 'Thema u.a.: Der erste Blick: Die Apple Watch',
100             'description': 'md5:4312b6c9d839ffe7d8caf03865a531af',
101             'thumbnail': 're:^https?://.*\.jpg$',
102             'timestamp': 1432751700,
103             'upload_date': '20150527',
104             'duration': 1083,
105         },
106         'params': {
107             # m3u8 download
108             'skip_download': True,
109         },
110     }, {
111         # vox
112         'url': 'http://www.nowtv.de/vox/der-hundeprofi/buero-fall-chihuahua-joel/player',
113         'info_dict': {
114             'id': '128953',
115             'display_id': 'der-hundeprofi/buero-fall-chihuahua-joel',
116             'ext': 'mp4',
117             'title': "Büro-Fall / Chihuahua 'Joel'",
118             'description': 'md5:e62cb6bf7c3cc669179d4f1eb279ad8d',
119             'thumbnail': 're:^https?://.*\.jpg$',
120             'timestamp': 1432408200,
121             'upload_date': '20150523',
122             'duration': 3092,
123         },
124         'params': {
125             # m3u8 download
126             'skip_download': True,
127         },
128     }]
129
130     def _real_extract(self, url):
131         mobj = re.match(self._VALID_URL, url)
132         display_id = mobj.group('id')
133         station = mobj.group('station')
134
135         info = self._download_json(
136             'https://api.nowtv.de/v3/movies/%s?fields=id,title,free,geoblocked,articleLong,articleShort,broadcastStartDate,seoUrl,duration,format,files' % display_id,
137             display_id)
138
139         video_id = compat_str(info['id'])
140
141         files = info['files']
142         if not files:
143             if info.get('geoblocked', False):
144                 raise ExtractorError(
145                     'Video %s is not available from your location due to geo restriction' % video_id,
146                     expected=True)
147             if not info.get('free', True):
148                 raise ExtractorError(
149                     'Video %s is not available for free' % video_id, expected=True)
150
151         f = info.get('format', {})
152         station = f.get('station') or station
153
154         STATIONS = {
155             'rtl': 'rtlnow',
156             'rtl2': 'rtl2now',
157             'vox': 'voxnow',
158             'nitro': 'rtlnitronow',
159             'ntv': 'n-tvnow',
160             'superrtl': 'superrtlnow'
161         }
162
163         formats = []
164         for item in files['items']:
165             item_path = remove_start(item['path'], '/')
166             tbr = int_or_none(item['bitrate'])
167             m3u8_url = 'http://hls.fra.%s.de/hls-vod-enc/%s.m3u8' % (STATIONS[station], item_path)
168             m3u8_url = m3u8_url.replace('now/', 'now/videos/')
169             formats.append({
170                 'url': m3u8_url,
171                 'format_id': '%s-%sk' % (item['id'], tbr),
172                 'ext': 'mp4',
173                 'tbr': tbr,
174             })
175         self._sort_formats(formats)
176
177         title = info['title']
178         description = info.get('articleLong') or info.get('articleShort')
179         timestamp = parse_iso8601(info.get('broadcastStartDate'), ' ')
180         duration = parse_duration(info.get('duration'))
181         thumbnail = f.get('defaultImage169Format') or f.get('defaultImage169Logo')
182
183         return {
184             'id': video_id,
185             'display_id': display_id,
186             'title': title,
187             'description': description,
188             'thumbnail': thumbnail,
189             'timestamp': timestamp,
190             'duration': duration,
191             'formats': formats,
192         }