[DRBonanza] Add new extractor (fixing #4581)
[youtube-dl] / youtube_dl / extractor / drbonanza.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from .common import ExtractorError
5 from ..utils import parse_iso8601
6 import json
7 import re
8
9 class DRBonanzaIE(InfoExtractor):
10     _VALID_URL = r'https?://(?:www\.)?dr\.dk/bonanza/(?:[^/]+/)+(?:[^/])+?(?:assetId=(?P<id>\d+))?(?:[#&]|$)'
11
12     _TESTS = [{
13         'url': 'http://www.dr.dk/bonanza/serie/portraetter/Talkshowet.htm?assetId=65517',
14         'md5': 'fe330252ddea607635cf2eb2c99a0af3',
15         'info_dict': {
16             'id': '65517',
17             'ext': 'mp4',
18             'title': 'Talkshowet - Leonard Cohen',
19             'description': 'md5:8f34194fb30cd8c8a30ad8b27b70c0ca',
20             'timestamp': 1295537932,
21             'upload_date': '20110120',
22             'duration': 3664000,
23         },
24     },{
25         'url': 'http://www.dr.dk/bonanza/radio/serie/sport/fodbold.htm?assetId=59410',
26         'md5': '6dfe039417e76795fb783c52da3de11d',
27         'info_dict': {
28             'id': '59410',
29             'ext': 'mp3',
30             'title': 'EM fodbold 1992 Danmark - Tyskland finale Transmission',
31             'description': 'md5:501e5a195749480552e214fbbed16c4e',
32             'timestamp': 1223274900,
33             'upload_date': '20081006',
34             'duration': 7369000,
35         },
36     }]
37
38     def _real_extract(self, url):
39         url_id = self._match_id(url)
40         
41         webpage = self._download_webpage(url, url_id if url_id else "")
42         
43         if url_id:
44             info = json.loads(self._html_search_regex(r'({.*?' + url_id + '.*})', webpage, 'json'))
45         else:
46             # Just fetch the first video on that page
47             info = json.loads(self._html_search_regex(r'bonanzaFunctions.newPlaylist\(({.*})\)', webpage, 'json'))
48         
49         asset_id = str(info['AssetId'])
50         title = info['Title'].rstrip(' \'\"-,.:;!?')
51         duration = info['Duration']
52         timestamp = parse_iso8601(re.sub(r'\.\d+$', '', info['Created'])) # First published online. "FirstPublished" contains the date for original airing.
53         
54         def parse_filename_info(url):
55             match = re.search(r'/\d+_(?P<width>\d+)x(?P<height>\d+)x(?P<bitrate>\d+)K\.(?P<ext>\w+)$', url)
56             if match:
57                 return {'width': int(match.group(1)), 'height': int(match.group(2)), 'bitrate': int(match.group(3)), 'ext': match.group(4)}
58             match = re.search(r'/\d+_(?P<bitrate>\d+)K\.(?P<ext>\w+)$', url)
59             if match:
60                 return {'bitrate': int(match.group(1)), 'ext': match.group(2)}
61             return {'width': None, 'height': None, 'bitrate': None, 'ext': None}
62         
63         video_types = ['VideoHigh', 'VideoMid', 'VideoLow']
64         preferencemap = {
65             'VideoHigh': -1,
66             'VideoMid': -2,
67             'VideoLow': -3,
68             'Audio': -4,
69         }
70         
71         formats = []
72         for file in info['Files']:
73             if info['Type'] == "Video":
74                 if file['Type'] in video_types:
75                     fileinfo = parse_filename_info(file['Location'])
76                     formats.append({
77                         'url': file['Location'],
78                         'format_id': file['Type'].replace('Video', ''),
79                         'preference': preferencemap.get(file['Type'], -10),
80                         'width': fileinfo['width'],
81                         'height': fileinfo['height'],
82                         'vbr': fileinfo['bitrate'],
83                         'ext': fileinfo['ext'],
84                     })
85                 elif file['Type'] == "Thumb":
86                     thumbnail = file['Location']
87             elif info['Type'] == "Audio":
88                 if file['Type'] == "Audio":
89                     fileinfo = parse_filename_info(file['Location'])
90                     formats.append({
91                         'url': file['Location'],
92                         'format_id': file['Type'],
93                         'abr': fileinfo['bitrate'],
94                         'ext': fileinfo['ext'],
95                         'vcodec': 'none',
96                     })
97                 elif file['Type'] == "Thumb":
98                     thumbnail = file['Location']
99         
100         description = "{}\n{}\n{}\n".format(info['Description'], info['Actors'], info['Colophon'])
101
102         for f in formats:
103             f['url'] = f['url'].replace('rtmp://vod-bonanza.gss.dr.dk/bonanza/', 'http://vodfiles.dr.dk/')
104             f['url'] = f['url'].replace('mp4:bonanza', 'bonanza')
105         
106         self._sort_formats(formats)
107         
108         display_id = re.sub(r'[^\w\d-]', '', re.sub(r' ', '-', title.lower())) + '-' + asset_id
109         display_id = re.sub(r'-+', '-', display_id)
110         
111         return {
112             'id': asset_id,
113             'display_id': display_id,
114             'title': title,
115             'formats': formats,
116             'description': description,
117             'thumbnail': thumbnail,
118             'timestamp': timestamp,
119             'duration': duration,
120         }