[vgtv] Detect geo restricted videos (#10348)
[youtube-dl] / youtube_dl / extractor / vgtv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from .xstream import XstreamIE
8 from ..utils import (
9     ExtractorError,
10     float_or_none,
11     try_get,
12 )
13
14
15 class VGTVIE(XstreamIE):
16     IE_DESC = 'VGTV, BTTV, FTV, Aftenposten and Aftonbladet'
17
18     _HOST_TO_APPNAME = {
19         'vgtv.no': 'vgtv',
20         'bt.no/tv': 'bttv',
21         'aftenbladet.no/tv': 'satv',
22         'fvn.no/fvntv': 'fvntv',
23         'aftenposten.no/webtv': 'aptv',
24         'ap.vgtv.no/webtv': 'aptv',
25     }
26
27     _APP_NAME_TO_VENDOR = {
28         'vgtv': 'vgtv',
29         'bttv': 'bt',
30         'satv': 'sa',
31         'fvntv': 'fvn',
32         'aptv': 'ap',
33     }
34
35     _VALID_URL = r'''(?x)
36                     (?:https?://(?:www\.)?
37                     (?P<host>
38                         %s
39                     )
40                     /?
41                     (?:
42                         \#!/(?:video|live)/|
43                         embed?.*id=
44                     )|
45                     (?P<appname>
46                         %s
47                     ):)
48                     (?P<id>\d+)
49                     ''' % ('|'.join(_HOST_TO_APPNAME.keys()), '|'.join(_APP_NAME_TO_VENDOR.keys()))
50
51     _TESTS = [
52         {
53             # streamType: vod
54             'url': 'http://www.vgtv.no/#!/video/84196/hevnen-er-soet-episode-10-abu',
55             'md5': 'b8be7a234cebb840c0d512c78013e02f',
56             'info_dict': {
57                 'id': '84196',
58                 'ext': 'mp4',
59                 'title': 'Hevnen er søt: Episode 10 - Abu',
60                 'description': 'md5:e25e4badb5f544b04341e14abdc72234',
61                 'thumbnail': 're:^https?://.*\.jpg',
62                 'duration': 648.000,
63                 'timestamp': 1404626400,
64                 'upload_date': '20140706',
65                 'view_count': int,
66             },
67         },
68         {
69             # streamType: wasLive
70             'url': 'http://www.vgtv.no/#!/live/100764/opptak-vgtv-foelger-em-kvalifiseringen',
71             'info_dict': {
72                 'id': '100764',
73                 'ext': 'flv',
74                 'title': 'OPPTAK: VGTV følger EM-kvalifiseringen',
75                 'description': 'md5:3772d9c0dc2dff92a886b60039a7d4d3',
76                 'thumbnail': 're:^https?://.*\.jpg',
77                 'duration': 9103.0,
78                 'timestamp': 1410113864,
79                 'upload_date': '20140907',
80                 'view_count': int,
81             },
82             'params': {
83                 # m3u8 download
84                 'skip_download': True,
85             },
86             'skip': 'Video is no longer available',
87         },
88         {
89             # streamType: wasLive
90             'url': 'http://www.vgtv.no/#!/live/113063/direkte-v75-fra-solvalla',
91             'info_dict': {
92                 'id': '113063',
93                 'ext': 'mp4',
94                 'title': 'V75 fra Solvalla 30.05.15',
95                 'description': 'md5:b3743425765355855f88e096acc93231',
96                 'thumbnail': 're:^https?://.*\.jpg',
97                 'duration': 25966,
98                 'timestamp': 1432975582,
99                 'upload_date': '20150530',
100                 'view_count': int,
101             },
102             'params': {
103                 # m3u8 download
104                 'skip_download': True,
105             },
106         },
107         {
108             'url': 'http://www.aftenposten.no/webtv/#!/video/21039/trailer-sweatshop-i-can-t-take-any-more',
109             'md5': 'fd828cd29774a729bf4d4425fe192972',
110             'info_dict': {
111                 'id': '21039',
112                 'ext': 'mp4',
113                 'title': 'TRAILER: «SWEATSHOP» - I can´t take any more',
114                 'description': 'md5:21891f2b0dd7ec2f78d84a50e54f8238',
115                 'duration': 66,
116                 'timestamp': 1417002452,
117                 'upload_date': '20141126',
118                 'view_count': int,
119             },
120             'params': {
121                 # m3u8 download
122                 'skip_download': True,
123             },
124         },
125         {
126             'url': 'http://www.bt.no/tv/#!/video/100250/norling-dette-er-forskjellen-paa-1-divisjon-og-eliteserien',
127             'only_matching': True,
128         },
129         {
130             'url': 'http://ap.vgtv.no/webtv#!/video/111084/de-nye-bysyklene-lettere-bedre-gir-stoerre-hjul-og-feste-til-mobil',
131             'only_matching': True,
132         },
133         {
134             # geoblocked
135             'url': 'http://www.vgtv.no/#!/video/127205/inside-the-mind-of-favela-funk',
136             'only_matching': True,
137         },
138     ]
139
140     def _real_extract(self, url):
141         mobj = re.match(self._VALID_URL, url)
142         video_id = mobj.group('id')
143         host = mobj.group('host')
144         appname = self._HOST_TO_APPNAME[host] if host else mobj.group('appname')
145         vendor = self._APP_NAME_TO_VENDOR[appname]
146
147         data = self._download_json(
148             'http://svp.vg.no/svp/api/v1/%s/assets/%s?appName=%s-website'
149             % (vendor, video_id, appname),
150             video_id, 'Downloading media JSON')
151
152         if data.get('status') == 'inactive':
153             raise ExtractorError(
154                 'Video %s is no longer available' % video_id, expected=True)
155
156         info = {
157             'formats': [],
158         }
159         if len(video_id) == 5:
160             if appname == 'bttv':
161                 info = self._extract_video_info('btno', video_id)
162
163         streams = data['streamUrls']
164         stream_type = data.get('streamType')
165
166         formats = []
167
168         hls_url = streams.get('hls')
169         if hls_url:
170             formats.extend(self._extract_m3u8_formats(
171                 hls_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
172
173         hds_url = streams.get('hds')
174         if hds_url:
175             hdcore_sign = 'hdcore=3.7.0'
176             f4m_formats = self._extract_f4m_formats(
177                 hds_url + '?%s' % hdcore_sign, video_id, f4m_id='hds', fatal=False)
178             if f4m_formats:
179                 for entry in f4m_formats:
180                     # URLs without the extra param induce an 404 error
181                     entry.update({'extra_param_to_segment_url': hdcore_sign})
182                     formats.append(entry)
183
184         mp4_urls = streams.get('pseudostreaming') or []
185         mp4_url = streams.get('mp4')
186         if mp4_url:
187             mp4_urls.append(mp4_url)
188         for mp4_url in mp4_urls:
189             format_info = {
190                 'url': mp4_url,
191             }
192             mobj = re.search('(\d+)_(\d+)_(\d+)', mp4_url)
193             if mobj:
194                 tbr = int(mobj.group(3))
195                 format_info.update({
196                     'width': int(mobj.group(1)),
197                     'height': int(mobj.group(2)),
198                     'tbr': tbr,
199                     'format_id': 'mp4-%s' % tbr,
200                 })
201             formats.append(format_info)
202
203         info['formats'].extend(formats)
204
205         if not info['formats']:
206             properties = try_get(
207                 data, lambda x: x['streamConfiguration']['properties'], list)
208             if properties and 'geoblocked' in properties:
209                 raise self.raise_geo_restricted()
210
211         self._sort_formats(info['formats'])
212
213         info.update({
214             'id': video_id,
215             'title': self._live_title(data['title']) if stream_type == 'live' else data['title'],
216             'description': data['description'],
217             'thumbnail': data['images']['main'] + '?t[]=900x506q80',
218             'timestamp': data['published'],
219             'duration': float_or_none(data['duration'], 1000),
220             'view_count': data['displays'],
221             'is_live': True if stream_type == 'live' else False,
222         })
223         return info
224
225
226 class BTArticleIE(InfoExtractor):
227     IE_NAME = 'bt:article'
228     IE_DESC = 'Bergens Tidende Articles'
229     _VALID_URL = r'https?://(?:www\.)?bt\.no/(?:[^/]+/)+(?P<id>[^/]+)-\d+\.html'
230     _TEST = {
231         'url': 'http://www.bt.no/nyheter/lokalt/Kjemper-for-internatet-1788214.html',
232         'md5': '2acbe8ad129b3469d5ae51b1158878df',
233         'info_dict': {
234             'id': '23199',
235             'ext': 'mp4',
236             'title': 'Alrekstad internat',
237             'description': 'md5:dc81a9056c874fedb62fc48a300dac58',
238             'thumbnail': 're:^https?://.*\.jpg',
239             'duration': 191,
240             'timestamp': 1289991323,
241             'upload_date': '20101117',
242             'view_count': int,
243         },
244     }
245
246     def _real_extract(self, url):
247         webpage = self._download_webpage(url, self._match_id(url))
248         video_id = self._search_regex(
249             r'<video[^>]+data-id="(\d+)"', webpage, 'video id')
250         return self.url_result('bttv:%s' % video_id, 'VGTV')
251
252
253 class BTVestlendingenIE(InfoExtractor):
254     IE_NAME = 'bt:vestlendingen'
255     IE_DESC = 'Bergens Tidende - Vestlendingen'
256     _VALID_URL = r'https?://(?:www\.)?bt\.no/spesial/vestlendingen/#!/(?P<id>\d+)'
257     _TESTS = [{
258         'url': 'http://www.bt.no/spesial/vestlendingen/#!/86588',
259         'md5': 'd7d17e3337dc80de6d3a540aefbe441b',
260         'info_dict': {
261             'id': '86588',
262             'ext': 'mov',
263             'title': 'Otto Wollertsen',
264             'description': 'Vestlendingen Otto Fredrik Wollertsen',
265             'timestamp': 1430473209,
266             'upload_date': '20150501',
267         },
268         'skip': '404 Error',
269     }, {
270         'url': 'http://www.bt.no/spesial/vestlendingen/#!/86255',
271         'md5': 'a2893f8632e96389f4bdf36aa9463ceb',
272         'info_dict': {
273             'id': '86255',
274             'ext': 'mov',
275             'title': 'Du må tåle å fryse og være sulten',
276             'description': 'md5:b8046f4d022d5830ddab04865791d063',
277             'upload_date': '20150321',
278             'timestamp': 1426942023,
279         },
280     }]
281
282     def _real_extract(self, url):
283         return self.url_result('bttv:%s' % self._match_id(url), 'VGTV')