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