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