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