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