Fix "invalid escape sequences" error on Python 3.6
[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 ..utils import (
8     xpath_text,
9     xpath_element,
10     int_or_none,
11     parse_duration,
12 )
13
14
15 class HBOBaseIE(InfoExtractor):
16     _FORMATS_INFO = {
17         '1920': {
18             'width': 1280,
19             'height': 720,
20         },
21         '640': {
22             'width': 768,
23             'height': 432,
24         },
25         'highwifi': {
26             'width': 640,
27             'height': 360,
28         },
29         'high3g': {
30             'width': 640,
31             'height': 360,
32         },
33         'medwifi': {
34             'width': 400,
35             'height': 224,
36         },
37         'med3g': {
38             'width': 400,
39             'height': 224,
40         },
41     }
42
43     def _extract_from_id(self, video_id):
44         video_data = self._download_xml(
45             'http://render.lv3.hbo.com/data/content/global/videos/data/%s.xml' % video_id, video_id)
46         title = xpath_text(video_data, 'title', 'title', True)
47
48         formats = []
49         for source in xpath_element(video_data, 'videos', 'sources', True):
50             if source.tag == 'size':
51                 path = xpath_text(source, './/path')
52                 if not path:
53                     continue
54                 width = source.attrib.get('width')
55                 format_info = self._FORMATS_INFO.get(width, {})
56                 height = format_info.get('height')
57                 fmt = {
58                     'url': path,
59                     'format_id': 'http%s' % ('-%dp' % height if height else ''),
60                     'width': format_info.get('width'),
61                     'height': height,
62                 }
63                 rtmp = re.search(r'^(?P<url>rtmpe?://[^/]+/(?P<app>.+))/(?P<playpath>mp4:.+)$', path)
64                 if rtmp:
65                     fmt.update({
66                         'url': rtmp.group('url'),
67                         'play_path': rtmp.group('playpath'),
68                         'app': rtmp.group('app'),
69                         'ext': 'flv',
70                         'format_id': fmt['format_id'].replace('http', 'rtmp'),
71                     })
72                 formats.append(fmt)
73             else:
74                 video_url = source.text
75                 if not video_url:
76                     continue
77                 if source.tag == 'tarball':
78                     formats.extend(self._extract_m3u8_formats(
79                         video_url.replace('.tar', '/base_index_w8.m3u8'),
80                         video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
81                 else:
82                     format_info = self._FORMATS_INFO.get(source.tag, {})
83                     formats.append({
84                         'format_id': 'http-%s' % source.tag,
85                         'url': video_url,
86                         'width': format_info.get('width'),
87                         'height': format_info.get('height'),
88                     })
89         self._sort_formats(formats, ('width', 'height', 'tbr', 'format_id'))
90
91         thumbnails = []
92         card_sizes = xpath_element(video_data, 'titleCardSizes')
93         if card_sizes is not None:
94             for size in card_sizes:
95                 path = xpath_text(size, 'path')
96                 if not path:
97                     continue
98                 width = int_or_none(size.get('width'))
99                 thumbnails.append({
100                     'id': width,
101                     'url': path,
102                     'width': width,
103                 })
104
105         return {
106             'id': video_id,
107             'title': title,
108             'duration': parse_duration(xpath_text(video_data, 'duration/tv14')),
109             'formats': formats,
110             'thumbnails': thumbnails,
111         }
112
113
114 class HBOIE(HBOBaseIE):
115     _VALID_URL = r'https?://(?:www\.)?hbo\.com/video/video\.html\?.*vid=(?P<id>[0-9]+)'
116     _TEST = {
117         'url': 'http://www.hbo.com/video/video.html?autoplay=true&g=u&vid=1437839',
118         'md5': '1c33253f0c7782142c993c0ba62a8753',
119         'info_dict': {
120             'id': '1437839',
121             'ext': 'mp4',
122             'title': 'Ep. 64 Clip: Encryption',
123             'thumbnail': r're:https?://.*\.jpg$',
124             'duration': 1072,
125         }
126     }
127
128     def _real_extract(self, url):
129         video_id = self._match_id(url)
130         return self._extract_from_id(video_id)
131
132
133 class HBOEpisodeIE(HBOBaseIE):
134     _VALID_URL = r'https?://(?:www\.)?hbo\.com/(?!video)([^/]+/)+video/(?P<id>[0-9a-z-]+)\.html'
135
136     _TESTS = [{
137         'url': 'http://www.hbo.com/girls/episodes/5/52-i-love-you-baby/video/ep-52-inside-the-episode.html?autoplay=true',
138         'md5': '689132b253cc0ab7434237fc3a293210',
139         'info_dict': {
140             'id': '1439518',
141             'display_id': 'ep-52-inside-the-episode',
142             'ext': 'mp4',
143             'title': 'Ep. 52: Inside the Episode',
144             'thumbnail': r're:https?://.*\.jpg$',
145             'duration': 240,
146         },
147     }, {
148         'url': 'http://www.hbo.com/game-of-thrones/about/video/season-5-invitation-to-the-set.html?autoplay=true',
149         'only_matching': True,
150     }]
151
152     def _real_extract(self, url):
153         display_id = self._match_id(url)
154
155         webpage = self._download_webpage(url, display_id)
156
157         video_id = self._search_regex(
158             r'(?P<q1>[\'"])videoId(?P=q1)\s*:\s*(?P<q2>[\'"])(?P<video_id>\d+)(?P=q2)',
159             webpage, 'video ID', group='video_id')
160
161         info_dict = self._extract_from_id(video_id)
162         info_dict['display_id'] = display_id
163
164         return info_dict