[porntube] Fix extraction (closes #17541)
[youtube-dl] / youtube_dl / extractor / fourtube.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import (
7     compat_b64decode,
8     compat_str,
9     compat_urllib_parse_unquote,
10     compat_urlparse,
11 )
12 from ..utils import (
13     int_or_none,
14     parse_duration,
15     parse_iso8601,
16     str_to_int,
17     try_get,
18     unified_timestamp,
19     url_or_none,
20 )
21
22
23 class FourTubeBaseIE(InfoExtractor):
24     _TKN_HOST = 'tkn.kodicdn.com'
25
26     def _extract_formats(self, url, video_id, media_id, sources):
27         token_url = 'https://%s/%s/desktop/%s' % (
28             self._TKN_HOST, media_id, '+'.join(sources))
29
30         parsed_url = compat_urlparse.urlparse(url)
31         tokens = self._download_json(token_url, video_id, data=b'', headers={
32             'Origin': '%s://%s' % (parsed_url.scheme, parsed_url.hostname),
33             'Referer': url,
34         })
35         formats = [{
36             'url': tokens[format]['token'],
37             'format_id': format + 'p',
38             'resolution': format + 'p',
39             'quality': int(format),
40         } for format in sources]
41         self._sort_formats(formats)
42         return formats
43
44     def _real_extract(self, url):
45         mobj = re.match(self._VALID_URL, url)
46         kind, video_id, display_id = mobj.group('kind', 'id', 'display_id')
47
48         if kind == 'm' or not display_id:
49             url = self._URL_TEMPLATE % video_id
50
51         webpage = self._download_webpage(url, video_id)
52
53         title = self._html_search_meta('name', webpage)
54         timestamp = parse_iso8601(self._html_search_meta(
55             'uploadDate', webpage))
56         thumbnail = self._html_search_meta('thumbnailUrl', webpage)
57         uploader_id = self._html_search_regex(
58             r'<a class="item-to-subscribe" href="[^"]+/(?:channel|user)s?/([^/"]+)" title="Go to [^"]+ page">',
59             webpage, 'uploader id', fatal=False)
60         uploader = self._html_search_regex(
61             r'<a class="item-to-subscribe" href="[^"]+/(?:channel|user)s?/[^/"]+" title="Go to ([^"]+) page">',
62             webpage, 'uploader', fatal=False)
63
64         categories_html = self._search_regex(
65             r'(?s)><i class="icon icon-tag"></i>\s*Categories / Tags\s*.*?<ul class="[^"]*?list[^"]*?">(.*?)</ul>',
66             webpage, 'categories', fatal=False)
67         categories = None
68         if categories_html:
69             categories = [
70                 c.strip() for c in re.findall(
71                     r'(?s)<li><a.*?>(.*?)</a>', categories_html)]
72
73         view_count = str_to_int(self._search_regex(
74             r'<meta[^>]+itemprop="interactionCount"[^>]+content="UserPlays:([0-9,]+)">',
75             webpage, 'view count', default=None))
76         like_count = str_to_int(self._search_regex(
77             r'<meta[^>]+itemprop="interactionCount"[^>]+content="UserLikes:([0-9,]+)">',
78             webpage, 'like count', default=None))
79         duration = parse_duration(self._html_search_meta('duration', webpage))
80
81         media_id = self._search_regex(
82             r'<button[^>]+data-id=(["\'])(?P<id>\d+)\1[^>]+data-quality=', webpage,
83             'media id', default=None, group='id')
84         sources = [
85             quality
86             for _, quality in re.findall(r'<button[^>]+data-quality=(["\'])(.+?)\1', webpage)]
87         if not (media_id and sources):
88             player_js = self._download_webpage(
89                 self._search_regex(
90                     r'<script[^>]id=(["\'])playerembed\1[^>]+src=(["\'])(?P<url>.+?)\2',
91                     webpage, 'player JS', group='url'),
92                 video_id, 'Downloading player JS')
93             params_js = self._search_regex(
94                 r'\$\.ajax\(url,\ opts\);\s*\}\s*\}\)\(([0-9,\[\] ]+)\)',
95                 player_js, 'initialization parameters')
96             params = self._parse_json('[%s]' % params_js, video_id)
97             media_id = params[0]
98             sources = ['%s' % p for p in params[2]]
99
100         formats = self._extract_formats(url, video_id, media_id, sources)
101
102         return {
103             'id': video_id,
104             'title': title,
105             'formats': formats,
106             'categories': categories,
107             'thumbnail': thumbnail,
108             'uploader': uploader,
109             'uploader_id': uploader_id,
110             'timestamp': timestamp,
111             'like_count': like_count,
112             'view_count': view_count,
113             'duration': duration,
114             'age_limit': 18,
115         }
116
117
118 class FourTubeIE(FourTubeBaseIE):
119     IE_NAME = '4tube'
120     _VALID_URL = r'https?://(?:(?P<kind>www|m)\.)?4tube\.com/(?:videos|embed)/(?P<id>\d+)(?:/(?P<display_id>[^/?#&]+))?'
121     _URL_TEMPLATE = 'https://www.4tube.com/videos/%s/video'
122     _TESTS = [{
123         'url': 'http://www.4tube.com/videos/209733/hot-babe-holly-michaels-gets-her-ass-stuffed-by-black',
124         'md5': '6516c8ac63b03de06bc8eac14362db4f',
125         'info_dict': {
126             'id': '209733',
127             'ext': 'mp4',
128             'title': 'Hot Babe Holly Michaels gets her ass stuffed by black',
129             'uploader': 'WCP Club',
130             'uploader_id': 'wcp-club',
131             'upload_date': '20131031',
132             'timestamp': 1383263892,
133             'duration': 583,
134             'view_count': int,
135             'like_count': int,
136             'categories': list,
137             'age_limit': 18,
138         },
139     }, {
140         'url': 'http://www.4tube.com/embed/209733',
141         'only_matching': True,
142     }, {
143         'url': 'http://m.4tube.com/videos/209733/hot-babe-holly-michaels-gets-her-ass-stuffed-by-black',
144         'only_matching': True,
145     }]
146
147
148 class FuxIE(FourTubeBaseIE):
149     _VALID_URL = r'https?://(?:(?P<kind>www|m)\.)?fux\.com/(?:video|embed)/(?P<id>\d+)(?:/(?P<display_id>[^/?#&]+))?'
150     _URL_TEMPLATE = 'https://www.fux.com/video/%s/video'
151     _TESTS = [{
152         'url': 'https://www.fux.com/video/195359/awesome-fucking-kitchen-ends-cum-swallow',
153         'info_dict': {
154             'id': '195359',
155             'ext': 'mp4',
156             'title': 'Awesome fucking in the kitchen ends with cum swallow',
157             'uploader': 'alenci2342',
158             'uploader_id': 'alenci2342',
159             'upload_date': '20131230',
160             'timestamp': 1388361660,
161             'duration': 289,
162             'view_count': int,
163             'like_count': int,
164             'categories': list,
165             'age_limit': 18,
166         },
167         'params': {
168             'skip_download': True,
169         },
170     }, {
171         'url': 'https://www.fux.com/embed/195359',
172         'only_matching': True,
173     }, {
174         'url': 'https://www.fux.com/video/195359/awesome-fucking-kitchen-ends-cum-swallow',
175         'only_matching': True,
176     }]
177
178
179 class PornTubeIE(FourTubeBaseIE):
180     _VALID_URL = r'https?://(?:(?P<kind>www|m)\.)?porntube\.com/(?:videos/(?P<display_id>[^/]+)_|embed/)(?P<id>\d+)'
181     _URL_TEMPLATE = 'https://www.porntube.com/videos/video_%s'
182     _TKN_HOST = 'tkn.porntube.com'
183     _TESTS = [{
184         'url': 'https://www.porntube.com/videos/teen-couple-doing-anal_7089759',
185         'info_dict': {
186             'id': '7089759',
187             'ext': 'mp4',
188             'title': 'Teen couple doing anal',
189             'uploader': 'Alexy',
190             'uploader_id': '91488',
191             'upload_date': '20150606',
192             'timestamp': 1433595647,
193             'duration': 5052,
194             'view_count': int,
195             'like_count': int,
196             'age_limit': 18,
197         },
198         'params': {
199             'skip_download': True,
200         },
201     }, {
202         'url': 'https://www.porntube.com/embed/7089759',
203         'only_matching': True,
204     }, {
205         'url': 'https://m.porntube.com/videos/teen-couple-doing-anal_7089759',
206         'only_matching': True,
207     }]
208
209     def _real_extract(self, url):
210         mobj = re.match(self._VALID_URL, url)
211         video_id, display_id = mobj.group('id', 'display_id')
212
213         webpage = self._download_webpage(url, display_id)
214
215         video = self._parse_json(
216             self._search_regex(
217                 r'INITIALSTATE\s*=\s*(["\'])(?P<value>(?:(?!\1).)+)\1',
218                 webpage, 'data', group='value'), video_id,
219             transform_source=lambda x: compat_urllib_parse_unquote(
220                 compat_b64decode(x).decode('utf-8')))['page']['video']
221
222         title = video['title']
223         media_id = video['mediaId']
224         sources = [compat_str(e['height'])
225                    for e in video['encodings'] if e.get('height')]
226         formats = self._extract_formats(url, video_id, media_id, sources)
227
228         thumbnail = url_or_none(video.get('masterThumb'))
229         uploader = try_get(video, lambda x: x['user']['username'], compat_str)
230         uploader_id = compat_str(try_get(video, lambda x: x['user']['id'], int))
231         like_count = int_or_none(video.get('likes'))
232         dislike_count = int_or_none(video.get('dislikes'))
233         view_count = int_or_none(video.get('playsQty'))
234         duration = int_or_none(video.get('durationInSeconds'))
235         timestamp = unified_timestamp(video.get('publishedAt'))
236
237         return {
238             'id': video_id,
239             'title': title,
240             'formats': formats,
241             'thumbnail': thumbnail,
242             'uploader': uploader,
243             'uploader_id': uploader_id,
244             'timestamp': timestamp,
245             'like_count': like_count,
246             'dislike_count': dislike_count,
247             'view_count': view_count,
248             'duration': duration,
249             'age_limit': 18,
250         }
251
252
253 class PornerBrosIE(FourTubeBaseIE):
254     _VALID_URL = r'https?://(?:(?P<kind>www|m)\.)?pornerbros\.com/(?:videos/(?P<display_id>[^/]+)_|embed/)(?P<id>\d+)'
255     _URL_TEMPLATE = 'https://www.pornerbros.com/videos/video_%s'
256     _TESTS = [{
257         'url': 'https://www.pornerbros.com/videos/skinny-brunette-takes-big-cock-down-her-anal-hole_181369',
258         'md5': '6516c8ac63b03de06bc8eac14362db4f',
259         'info_dict': {
260             'id': '181369',
261             'ext': 'mp4',
262             'title': 'Skinny brunette takes big cock down her anal hole',
263             'uploader': 'PornerBros HD',
264             'uploader_id': 'pornerbros-hd',
265             'upload_date': '20130130',
266             'timestamp': 1359527401,
267             'duration': 1224,
268             'view_count': int,
269             'categories': list,
270             'age_limit': 18,
271         },
272         'params': {
273             'skip_download': True,
274         },
275     }, {
276         'url': 'https://www.pornerbros.com/embed/181369',
277         'only_matching': True,
278     }, {
279         'url': 'https://m.pornerbros.com/videos/skinny-brunette-takes-big-cock-down-her-anal-hole_181369',
280         'only_matching': True,
281     }]