]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/hbo.py
[downloader/http] Randomize HTTP chunk size
[youtube-dl] / youtube_dl / extractor / hbo.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_str
8 from ..utils import (
9     xpath_text,
10     xpath_element,
11     int_or_none,
12     parse_duration,
13 )
14
15
16 class HBOBaseIE(InfoExtractor):
17     _FORMATS_INFO = {
18         'pro7': {
19             'width': 1280,
20             'height': 720,
21         },
22         '1920': {
23             'width': 1280,
24             'height': 720,
25         },
26         'pro6': {
27             'width': 768,
28             'height': 432,
29         },
30         '640': {
31             'width': 768,
32             'height': 432,
33         },
34         'pro5': {
35             'width': 640,
36             'height': 360,
37         },
38         'highwifi': {
39             'width': 640,
40             'height': 360,
41         },
42         'high3g': {
43             'width': 640,
44             'height': 360,
45         },
46         'medwifi': {
47             'width': 400,
48             'height': 224,
49         },
50         'med3g': {
51             'width': 400,
52             'height': 224,
53         },
54     }
55
56     def _extract_from_id(self, video_id):
57         video_data = self._download_xml(
58             'http://render.lv3.hbo.com/data/content/global/videos/data/%s.xml' % video_id, video_id)
59         title = xpath_text(video_data, 'title', 'title', True)
60
61         formats = []
62         for source in xpath_element(video_data, 'videos', 'sources', True):
63             if source.tag == 'size':
64                 path = xpath_text(source, './/path')
65                 if not path:
66                     continue
67                 width = source.attrib.get('width')
68                 format_info = self._FORMATS_INFO.get(width, {})
69                 height = format_info.get('height')
70                 fmt = {
71                     'url': path,
72                     'format_id': 'http%s' % ('-%dp' % height if height else ''),
73                     'width': format_info.get('width'),
74                     'height': height,
75                 }
76                 rtmp = re.search(r'^(?P<url>rtmpe?://[^/]+/(?P<app>.+))/(?P<playpath>mp4:.+)$', path)
77                 if rtmp:
78                     fmt.update({
79                         'url': rtmp.group('url'),
80                         'play_path': rtmp.group('playpath'),
81                         'app': rtmp.group('app'),
82                         'ext': 'flv',
83                         'format_id': fmt['format_id'].replace('http', 'rtmp'),
84                     })
85                 formats.append(fmt)
86             else:
87                 video_url = source.text
88                 if not video_url:
89                     continue
90                 if source.tag == 'tarball':
91                     formats.extend(self._extract_m3u8_formats(
92                         video_url.replace('.tar', '/base_index_w8.m3u8'),
93                         video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
94                 elif source.tag == 'hls':
95                     m3u8_formats = self._extract_m3u8_formats(
96                         video_url.replace('.tar', '/base_index.m3u8'),
97                         video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
98                     for f in m3u8_formats:
99                         if f.get('vcodec') == 'none' and not f.get('tbr'):
100                             f['tbr'] = int_or_none(self._search_regex(
101                                 r'-(\d+)k/', f['url'], 'tbr', default=None))
102                     formats.extend(m3u8_formats)
103                 elif source.tag == 'dash':
104                     formats.extend(self._extract_mpd_formats(
105                         video_url.replace('.tar', '/manifest.mpd'),
106                         video_id, mpd_id='dash', fatal=False))
107                 else:
108                     format_info = self._FORMATS_INFO.get(source.tag, {})
109                     formats.append({
110                         'format_id': 'http-%s' % source.tag,
111                         'url': video_url,
112                         'width': format_info.get('width'),
113                         'height': format_info.get('height'),
114                     })
115         self._sort_formats(formats)
116
117         thumbnails = []
118         card_sizes = xpath_element(video_data, 'titleCardSizes')
119         if card_sizes is not None:
120             for size in card_sizes:
121                 path = xpath_text(size, 'path')
122                 if not path:
123                     continue
124                 width = int_or_none(size.get('width'))
125                 thumbnails.append({
126                     'id': width,
127                     'url': path,
128                     'width': width,
129                 })
130
131         return {
132             'id': video_id,
133             'title': title,
134             'duration': parse_duration(xpath_text(video_data, 'duration/tv14')),
135             'formats': formats,
136             'thumbnails': thumbnails,
137         }
138
139
140 class HBOIE(HBOBaseIE):
141     IE_NAME = 'hbo'
142     _VALID_URL = r'https?://(?:www\.)?hbo\.com/video/video\.html\?.*vid=(?P<id>[0-9]+)'
143     _TEST = {
144         'url': 'http://www.hbo.com/video/video.html?autoplay=true&g=u&vid=1437839',
145         'md5': '2c6a6bc1222c7e91cb3334dad1746e5a',
146         'info_dict': {
147             'id': '1437839',
148             'ext': 'mp4',
149             'title': 'Ep. 64 Clip: Encryption',
150             'thumbnail': r're:https?://.*\.jpg$',
151             'duration': 1072,
152         }
153     }
154
155     def _real_extract(self, url):
156         video_id = self._match_id(url)
157         return self._extract_from_id(video_id)
158
159
160 class HBOEpisodeIE(HBOBaseIE):
161     IE_NAME = 'hbo:episode'
162     _VALID_URL = r'https?://(?:www\.)?hbo\.com/(?P<path>(?!video)(?:(?:[^/]+/)+video|watch-free-episodes)/(?P<id>[0-9a-z-]+))(?:\.html)?'
163
164     _TESTS = [{
165         'url': 'http://www.hbo.com/girls/episodes/5/52-i-love-you-baby/video/ep-52-inside-the-episode.html?autoplay=true',
166         'md5': '61ead79b9c0dfa8d3d4b07ef4ac556fb',
167         'info_dict': {
168             'id': '1439518',
169             'display_id': 'ep-52-inside-the-episode',
170             'ext': 'mp4',
171             'title': 'Ep. 52: Inside the Episode',
172             'thumbnail': r're:https?://.*\.jpg$',
173             'duration': 240,
174         },
175     }, {
176         'url': 'http://www.hbo.com/game-of-thrones/about/video/season-5-invitation-to-the-set.html?autoplay=true',
177         'only_matching': True,
178     }, {
179         'url': 'http://www.hbo.com/watch-free-episodes/last-week-tonight-with-john-oliver',
180         'only_matching': True,
181     }]
182
183     def _real_extract(self, url):
184         path, display_id = re.match(self._VALID_URL, url).groups()
185
186         content = self._download_json(
187             'http://www.hbo.com/api/content/' + path, display_id)['content']
188
189         video_id = compat_str((content.get('parsed', {}).get(
190             'common:FullBleedVideo', {}) or content['selectedEpisode'])['videoId'])
191
192         info_dict = self._extract_from_id(video_id)
193         info_dict['display_id'] = display_id
194
195         return info_dict