[xtube] Fix extraction (Closes #8565)
[youtube-dl] / youtube_dl / extractor / xtube.py
1 from __future__ import unicode_literals
2
3 import itertools
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_urllib_parse_unquote
8 from ..utils import (
9     int_or_none,
10     sanitized_Request,
11     str_to_int,
12 )
13
14
15 class XTubeIE(InfoExtractor):
16     _VALID_URL = r'(?:xtube:|https?://(?:www\.)?xtube\.com/(?:watch\.php\?.*\bv=|video-watch/(?P<display_id>[^/]+)-))(?P<id>[^/?&#]+)'
17
18     _TESTS = [{
19         # old URL schema
20         'url': 'http://www.xtube.com/watch.php?v=kVTUy_G222_',
21         'md5': '092fbdd3cbe292c920ef6fc6a8a9cdab',
22         'info_dict': {
23             'id': 'kVTUy_G222_',
24             'ext': 'mp4',
25             'title': 'strange erotica',
26             'description': 'contains:an ET kind of thing',
27             'uploader': 'greenshowers',
28             'duration': 450,
29             'age_limit': 18,
30         }
31     }, {
32         # new URL schema
33         'url': 'http://www.xtube.com/video-watch/strange-erotica-625837',
34         'only_matching': True,
35     }, {
36         'url': 'xtube:625837',
37         'only_matching': True,
38     }]
39
40     def _real_extract(self, url):
41         mobj = re.match(self._VALID_URL, url)
42         video_id = mobj.group('id')
43         display_id = mobj.group('display_id')
44
45         if not display_id:
46             display_id = video_id
47             url = 'http://www.xtube.com/watch.php?v=%s' % video_id
48
49         req = sanitized_Request(url)
50         req.add_header('Cookie', 'age_verified=1; cookiesAccepted=1')
51         webpage = self._download_webpage(req, display_id)
52
53         flashvars = self._parse_json(
54             self._search_regex(
55                 r'xt\.playerOps\s*=\s*({.+?});', webpage, 'player ops'),
56             video_id)['flashvars']
57
58         title = flashvars.get('title') or self._search_regex(
59             r'<h1>([^<]+)</h1>', webpage, 'title')
60         video_url = compat_urllib_parse_unquote(flashvars['video_url'])
61         duration = int_or_none(flashvars.get('video_duration'))
62
63         uploader = self._search_regex(
64             r'<input[^>]+name="contentOwnerId"[^>]+value="([^"]+)"',
65             webpage, 'uploader', fatal=False)
66         description = self._search_regex(
67             r'</h1>\s*<p>([^<]+)', webpage, 'description', fatal=False)
68         view_count = str_to_int(self._search_regex(
69             r'<dt>Views:</dt>\s*<dd>([\d,\.]+)</dd>',
70             webpage, 'view count', fatal=False))
71         comment_count = str_to_int(self._html_search_regex(
72             r'>Comments? \(([\d,\.]+)\)<',
73             webpage, 'comment count', fatal=False))
74
75         return {
76             'id': video_id,
77             'display_id': display_id,
78             'url': video_url,
79             'title': title,
80             'description': description,
81             'uploader': uploader,
82             'duration': duration,
83             'view_count': view_count,
84             'comment_count': comment_count,
85             'age_limit': 18,
86         }
87
88
89 class XTubeUserIE(InfoExtractor):
90     IE_DESC = 'XTube user profile'
91     _VALID_URL = r'https?://(?:www\.)?xtube\.com/profile/(?P<id>[^/]+-\d+)'
92     _TEST = {
93         'url': 'http://www.xtube.com/profile/greenshowers-4056496',
94         'info_dict': {
95             'id': 'greenshowers-4056496',
96             'age_limit': 18,
97         },
98         'playlist_mincount': 155,
99     }
100
101     def _real_extract(self, url):
102         user_id = self._match_id(url)
103
104         entries = []
105         for pagenum in itertools.count(1):
106             request = sanitized_Request(
107                 'http://www.xtube.com/profile/%s/videos/%d' % (user_id, pagenum),
108                 headers={
109                     'Cookie': 'popunder=4',
110                     'X-Requested-With': 'XMLHttpRequest',
111                     'Referer': url,
112                 })
113
114             page = self._download_json(
115                 request, user_id, 'Downloading videos JSON page %d' % pagenum)
116
117             html = page.get('html')
118             if not html:
119                 break
120
121             for _, video_id in re.findall(r'data-plid=(["\'])(.+?)\1', html):
122                 entries.append(self.url_result('xtube:%s' % video_id, XTubeIE.ie_key()))
123
124             page_count = int_or_none(page.get('pageCount'))
125             if not page_count or pagenum == page_count:
126                 break
127
128         playlist = self.playlist_result(entries, user_id)
129         playlist['age_limit'] = 18
130         return playlist