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