# coding: utf-8 from __future__ import unicode_literals import re from .common import InfoExtractor from ..utils import ( xpath_text, xpath_element, int_or_none, parse_duration, ) class HBOBaseIE(InfoExtractor): _FORMATS_INFO = { 'pro7': { 'width': 1280, 'height': 720, }, '1920': { 'width': 1280, 'height': 720, }, 'pro6': { 'width': 768, 'height': 432, }, '640': { 'width': 768, 'height': 432, }, 'pro5': { 'width': 640, 'height': 360, }, 'highwifi': { 'width': 640, 'height': 360, }, 'high3g': { 'width': 640, 'height': 360, }, 'medwifi': { 'width': 400, 'height': 224, }, 'med3g': { 'width': 400, 'height': 224, }, } def _extract_from_id(self, video_id): video_data = self._download_xml( 'https://www.hbo.com/services/hbo/video.xml/vpath=/content/hbodata/en/%s.xml' % video_id, video_id) title = xpath_text(video_data, 'title', 'title', True) formats = [] for source in xpath_element(video_data, 'videos', 'sources', True): if source.tag == 'size': path = xpath_text(source, './/path') if not path: continue width = source.attrib.get('width') format_info = self._FORMATS_INFO.get(width, {}) height = format_info.get('height') fmt = { 'url': path, 'format_id': 'http%s' % ('-%dp' % height if height else ''), 'width': format_info.get('width'), 'height': height, } rtmp = re.search(r'^(?Prtmpe?://[^/]+/(?P.+))/(?Pmp4:.+)$', path) if rtmp: fmt.update({ 'url': rtmp.group('url'), 'play_path': rtmp.group('playpath'), 'app': rtmp.group('app'), 'ext': 'flv', 'format_id': fmt['format_id'].replace('http', 'rtmp'), }) formats.append(fmt) else: video_url = source.text if not video_url: continue if source.tag == 'tarball': formats.extend(self._extract_m3u8_formats( video_url.replace('.tar', '/base_index_w8.m3u8'), video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)) elif source.tag == 'hls': m3u8_formats = self._extract_m3u8_formats( video_url.replace('.tar', '/base_index.m3u8'), video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False) for f in m3u8_formats: if f.get('vcodec') == 'none' and not f.get('tbr'): f['tbr'] = int_or_none(self._search_regex( r'-(\d+)k/', f['url'], 'tbr', default=None)) formats.extend(m3u8_formats) elif source.tag == 'dash': formats.extend(self._extract_mpd_formats( video_url.replace('.tar', '/manifest.mpd'), video_id, mpd_id='dash', fatal=False)) else: format_info = self._FORMATS_INFO.get(source.tag, {}) formats.append({ 'format_id': 'http-%s' % source.tag, 'url': video_url, 'width': format_info.get('width'), 'height': format_info.get('height'), }) self._sort_formats(formats) thumbnails = [] card_sizes = xpath_element(video_data, 'titleCardSizes') if card_sizes is not None: for size in card_sizes: path = xpath_text(size, 'path') if not path: continue width = int_or_none(size.get('width')) thumbnails.append({ 'id': width, 'url': path, 'width': width, }) return { 'id': video_id, 'title': title, 'duration': parse_duration(xpath_text(video_data, 'duration/tv14')), 'formats': formats, 'thumbnails': thumbnails, } class HBOIE(HBOBaseIE): IE_NAME = 'hbo' _VALID_URL = r'https?://(?:www\.)?hbo\.com/video/(?P(specials|movies|boxing)[0-9a-z-/]+)' _TESTS = [{ 'url': 'https://www.hbo.com/video/movies/notes-from-the-field/videos/trailer', 'md5': 'c8779130d9b458caa52f670f2ee28084', 'info_dict': { 'id': 'movies/notes-from-the-field/videos/trailer', 'ext': 'mp4', 'title': 'Trailer', 'thumbnail': r're:https?://.*\.jpg$', 'duration': 65, } }, { 'url': 'https://www.hbo.com/video/specials/tour-de-pharmacy/videos/trailer', 'md5': 'd8cd9aca78fa3a01bd883a403dc7da9c', 'info_dict': { 'id': 'specials/tour-de-pharmacy/videos/trailer', 'ext': 'mp4', 'title': 'Trailer', 'thumbnail': r're:https?://.*\.jpg$', 'duration': 107, } }, { 'url': 'https://www.hbo.com/video/boxing/2018/event/2018-03-03-kovalev-vs-mikhalkin/videos/kovalev-vs-mikhalkin-preview', 'md5': '3beea848a5b7c8eebe8654ae85daa8e6', 'info_dict': { 'id': 'boxing/2018/event/2018-03-03-kovalev-vs-mikhalkin/videos/kovalev-vs-mikhalkin-preview', 'ext': 'mp4', 'title': 'Kovalev vs. Mikhalkin Preview', 'thumbnail': r're:https?://.*\.jpg$', 'duration': 46, } }] def _real_extract(self, url): video_id = self._match_id(url) return self._extract_from_id(video_id) class HBOEpisodeIE(HBOBaseIE): IE_NAME = 'hbo:episode' _VALID_URL = r'https?://(?:www\.)?hbo\.com/video/(?P(?!specials|movies|boxing)[0-9a-z-/]+)' _TESTS = [{ 'url': 'https://www.hbo.com/video/girls/seasons/season-05/episodes/1-wedding-day/videos/s5-ep-1-wedding-day-recap', 'md5': 'e580d9ab668f5bff196af43c068de850', 'info_dict': { 'id': 'series/girls/seasons/season-05/episodes/1-wedding-day/videos/s5-ep-1-wedding-day-recap', 'ext': 'mp4', 'title': 'S5 Ep 1: Wedding Day - Recap', 'thumbnail': r're:https?://.*\.jpg$', 'duration': 40, } }] def _real_extract(self, url): video_id = 'series/' + self._match_id(url) return self._extract_from_id(video_id)