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