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