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