[vgtv] improve _VALID_URL regex
[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 ..utils import (
8     ExtractorError,
9     float_or_none,
10 )
11
12
13 class VGTVIE(InfoExtractor):
14     IE_DESC = 'VGTV, BTTV, FTV, Aftenposten and Aftonbladet'
15     _VALID_URL = r'''(?x)
16                     http://(?:www\.)?
17                     (?P<host>
18                         vgtv.no|
19                         (?:bt|aftenbladet).no/tv|
20                         fvn.no/fvntv|
21                         aftenposten.no/webtv
22                     )
23                     /
24                     (?:
25                         \#!/(?:video|live)/|
26                         embed?.*id=
27                     )
28                     (?P<id>[0-9]+)
29                     '''
30     _TESTS = [
31         {
32             # streamType: vod
33             'url': 'http://www.vgtv.no/#!/video/84196/hevnen-er-soet-episode-10-abu',
34             'md5': 'b8be7a234cebb840c0d512c78013e02f',
35             'info_dict': {
36                 'id': '84196',
37                 'ext': 'mp4',
38                 'title': 'Hevnen er søt: Episode 10 - Abu',
39                 'description': 'md5:e25e4badb5f544b04341e14abdc72234',
40                 'thumbnail': 're:^https?://.*\.jpg',
41                 'duration': 648.000,
42                 'timestamp': 1404626400,
43                 'upload_date': '20140706',
44                 'view_count': int,
45             },
46         },
47         {
48             # streamType: wasLive
49             'url': 'http://www.vgtv.no/#!/live/100764/opptak-vgtv-foelger-em-kvalifiseringen',
50             'info_dict': {
51                 'id': '100764',
52                 'ext': 'flv',
53                 'title': 'OPPTAK: VGTV følger EM-kvalifiseringen',
54                 'description': 'md5:3772d9c0dc2dff92a886b60039a7d4d3',
55                 'thumbnail': 're:^https?://.*\.jpg',
56                 'duration': 9103.0,
57                 'timestamp': 1410113864,
58                 'upload_date': '20140907',
59                 'view_count': int,
60             },
61             'params': {
62                 # m3u8 download
63                 'skip_download': True,
64             },
65             'skip': 'Video is no longer available',
66         },
67         {
68             # streamType: wasLive
69             'url': 'http://www.vgtv.no/#!/live/113063/direkte-v75-fra-solvalla',
70             'info_dict': {
71                 'id': '113063',
72                 'ext': 'mp4',
73                 'title': 'V75 fra Solvalla 30.05.15',
74                 'description': 'md5:b3743425765355855f88e096acc93231',
75                 'thumbnail': 're:^https?://.*\.jpg',
76                 'duration': 25966,
77                 'timestamp': 1432975582,
78                 'upload_date': '20150530',
79                 'view_count': int,
80             },
81             'params': {
82                 # m3u8 download
83                 'skip_download': True,
84             },
85         },{
86             'url': 'http://www.aftenposten.no/webtv/#!/video/21039/trailer-sweatshop-i-can-t-take-any-more',
87             'md5': '7fbc265a3ca4933a423c7a66aa879a67',
88             'info_dict': {
89                 'id': '21039',
90                 'ext': 'mp4',
91                 'title': 'TRAILER: «SWEATSHOP» - I can´t take any more',
92                 'description': 'md5:21891f2b0dd7ec2f78d84a50e54f8238',
93                 'duration': 66,
94                 'timestamp': 1417002452,
95                 'upload_date': '20141126',
96                 'view_count': int,
97             }
98         },
99         {
100             'url': 'http://www.bt.no/tv/#!/video/100250/norling-dette-er-forskjellen-paa-1-divisjon-og-eliteserien',
101             'only_matching': True,
102         },
103     ]
104     _HOST_WEBSITES = {
105         'vgtv.no': {
106             'vendor': 'vgtv',
107             'appname': 'vgtv',
108         },
109         'bt.no/tv': {
110             'vendor': 'bt',
111             'appname': 'bttv',
112         },
113         'aftenbladet.no/tv': {
114             'vendor': 'sa',
115             'appname': 'satv',
116         },
117         'fvn.no/fvntv': {
118             'vendor': 'fvn',
119             'appname': 'fvntv',
120         },
121         'aftenposten.no/webtv': {
122             'vendor': 'ap',
123             'appname': 'aptv',
124         },
125     }
126
127     def _real_extract(self, url):
128         mobj = re.match(self._VALID_URL, url)
129         video_id = mobj.group('id')
130         host = mobj.group('host')
131
132         data = self._download_json(
133             'http://svp.vg.no/svp/api/v1/%s/assets/%s?appName=%s-website'
134             % (self._HOST_WEBSITES[host]['vendor'], video_id, self._HOST_WEBSITES[host]['appname']),
135             video_id, 'Downloading media JSON')
136
137         if data.get('status') == 'inactive':
138             raise ExtractorError(
139                 'Video %s is no longer available' % video_id, expected=True)
140
141         streams = data['streamUrls']
142         stream_type = data.get('streamType')
143
144         formats = []
145
146         hls_url = streams.get('hls')
147         if hls_url:
148             formats.extend(self._extract_m3u8_formats(
149                 hls_url, video_id, 'mp4', m3u8_id='hls'))
150
151         hds_url = streams.get('hds')
152         # wasLive hds are always 404
153         if hds_url and stream_type != 'wasLive':
154             formats.extend(self._extract_f4m_formats(
155                 hds_url + '?hdcore=3.2.0&plugin=aasp-3.2.0.77.18',
156                 video_id, f4m_id='hds'))
157
158         mp4_url = streams.get('mp4')
159         if mp4_url:
160             _url = hls_url or hds_url
161             MP4_URL_TEMPLATE = '%s/%%s.%s' % (mp4_url.rpartition('/')[0], mp4_url.rpartition('.')[-1])
162             for mp4_format in _url.split(','):
163                 m = re.search('(?P<width>\d+)_(?P<height>\d+)_(?P<vbr>\d+)', mp4_format)
164                 if not m:
165                     continue
166                 width = int(m.group('width'))
167                 height = int(m.group('height'))
168                 vbr = int(m.group('vbr'))
169                 formats.append({
170                     'url': MP4_URL_TEMPLATE % mp4_format,
171                     'format_id': 'mp4-%s' % vbr,
172                     'width': width,
173                     'height': height,
174                     'vbr': vbr,
175                     'preference': 1,
176                 })
177         self._sort_formats(formats)
178
179         return {
180             'id': video_id,
181             'title': self._live_title(data['title']) if stream_type == 'live' else data['title'],
182             'description': data['description'],
183             'thumbnail': data['images']['main'] + '?t[]=900x506q80',
184             'timestamp': data['published'],
185             'duration': float_or_none(data['duration'], 1000),
186             'view_count': data['displays'],
187             'formats': formats,
188             'is_live': True if stream_type == 'live' else False,
189         }
190
191
192 class BTArticleIE(InfoExtractor):
193     IE_NAME = 'bt:article'
194     IE_DESC = 'Bergens Tidende Articles'
195     _VALID_URL = 'http://(?:www\.)?bt\.no/(?:[^/]+/)+(?P<id>[^/]+)-\d+\.html'
196     _TEST = {
197         'url': 'http://www.bt.no/nyheter/lokalt/Kjemper-for-internatet-1788214.html',
198         'md5': 'd055e8ee918ef2844745fcfd1a4175fb',
199         'info_dict': {
200             'id': '23199',
201             'ext': 'mp4',
202             'title': 'Alrekstad internat',
203             'description': 'md5:dc81a9056c874fedb62fc48a300dac58',
204             'thumbnail': 're:^https?://.*\.jpg',
205             'duration': 191,
206             'timestamp': 1289991323,
207             'upload_date': '20101117',
208             'view_count': int,
209         },
210     }
211
212     def _real_extract(self, url):
213         webpage = self._download_webpage(url, self._match_id(url))
214         video_id = self._search_regex(
215             r'SVP\.Player\.load\(\s*(\d+)', webpage, 'video id')
216         return self.url_result('http://bt.no/tv/embed?id=%s' % video_id, 'VGTV')
217
218
219 class BTVestlendingenIE(InfoExtractor):
220     IE_NAME = 'bt:vestlendingen'
221     IE_DESC = 'Bergens Tidende - Vestlendingen'
222     _VALID_URL = 'http://(?:www\.)?bt\.no/spesial/vestlendingen/#!/(?P<id>\d+)'
223     _TEST = {
224         'url': 'http://www.bt.no/spesial/vestlendingen/#!/86588',
225         'md5': 'd7d17e3337dc80de6d3a540aefbe441b',
226         'info_dict': {
227             'id': '86588',
228             'ext': 'mov',
229             'title': 'Otto Wollertsen',
230             'description': 'Vestlendingen Otto Fredrik Wollertsen',
231             'timestamp': 1430473209,
232             'upload_date': '20150501',
233         },
234     }
235
236     def _real_extract(self, url):
237         return self.url_result('xstream:btno:%s' % self._match_id(url), 'Xstream')