Merge branch 'dbtv' of https://github.com/mrkolby/youtube-dl into mrkolby-dbtv
[youtube-dl] / youtube_dl / extractor / dbtv.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 DBTVIE(InfoExtractor):
13   _VALID_URL = r'http://dbtv.no/(?P<id>[0-9]+)/?(?P<slug>.*)$'
14   _TEST = {
15     'url': 'http://dbtv.no/3649835190001#Skulle_teste_ut_fornøyelsespark,_men_kollegaen_var_bare_opptatt_av_bikinikroppen',
16     'md5': 'b89953ed25dacb6edb3ef6c6f430f8bc',
17     'info_dict': {
18       'id': '3649835190001',
19       'ext': 'mp4',
20       'title': 'Skulle teste ut fornøyelsespark, men kollegaen var bare opptatt av bikinikroppen',
21       'description': 'md5:d681bf2bb7dd3503892cedb9c2d0e6f2',
22       'thumbnail': 'http://gfx.dbtv.no/thumbs/still/33100.jpg',
23       'timestamp': 1404039863,
24       'upload_date': '20140629',
25       'duration': 69544,
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://api.dbtv.no/discovery/%s' % video_id, video_id, 'Downloading media JSON')
35     # We only want the first video in the JSON API file.
36     video = data['playlist'][0]
37
38     # Check for full HD video, else use the standard video URL
39     for i in range(0, len(video['renditions'])):
40       if int(video['renditions'][i]['width']) == 1280:
41         video_url = video['renditions'][i]['URL']
42         break
43       else:
44         video_url = video['URL']
45
46     # Add access token to image or it will fail.
47     thumbnail = video['splash']
48
49     # Duration int.
50     duration = int(video['length'])
51
52     # Timestamp is given in milliseconds.
53     timestamp = float(str(video['publishedAt'])[0:-3])
54
55     formats = []
56
57     # Video URL.
58     if video['URL'] is not None:
59       formats.append({
60         'url': video_url,
61         'format_id': 'mp4',
62         'ext': 'mp4'
63       })
64     else:
65       raise ExtractorError('No download URL found for video: %s.' % video_id, expected=True)
66
67     return {
68       'id': video_id,
69       'title': video['title'],
70       'description': video['desc'],
71       'thumbnail': thumbnail,
72       'timestamp': timestamp,
73       'duration': duration,
74       'view_count': video['views'],
75       'formats': formats,
76     }