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