]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/hbo.py
[nhk] add support for audio URLs
[youtube-dl.git] / 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 ..utils import (
8     xpath_text,
9     xpath_element,
10     int_or_none,
11     parse_duration,
12     urljoin,
13 )
14
15
16 class HBOIE(InfoExtractor):
17     IE_NAME = 'hbo'
18     _VALID_URL = r'https?://(?:www\.)?hbo\.com/(?:video|embed)(?:/[^/]+)*/(?P<id>[^/?#]+)'
19     _TEST = {
20         'url': 'https://www.hbo.com/video/game-of-thrones/seasons/season-8/videos/trailer',
21         'md5': '8126210656f433c452a21367f9ad85b3',
22         'info_dict': {
23             'id': '22113301',
24             'ext': 'mp4',
25             'title': 'Game of Thrones - Trailer',
26         },
27         'expected_warnings': ['Unknown MIME type application/mp4 in DASH manifest'],
28     }
29     _FORMATS_INFO = {
30         'pro7': {
31             'width': 1280,
32             'height': 720,
33         },
34         '1920': {
35             'width': 1280,
36             'height': 720,
37         },
38         'pro6': {
39             'width': 768,
40             'height': 432,
41         },
42         '640': {
43             'width': 768,
44             'height': 432,
45         },
46         'pro5': {
47             'width': 640,
48             'height': 360,
49         },
50         'highwifi': {
51             'width': 640,
52             'height': 360,
53         },
54         'high3g': {
55             'width': 640,
56             'height': 360,
57         },
58         'medwifi': {
59             'width': 400,
60             'height': 224,
61         },
62         'med3g': {
63             'width': 400,
64             'height': 224,
65         },
66     }
67
68     def _real_extract(self, url):
69         display_id = self._match_id(url)
70         webpage = self._download_webpage(url, display_id)
71         location_path = self._parse_json(self._html_search_regex(
72             r'data-state="({.+?})"', webpage, 'state'), display_id)['video']['locationUrl']
73         video_data = self._download_xml(urljoin(url, location_path), display_id)
74         video_id = xpath_text(video_data, 'id', fatal=True)
75         episode_title = title = xpath_text(video_data, 'title', fatal=True)
76         series = xpath_text(video_data, 'program')
77         if series:
78             title = '%s - %s' % (series, title)
79
80         formats = []
81         for source in xpath_element(video_data, 'videos', 'sources', True):
82             if source.tag == 'size':
83                 path = xpath_text(source, './/path')
84                 if not path:
85                     continue
86                 width = source.attrib.get('width')
87                 format_info = self._FORMATS_INFO.get(width, {})
88                 height = format_info.get('height')
89                 fmt = {
90                     'url': path,
91                     'format_id': 'http%s' % ('-%dp' % height if height else ''),
92                     'width': format_info.get('width'),
93                     'height': height,
94                 }
95                 rtmp = re.search(r'^(?P<url>rtmpe?://[^/]+/(?P<app>.+))/(?P<playpath>mp4:.+)$', path)
96                 if rtmp:
97                     fmt.update({
98                         'url': rtmp.group('url'),
99                         'play_path': rtmp.group('playpath'),
100                         'app': rtmp.group('app'),
101                         'ext': 'flv',
102                         'format_id': fmt['format_id'].replace('http', 'rtmp'),
103                     })
104                 formats.append(fmt)
105             else:
106                 video_url = source.text
107                 if not video_url:
108                     continue
109                 if source.tag == 'tarball':
110                     formats.extend(self._extract_m3u8_formats(
111                         video_url.replace('.tar', '/base_index_w8.m3u8'),
112                         video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
113                 elif source.tag == 'hls':
114                     m3u8_formats = self._extract_m3u8_formats(
115                         video_url.replace('.tar', '/base_index.m3u8'),
116                         video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
117                     for f in m3u8_formats:
118                         if f.get('vcodec') == 'none' and not f.get('tbr'):
119                             f['tbr'] = int_or_none(self._search_regex(
120                                 r'-(\d+)k/', f['url'], 'tbr', default=None))
121                     formats.extend(m3u8_formats)
122                 elif source.tag == 'dash':
123                     formats.extend(self._extract_mpd_formats(
124                         video_url.replace('.tar', '/manifest.mpd'),
125                         video_id, mpd_id='dash', fatal=False))
126                 else:
127                     format_info = self._FORMATS_INFO.get(source.tag, {})
128                     formats.append({
129                         'format_id': 'http-%s' % source.tag,
130                         'url': video_url,
131                         'width': format_info.get('width'),
132                         'height': format_info.get('height'),
133                     })
134         self._sort_formats(formats)
135
136         thumbnails = []
137         card_sizes = xpath_element(video_data, 'titleCardSizes')
138         if card_sizes is not None:
139             for size in card_sizes:
140                 path = xpath_text(size, 'path')
141                 if not path:
142                     continue
143                 width = int_or_none(size.get('width'))
144                 thumbnails.append({
145                     'id': width,
146                     'url': path,
147                     'width': width,
148                 })
149
150         subtitles = None
151         caption_url = xpath_text(video_data, 'captionUrl')
152         if caption_url:
153             subtitles = {
154                 'en': [{
155                     'url': caption_url,
156                     'ext': 'ttml'
157                 }],
158             }
159
160         return {
161             'id': video_id,
162             'title': title,
163             'duration': parse_duration(xpath_text(video_data, 'duration/tv14')),
164             'series': series,
165             'episode': episode_title,
166             'formats': formats,
167             'thumbnails': thumbnails,
168             'subtitles': subtitles,
169         }