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