[vgtv] Check for inactive videos
[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/100015/direkte-her-kan-du-se-laksen-live-fra-suldalslaagen',
66             'info_dict': {
67                 'id': '100015',
68                 'ext': 'flv',
69                 'title': 'DIREKTE: Her kan du se laksen live fra Suldalslågen!',
70                 'description': 'md5:9a60cc23fa349f761628924e56eeec2d',
71                 'thumbnail': 're:^https?://.*\.jpg',
72                 'duration': 0,
73                 'timestamp': 1407423348,
74                 'upload_date': '20140807',
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
109         formats = []
110
111         hls_url = streams.get('hls')
112         if hls_url:
113             formats.extend(self._extract_m3u8_formats(
114                 hls_url, video_id, 'mp4', m3u8_id='hls'))
115
116         hds_url = streams.get('hds')
117         # wasLive hds are always 404
118         if hds_url and data.get('streamType') != 'wasLive':
119             formats.extend(self._extract_f4m_formats(
120                 hds_url + '?hdcore=3.2.0&plugin=aasp-3.2.0.77.18',
121                 video_id, f4m_id='hds'))
122
123         mp4_url = streams.get('mp4')
124         if mp4_url:
125             _url = hls_url or hds_url
126             MP4_URL_TEMPLATE = '%s/%%s.%s' % (mp4_url.rpartition('/')[0], mp4_url.rpartition('.')[-1])
127             for mp4_format in _url.split(','):
128                 m = re.search('(?P<width>\d+)_(?P<height>\d+)_(?P<vbr>\d+)', mp4_format)
129                 if not m:
130                     continue
131                 width = int(m.group('width'))
132                 height = int(m.group('height'))
133                 vbr = int(m.group('vbr'))
134                 formats.append({
135                     'url': MP4_URL_TEMPLATE % mp4_format,
136                     'format_id': 'mp4-%s' % vbr,
137                     'width': width,
138                     'height': height,
139                     'vbr': vbr,
140                     'preference': 1,
141                 })
142         self._sort_formats(formats)
143
144         return {
145             'id': video_id,
146             'title': data['title'],
147             'description': data['description'],
148             'thumbnail': data['images']['main'] + '?t[]=900x506q80',
149             'timestamp': data['published'],
150             'duration': float_or_none(data['duration'], 1000),
151             'view_count': data['displays'],
152             'formats': formats,
153         }
154
155
156 class BTArticleIE(InfoExtractor):
157     IE_NAME = 'bt:article'
158     IE_DESC = 'Bergens Tidende Articles'
159     _VALID_URL = 'http://(?:www\.)?bt\.no/(?:[^/]+/)+(?P<id>[^/]+)-\d+\.html'
160     _TEST = {
161         'url': 'http://www.bt.no/nyheter/lokalt/Kjemper-for-internatet-1788214.html',
162         'md5': 'd055e8ee918ef2844745fcfd1a4175fb',
163         'info_dict': {
164             'id': '23199',
165             'ext': 'mp4',
166             'title': 'Alrekstad internat',
167             'description': 'md5:dc81a9056c874fedb62fc48a300dac58',
168             'thumbnail': 're:^https?://.*\.jpg',
169             'duration': 191,
170             'timestamp': 1289991323,
171             'upload_date': '20101117',
172             'view_count': int,
173         },
174     }
175
176     def _real_extract(self, url):
177         webpage = self._download_webpage(url, self._match_id(url))
178         video_id = self._search_regex(
179             r'SVP\.Player\.load\(\s*(\d+)', webpage, 'video id')
180         return self.url_result('vgtv:bt:%s' % video_id, 'VGTV')
181
182
183 class BTVestlendingenIE(InfoExtractor):
184     IE_NAME = 'bt:vestlendingen'
185     IE_DESC = 'Bergens Tidende - Vestlendingen'
186     _VALID_URL = 'http://(?:www\.)?bt\.no/spesial/vestlendingen/#!/(?P<id>\d+)'
187     _TEST = {
188         'url': 'http://www.bt.no/spesial/vestlendingen/#!/86588',
189         'md5': 'd7d17e3337dc80de6d3a540aefbe441b',
190         'info_dict': {
191             'id': '86588',
192             'ext': 'mov',
193             'title': 'Otto Wollertsen',
194             'description': 'Vestlendingen Otto Fredrik Wollertsen',
195             'timestamp': 1430473209,
196             'upload_date': '20150501',
197         },
198     }
199
200     def _real_extract(self, url):
201         return self.url_result('xstream:btno:%s' % self._match_id(url), 'Xstream')