[youtube] fix extraction for embed restricted live streams(fixes #16433)
[youtube-dl] / youtube_dl / extractor / tnaflix.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import compat_str
7 from ..utils import (
8     fix_xml_ampersands,
9     float_or_none,
10     int_or_none,
11     parse_duration,
12     str_to_int,
13     unescapeHTML,
14     xpath_text,
15 )
16
17
18 class TNAFlixNetworkBaseIE(InfoExtractor):
19     # May be overridden in descendants if necessary
20     _CONFIG_REGEX = [
21         r'flashvars\.config\s*=\s*escape\("([^"]+)"',
22         r'<input[^>]+name="config\d?" value="([^"]+)"',
23     ]
24     _HOST = 'tna'
25     _VKEY_SUFFIX = ''
26     _TITLE_REGEX = r'<input[^>]+name="title" value="([^"]+)"'
27     _DESCRIPTION_REGEX = r'<input[^>]+name="description" value="([^"]+)"'
28     _UPLOADER_REGEX = r'<input[^>]+name="username" value="([^"]+)"'
29     _VIEW_COUNT_REGEX = None
30     _COMMENT_COUNT_REGEX = None
31     _AVERAGE_RATING_REGEX = None
32     _CATEGORIES_REGEX = r'<li[^>]*>\s*<span[^>]+class="infoTitle"[^>]*>Categories:</span>\s*<span[^>]+class="listView"[^>]*>(.+?)</span>\s*</li>'
33
34     def _extract_thumbnails(self, flix_xml):
35
36         def get_child(elem, names):
37             for name in names:
38                 child = elem.find(name)
39                 if child is not None:
40                     return child
41
42         timeline = get_child(flix_xml, ['timeline', 'rolloverBarImage'])
43         if timeline is None:
44             return
45
46         pattern_el = get_child(timeline, ['imagePattern', 'pattern'])
47         if pattern_el is None or not pattern_el.text:
48             return
49
50         first_el = get_child(timeline, ['imageFirst', 'first'])
51         last_el = get_child(timeline, ['imageLast', 'last'])
52         if first_el is None or last_el is None:
53             return
54
55         first_text = first_el.text
56         last_text = last_el.text
57         if not first_text.isdigit() or not last_text.isdigit():
58             return
59
60         first = int(first_text)
61         last = int(last_text)
62         if first > last:
63             return
64
65         width = int_or_none(xpath_text(timeline, './imageWidth', 'thumbnail width'))
66         height = int_or_none(xpath_text(timeline, './imageHeight', 'thumbnail height'))
67
68         return [{
69             'url': self._proto_relative_url(pattern_el.text.replace('#', compat_str(i)), 'http:'),
70             'width': width,
71             'height': height,
72         } for i in range(first, last + 1)]
73
74     def _real_extract(self, url):
75         mobj = re.match(self._VALID_URL, url)
76         video_id = mobj.group('id')
77         for display_id_key in ('display_id', 'display_id_2'):
78             if display_id_key in mobj.groupdict():
79                 display_id = mobj.group(display_id_key)
80                 if display_id:
81                     break
82         else:
83             display_id = video_id
84
85         webpage = self._download_webpage(url, display_id)
86
87         cfg_url = self._proto_relative_url(self._html_search_regex(
88             self._CONFIG_REGEX, webpage, 'flashvars.config', default=None), 'http:')
89
90         if not cfg_url:
91             inputs = self._hidden_inputs(webpage)
92             cfg_url = ('https://cdn-fck.%sflix.com/%sflix/%s%s.fid?key=%s&VID=%s&premium=1&vip=1&alpha'
93                        % (self._HOST, self._HOST, inputs['vkey'], self._VKEY_SUFFIX, inputs['nkey'], video_id))
94
95         cfg_xml = self._download_xml(
96             cfg_url, display_id, 'Downloading metadata',
97             transform_source=fix_xml_ampersands)
98
99         formats = []
100
101         def extract_video_url(vl):
102             # Any URL modification now results in HTTP Error 403: Forbidden
103             return unescapeHTML(vl.text)
104
105         video_link = cfg_xml.find('./videoLink')
106         if video_link is not None:
107             formats.append({
108                 'url': extract_video_url(video_link),
109                 'ext': xpath_text(cfg_xml, './videoConfig/type', 'type', default='flv'),
110             })
111
112         for item in cfg_xml.findall('./quality/item'):
113             video_link = item.find('./videoLink')
114             if video_link is None:
115                 continue
116             res = item.find('res')
117             format_id = None if res is None else res.text
118             height = int_or_none(self._search_regex(
119                 r'^(\d+)[pP]', format_id, 'height', default=None))
120             formats.append({
121                 'url': self._proto_relative_url(extract_video_url(video_link), 'http:'),
122                 'format_id': format_id,
123                 'height': height,
124             })
125
126         self._sort_formats(formats)
127
128         thumbnail = self._proto_relative_url(
129             xpath_text(cfg_xml, './startThumb', 'thumbnail'), 'http:')
130         thumbnails = self._extract_thumbnails(cfg_xml)
131
132         title = None
133         if self._TITLE_REGEX:
134             title = self._html_search_regex(
135                 self._TITLE_REGEX, webpage, 'title', default=None)
136         if not title:
137             title = self._og_search_title(webpage)
138
139         age_limit = self._rta_search(webpage) or 18
140
141         duration = parse_duration(self._html_search_meta(
142             'duration', webpage, 'duration', default=None))
143
144         def extract_field(pattern, name):
145             return self._html_search_regex(pattern, webpage, name, default=None) if pattern else None
146
147         description = extract_field(self._DESCRIPTION_REGEX, 'description')
148         uploader = extract_field(self._UPLOADER_REGEX, 'uploader')
149         view_count = str_to_int(extract_field(self._VIEW_COUNT_REGEX, 'view count'))
150         comment_count = str_to_int(extract_field(self._COMMENT_COUNT_REGEX, 'comment count'))
151         average_rating = float_or_none(extract_field(self._AVERAGE_RATING_REGEX, 'average rating'))
152
153         categories_str = extract_field(self._CATEGORIES_REGEX, 'categories')
154         categories = [c.strip() for c in categories_str.split(',')] if categories_str is not None else []
155
156         return {
157             'id': video_id,
158             'display_id': display_id,
159             'title': title,
160             'description': description,
161             'thumbnail': thumbnail,
162             'thumbnails': thumbnails,
163             'duration': duration,
164             'age_limit': age_limit,
165             'uploader': uploader,
166             'view_count': view_count,
167             'comment_count': comment_count,
168             'average_rating': average_rating,
169             'categories': categories,
170             'formats': formats,
171         }
172
173
174 class TNAFlixNetworkEmbedIE(TNAFlixNetworkBaseIE):
175     _VALID_URL = r'https?://player\.(?:tna|emp)flix\.com/video/(?P<id>\d+)'
176
177     _TITLE_REGEX = r'<title>([^<]+)</title>'
178
179     _TESTS = [{
180         'url': 'https://player.tnaflix.com/video/6538',
181         'info_dict': {
182             'id': '6538',
183             'display_id': '6538',
184             'ext': 'mp4',
185             'title': 'Educational xxx video',
186             'thumbnail': r're:https?://.*\.jpg$',
187             'age_limit': 18,
188         },
189         'params': {
190             'skip_download': True,
191         },
192     }, {
193         'url': 'https://player.empflix.com/video/33051',
194         'only_matching': True,
195     }]
196
197     @staticmethod
198     def _extract_urls(webpage):
199         return [url for _, url in re.findall(
200             r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//player\.(?:tna|emp)flix\.com/video/\d+)\1',
201             webpage)]
202
203
204 class TNAEMPFlixBaseIE(TNAFlixNetworkBaseIE):
205     _DESCRIPTION_REGEX = r'(?s)>Description:</[^>]+>(.+?)<'
206     _UPLOADER_REGEX = r'<span>by\s*<a[^>]+\bhref=["\']/profile/[^>]+>([^<]+)<'
207     _CATEGORIES_REGEX = r'(?s)<span[^>]*>Categories:</span>(.+?)</div>'
208
209
210 class TNAFlixIE(TNAEMPFlixBaseIE):
211     _VALID_URL = r'https?://(?:www\.)?tnaflix\.com/[^/]+/(?P<display_id>[^/]+)/video(?P<id>\d+)'
212
213     _TITLE_REGEX = r'<title>(.+?) - (?:TNAFlix Porn Videos|TNAFlix\.com)</title>'
214
215     _TESTS = [{
216         # anonymous uploader, no categories
217         'url': 'http://www.tnaflix.com/porn-stars/Carmella-Decesare-striptease/video553878',
218         'md5': '7e569419fe6d69543d01e6be22f5f7c4',
219         'info_dict': {
220             'id': '553878',
221             'display_id': 'Carmella-Decesare-striptease',
222             'ext': 'mp4',
223             'title': 'Carmella Decesare - striptease',
224             'thumbnail': r're:https?://.*\.jpg$',
225             'duration': 91,
226             'age_limit': 18,
227             'categories': ['Porn Stars'],
228         }
229     }, {
230         # non-anonymous uploader, categories
231         'url': 'https://www.tnaflix.com/teen-porn/Educational-xxx-video/video6538',
232         'md5': '0f5d4d490dbfd117b8607054248a07c0',
233         'info_dict': {
234             'id': '6538',
235             'display_id': 'Educational-xxx-video',
236             'ext': 'mp4',
237             'title': 'Educational xxx video',
238             'description': 'md5:b4fab8f88a8621c8fabd361a173fe5b8',
239             'thumbnail': r're:https?://.*\.jpg$',
240             'duration': 164,
241             'age_limit': 18,
242             'uploader': 'bobwhite39',
243             'categories': list,
244         }
245     }, {
246         'url': 'https://www.tnaflix.com/amateur-porn/bunzHD-Ms.Donk/video358632',
247         'only_matching': True,
248     }]
249
250
251 class EMPFlixIE(TNAEMPFlixBaseIE):
252     _VALID_URL = r'https?://(?:www\.)?empflix\.com/(?:videos/(?P<display_id>.+?)-|[^/]+/(?P<display_id_2>[^/]+)/video)(?P<id>[0-9]+)'
253
254     _HOST = 'emp'
255     _VKEY_SUFFIX = '-1'
256
257     _TESTS = [{
258         'url': 'http://www.empflix.com/videos/Amateur-Finger-Fuck-33051.html',
259         'md5': 'bc30d48b91a7179448a0bda465114676',
260         'info_dict': {
261             'id': '33051',
262             'display_id': 'Amateur-Finger-Fuck',
263             'ext': 'mp4',
264             'title': 'Amateur Finger Fuck',
265             'description': 'Amateur solo finger fucking.',
266             'thumbnail': r're:https?://.*\.jpg$',
267             'duration': 83,
268             'age_limit': 18,
269             'uploader': 'cwbike',
270             'categories': ['Amateur', 'Anal', 'Fisting', 'Home made', 'Solo'],
271         }
272     }, {
273         'url': 'http://www.empflix.com/videos/[AROMA][ARMD-718]-Aoi-Yoshino-Sawa-25826.html',
274         'only_matching': True,
275     }, {
276         'url': 'https://www.empflix.com/amateur-porn/Amateur-Finger-Fuck/video33051',
277         'only_matching': True,
278     }]
279
280
281 class MovieFapIE(TNAFlixNetworkBaseIE):
282     _VALID_URL = r'https?://(?:www\.)?moviefap\.com/videos/(?P<id>[0-9a-f]+)/(?P<display_id>[^/]+)\.html'
283
284     _VIEW_COUNT_REGEX = r'<br>Views\s*<strong>([\d,.]+)</strong>'
285     _COMMENT_COUNT_REGEX = r'<span[^>]+id="comCount"[^>]*>([\d,.]+)</span>'
286     _AVERAGE_RATING_REGEX = r'Current Rating\s*<br>\s*<strong>([\d.]+)</strong>'
287     _CATEGORIES_REGEX = r'(?s)<div[^>]+id="vid_info"[^>]*>\s*<div[^>]*>.+?</div>(.*?)<br>'
288
289     _TESTS = [{
290         # normal, multi-format video
291         'url': 'http://www.moviefap.com/videos/be9867c9416c19f54a4a/experienced-milf-amazing-handjob.html',
292         'md5': '26624b4e2523051b550067d547615906',
293         'info_dict': {
294             'id': 'be9867c9416c19f54a4a',
295             'display_id': 'experienced-milf-amazing-handjob',
296             'ext': 'mp4',
297             'title': 'Experienced MILF Amazing Handjob',
298             'description': 'Experienced MILF giving an Amazing Handjob',
299             'thumbnail': r're:https?://.*\.jpg$',
300             'age_limit': 18,
301             'uploader': 'darvinfred06',
302             'view_count': int,
303             'comment_count': int,
304             'average_rating': float,
305             'categories': ['Amateur', 'Masturbation', 'Mature', 'Flashing'],
306         }
307     }, {
308         # quirky single-format case where the extension is given as fid, but the video is really an flv
309         'url': 'http://www.moviefap.com/videos/e5da0d3edce5404418f5/jeune-couple-russe.html',
310         'md5': 'fa56683e291fc80635907168a743c9ad',
311         'info_dict': {
312             'id': 'e5da0d3edce5404418f5',
313             'display_id': 'jeune-couple-russe',
314             'ext': 'flv',
315             'title': 'Jeune Couple Russe',
316             'description': 'Amateur',
317             'thumbnail': r're:https?://.*\.jpg$',
318             'age_limit': 18,
319             'uploader': 'whiskeyjar',
320             'view_count': int,
321             'comment_count': int,
322             'average_rating': float,
323             'categories': ['Amateur', 'Teen'],
324         }
325     }]