[vgtv] Add support for bt.no articles (#5620)
[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 float_or_none
8
9
10 class VGTVIE(InfoExtractor):
11     IE_DESC = 'VGTV and BTTV'
12     _VALID_URL = r'''(?x)
13                     (?:
14                         vgtv:|
15                         http://(?:www\.)?
16                     )
17                     (?P<host>vgtv|bt)
18                     (?:
19                         :|
20                         \.no/(?:tv/)?#!/(?:video|live)/
21                     )
22                     (?P<id>[0-9]+)
23                     '''
24     _TESTS = [
25         {
26             # streamType: vod
27             'url': 'http://www.vgtv.no/#!/video/84196/hevnen-er-soet-episode-10-abu',
28             'md5': 'b8be7a234cebb840c0d512c78013e02f',
29             'info_dict': {
30                 'id': '84196',
31                 'ext': 'mp4',
32                 'title': 'Hevnen er søt: Episode 10 - Abu',
33                 'description': 'md5:e25e4badb5f544b04341e14abdc72234',
34                 'thumbnail': 're:^https?://.*\.jpg',
35                 'duration': 648.000,
36                 'timestamp': 1404626400,
37                 'upload_date': '20140706',
38                 'view_count': int,
39             },
40         },
41         {
42             # streamType: wasLive
43             'url': 'http://www.vgtv.no/#!/live/100764/opptak-vgtv-foelger-em-kvalifiseringen',
44             'info_dict': {
45                 'id': '100764',
46                 'ext': 'flv',
47                 'title': 'OPPTAK: VGTV følger EM-kvalifiseringen',
48                 'description': 'md5:3772d9c0dc2dff92a886b60039a7d4d3',
49                 'thumbnail': 're:^https?://.*\.jpg',
50                 'duration': 9103.0,
51                 'timestamp': 1410113864,
52                 'upload_date': '20140907',
53                 'view_count': int,
54             },
55             'params': {
56                 # m3u8 download
57                 'skip_download': True,
58             },
59         },
60         {
61             # streamType: live
62             'url': 'http://www.vgtv.no/#!/live/100015/direkte-her-kan-du-se-laksen-live-fra-suldalslaagen',
63             'info_dict': {
64                 'id': '100015',
65                 'ext': 'flv',
66                 'title': 'DIREKTE: Her kan du se laksen live fra Suldalslågen!',
67                 'description': 'md5:9a60cc23fa349f761628924e56eeec2d',
68                 'thumbnail': 're:^https?://.*\.jpg',
69                 'duration': 0,
70                 'timestamp': 1407423348,
71                 'upload_date': '20140807',
72                 'view_count': int,
73             },
74             'params': {
75                 # m3u8 download
76                 'skip_download': True,
77             },
78         },
79         {
80             'url': 'http://www.bt.no/tv/#!/video/100250/norling-dette-er-forskjellen-paa-1-divisjon-og-eliteserien',
81             'only_matching': True,
82         },
83     ]
84
85     def _real_extract(self, url):
86         mobj = re.match(self._VALID_URL, url)
87         video_id = mobj.group('id')
88         host = mobj.group('host')
89
90         HOST_WEBSITES = {
91             'vgtv': 'vgtv',
92             'bt': 'bttv',
93         }
94
95         data = self._download_json(
96             'http://svp.vg.no/svp/api/v1/%s/assets/%s?appName=%s-website'
97             % (host, video_id, HOST_WEBSITES[host]),
98             video_id, 'Downloading media JSON')
99
100         streams = data['streamUrls']
101
102         formats = []
103
104         hls_url = streams.get('hls')
105         if hls_url:
106             formats.extend(self._extract_m3u8_formats(hls_url, video_id, 'mp4'))
107
108         hds_url = streams.get('hds')
109         if hds_url:
110             formats.extend(self._extract_f4m_formats(hds_url + '?hdcore=3.2.0&plugin=aasp-3.2.0.77.18', video_id))
111
112         mp4_url = streams.get('mp4')
113         if mp4_url:
114             _url = hls_url or hds_url
115             MP4_URL_TEMPLATE = '%s/%%s.%s' % (mp4_url.rpartition('/')[0], mp4_url.rpartition('.')[-1])
116             for mp4_format in _url.split(','):
117                 m = re.search('(?P<width>\d+)_(?P<height>\d+)_(?P<vbr>\d+)', mp4_format)
118                 if not m:
119                     continue
120                 width = int(m.group('width'))
121                 height = int(m.group('height'))
122                 vbr = int(m.group('vbr'))
123                 formats.append({
124                     'url': MP4_URL_TEMPLATE % mp4_format,
125                     'format_id': 'mp4-%s' % vbr,
126                     'width': width,
127                     'height': height,
128                     'vbr': vbr,
129                     'preference': 1,
130                 })
131         self._sort_formats(formats)
132
133         return {
134             'id': video_id,
135             'title': data['title'],
136             'description': data['description'],
137             'thumbnail': data['images']['main'] + '?t[]=900x506q80',
138             'timestamp': data['published'],
139             'duration': float_or_none(data['duration'], 1000),
140             'view_count': data['displays'],
141             'formats': formats,
142         }
143
144
145 class BTArticleIE(InfoExtractor):
146     IE_DESC = 'Bergens Tidende'
147     _VALID_URL = 'http://(?:www\.)?bt\.no/(?:[^/]+/)+(?P<id>[^/]+)-\d+\.html'
148     _TEST = {
149         'url': 'http://www.bt.no/nyheter/lokalt/Kjemper-for-internatet-1788214.html',
150         'md5': 'd055e8ee918ef2844745fcfd1a4175fb',
151         'info_dict': {
152             'id': '23199',
153             'ext': 'mp4',
154             'title': 'Alrekstad internat',
155             'description': 'md5:dc81a9056c874fedb62fc48a300dac58',
156             'thumbnail': 're:^https?://.*\.jpg',
157             'duration': 191,
158             'timestamp': 1289991323,
159             'upload_date': '20101117',
160             'view_count': int,
161         },
162     }
163
164     def _real_extract(self, url):
165         webpage = self._download_webpage(url, self._match_id(url))
166         video_id = self._search_regex(
167             r'SVP\.Player\.load\(\s*(\d+)', webpage, 'video id')
168         return self.url_result('vgtv:bt:%s' % video_id, 'VGTV')