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