Merge branch 'vgtv' of https://github.com/mrkolby/youtube-dl into 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
8 from ..utils import (
9   ExtractorError
10 )
11
12 class VGTVIE(InfoExtractor):
13   # Because of the #! in the URL structure we need to add ' before and after given URL.
14   # Or else it will cry: -bash: !/video/100495/lars-og-lars-sesong-6-episode-6-lakselus: event not found
15   _VALID_URL = r'http://(?:www\.)?vgtv\.no/#!/(?:.*)/(?P<id>[0-9]+)/(?P<title>[^?#]*)'
16   _TEST = {
17     'url': 'http://www.vgtv.no/#!/video/84196/hevnen-er-soet-episode-10-abu',
18     'md5': 'b8be7a234cebb840c0d512c78013e02f',
19     'info_dict': {
20       'id': '84196',
21       'ext': 'mp4',
22       'title': 'Hevnen er søt episode 10: Abu',
23       'description': 'md5:e25e4badb5f544b04341e14abdc72234',
24       'timestamp': 1404626400,
25       'upload_date': '20140706'
26     }
27   }
28
29   def _real_extract(self, url):
30     mobj = re.match(self._VALID_URL, url)
31     video_id = mobj.group('id')
32
33     # Download JSON file containing video info.
34     data = self._download_json('http://svp.vg.no/svp/api/v1/vgtv/assets/%s?appName=vgtv-website' % video_id, video_id, 'Downloading media JSON')
35
36     # Known streamType: vod, live, wasLive
37     # Will it even be possible to add support for live streams?
38     if data['streamType'] != 'vod':
39       raise ExtractorError('Stream type \'%s\' is not yet supported.' % data['streamType'], expected=True)
40
41     # Add access token to image or it will fail.
42     thumbnail = data['images']['main'] + '?t[]=900x506q80'
43
44     formats = []
45
46     # Most videos are in MP4, but some are either HLS or HDS.
47     # Don't want to support HDS.
48     if data['streamUrls']['mp4'] is not None:
49       formats.append({
50         'url': data['streamUrls']['mp4'],
51         'format_id': 'mp4',
52         'ext': 'mp4'
53       })
54     elif data['streamUrls']['hls'] is not None:
55       self.to_screen(u'No MP4 URL found, using m3u8. This may take some extra time.')
56       formats.append({
57         'url': data['streamUrls']['hls'],
58         'format_id': 'm3u8',
59         'ext': 'mp4'
60       })
61     else:
62       raise ExtractorError('No download URL found for video: %s.' % video_id, expected=True)
63
64     return {
65       'id': video_id,
66       'title': data['title'],
67       'description': data['description'],
68       'thumbnail': thumbnail,
69       'timestamp': data['published'],
70       'duration': data['duration'],
71       'view_count': data['displays'],
72       'formats': formats,
73     }