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