]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/xtube.py
Update to ytdl-commit-a726009
[yt-dlp.git] / yt_dlp / 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 js_to_json,
10 orderedSet,
11 parse_duration,
12 sanitized_Request,
13 str_to_int,
14 url_or_none,
15 )
16
17
18 class XTubeIE(InfoExtractor):
19 _VALID_URL = r'''(?x)
20 (?:
21 xtube:|
22 https?://(?:www\.)?xtube\.com/(?:watch\.php\?.*\bv=|video-watch/(?:embedded/)?(?P<display_id>[^/]+)-)
23 )
24 (?P<id>[^/?&#]+)
25 '''
26
27 _TESTS = [{
28 # old URL schema
29 'url': 'http://www.xtube.com/watch.php?v=kVTUy_G222_',
30 'md5': '092fbdd3cbe292c920ef6fc6a8a9cdab',
31 'info_dict': {
32 'id': 'kVTUy_G222_',
33 'ext': 'mp4',
34 'title': 'strange erotica',
35 'description': 'contains:an ET kind of thing',
36 'uploader': 'greenshowers',
37 'duration': 450,
38 'view_count': int,
39 'comment_count': int,
40 'age_limit': 18,
41 }
42 }, {
43 # new URL schema
44 'url': 'http://www.xtube.com/video-watch/strange-erotica-625837',
45 'only_matching': True,
46 }, {
47 'url': 'xtube:625837',
48 'only_matching': True,
49 }, {
50 'url': 'xtube:kVTUy_G222_',
51 'only_matching': True,
52 }, {
53 'url': 'https://www.xtube.com/video-watch/embedded/milf-tara-and-teen-shared-and-cum-covered-extreme-bukkake-32203482?embedsize=big',
54 'only_matching': True,
55 }]
56
57 def _real_extract(self, url):
58 mobj = re.match(self._VALID_URL, url)
59 video_id = mobj.group('id')
60 display_id = mobj.group('display_id')
61
62 if not display_id:
63 display_id = video_id
64
65 if video_id.isdigit() and len(video_id) < 11:
66 url_pattern = 'http://www.xtube.com/video-watch/-%s'
67 else:
68 url_pattern = 'http://www.xtube.com/watch.php?v=%s'
69
70 webpage = self._download_webpage(
71 url_pattern % video_id, display_id, headers={
72 'Cookie': 'age_verified=1; cookiesAccepted=1',
73 })
74
75 title, thumbnail, duration, sources, media_definition = [None] * 5
76
77 config = self._parse_json(self._search_regex(
78 r'playerConf\s*=\s*({.+?})\s*,\s*(?:\n|loaderConf|playerWrapper)', webpage, 'config',
79 default='{}'), video_id, transform_source=js_to_json, fatal=False)
80 if config:
81 config = config.get('mainRoll')
82 if isinstance(config, dict):
83 title = config.get('title')
84 thumbnail = config.get('poster')
85 duration = int_or_none(config.get('duration'))
86 sources = config.get('sources') or config.get('format')
87 media_definition = config.get('mediaDefinition')
88
89 if not isinstance(sources, dict) and not media_definition:
90 sources = self._parse_json(self._search_regex(
91 r'(["\'])?sources\1?\s*:\s*(?P<sources>{.+?}),',
92 webpage, 'sources', group='sources'), video_id,
93 transform_source=js_to_json)
94
95 formats = []
96 format_urls = set()
97
98 if isinstance(sources, dict):
99 for format_id, format_url in sources.items():
100 format_url = url_or_none(format_url)
101 if not format_url:
102 continue
103 if format_url in format_urls:
104 continue
105 format_urls.add(format_url)
106 formats.append({
107 'url': format_url,
108 'format_id': format_id,
109 'height': int_or_none(format_id),
110 })
111
112 if isinstance(media_definition, list):
113 for media in media_definition:
114 video_url = url_or_none(media.get('videoUrl'))
115 if not video_url:
116 continue
117 if video_url in format_urls:
118 continue
119 format_urls.add(video_url)
120 format_id = media.get('format')
121 if format_id == 'hls':
122 formats.extend(self._extract_m3u8_formats(
123 video_url, video_id, 'mp4', entry_protocol='m3u8_native',
124 m3u8_id='hls', fatal=False))
125 elif format_id == 'mp4':
126 height = int_or_none(media.get('quality'))
127 formats.append({
128 'url': video_url,
129 'format_id': '%s-%d' % (format_id, height) if height else format_id,
130 'height': height,
131 })
132
133 self._remove_duplicate_formats(formats)
134 self._sort_formats(formats)
135
136 if not title:
137 title = self._search_regex(
138 (r'<h1>\s*(?P<title>[^<]+?)\s*</h1>', r'videoTitle\s*:\s*(["\'])(?P<title>.+?)\1'),
139 webpage, 'title', group='title')
140 description = self._og_search_description(
141 webpage, default=None) or self._html_search_meta(
142 'twitter:description', webpage, default=None) or self._search_regex(
143 r'</h1>\s*<p>([^<]+)', webpage, 'description', fatal=False)
144 uploader = self._search_regex(
145 (r'<input[^>]+name="contentOwnerId"[^>]+value="([^"]+)"',
146 r'<span[^>]+class="nickname"[^>]*>([^<]+)'),
147 webpage, 'uploader', fatal=False)
148 if not duration:
149 duration = parse_duration(self._search_regex(
150 r'<dt>Runtime:?</dt>\s*<dd>([^<]+)</dd>',
151 webpage, 'duration', fatal=False))
152 view_count = str_to_int(self._search_regex(
153 (r'["\']viewsCount["\'][^>]*>(\d+)\s+views',
154 r'<dt>Views:?</dt>\s*<dd>([\d,\.]+)</dd>'),
155 webpage, 'view count', fatal=False))
156 comment_count = str_to_int(self._html_search_regex(
157 r'>Comments? \(([\d,\.]+)\)<',
158 webpage, 'comment count', fatal=False))
159
160 return {
161 'id': video_id,
162 'display_id': display_id,
163 'title': title,
164 'description': description,
165 'thumbnail': thumbnail,
166 'uploader': uploader,
167 'duration': duration,
168 'view_count': view_count,
169 'comment_count': comment_count,
170 'age_limit': 18,
171 'formats': formats,
172 }
173
174
175 class XTubeUserIE(InfoExtractor):
176 IE_DESC = 'XTube user profile'
177 _VALID_URL = r'https?://(?:www\.)?xtube\.com/profile/(?P<id>[^/]+-\d+)'
178 _TEST = {
179 'url': 'http://www.xtube.com/profile/greenshowers-4056496',
180 'info_dict': {
181 'id': 'greenshowers-4056496',
182 'age_limit': 18,
183 },
184 'playlist_mincount': 154,
185 }
186
187 def _real_extract(self, url):
188 user_id = self._match_id(url)
189
190 entries = []
191 for pagenum in itertools.count(1):
192 request = sanitized_Request(
193 'http://www.xtube.com/profile/%s/videos/%d' % (user_id, pagenum),
194 headers={
195 'Cookie': 'popunder=4',
196 'X-Requested-With': 'XMLHttpRequest',
197 'Referer': url,
198 })
199
200 page = self._download_json(
201 request, user_id, 'Downloading videos JSON page %d' % pagenum)
202
203 html = page.get('html')
204 if not html:
205 break
206
207 for video_id in orderedSet([video_id for _, video_id in re.findall(
208 r'data-plid=(["\'])(.+?)\1', html)]):
209 entries.append(self.url_result('xtube:%s' % video_id, XTubeIE.ie_key()))
210
211 page_count = int_or_none(page.get('pageCount'))
212 if not page_count or pagenum == page_count:
213 break
214
215 playlist = self.playlist_result(entries, user_id)
216 playlist['age_limit'] = 18
217 return playlist