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