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