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