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