]> jfr.im git - yt-dlp.git/blob - youtube_dlc/extractor/xtube.py
Merge pull request #68 from peet1993/master
[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 ExtractorError,
9 int_or_none,
10 js_to_json,
11 orderedSet,
12 parse_duration,
13 sanitized_Request,
14 str_to_int,
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': 449,
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 = [None] * 3
76
77 json_config_string = self._search_regex(
78 r'playerConf=({.+?}),loaderConf',
79 webpage, 'config', default=None)
80 if not json_config_string:
81 raise ExtractorError("Could not extract video player data")
82
83 json_config_string = json_config_string.replace("!0", "true").replace("!1", "false")
84
85 config = self._parse_json(json_config_string, video_id, transform_source=js_to_json, fatal=False)
86 if not config:
87 raise ExtractorError("Could not extract video player data")
88
89 config = config.get('mainRoll')
90 if isinstance(config, dict):
91 title = config.get('title')
92 thumbnail = config.get('poster')
93 duration = int_or_none(config.get('duration'))
94 sources = config.get('sources') or config.get('format')
95
96 if not isinstance(sources, dict):
97 sources = self._parse_json(self._search_regex(
98 r'(["\'])?sources\1?\s*:\s*(?P<sources>{.+?}),',
99 webpage, 'sources', group='sources'), video_id,
100 transform_source=js_to_json)
101
102 formats = []
103 for format_id, format_url in sources.items():
104 formats.append({
105 'url': format_url,
106 'format_id': format_id,
107 'height': int_or_none(format_id),
108 })
109 self._remove_duplicate_formats(formats)
110 self._sort_formats(formats)
111
112 if not title:
113 title = self._search_regex(
114 (r'<h1>\s*(?P<title>[^<]+?)\s*</h1>', r'videoTitle\s*:\s*(["\'])(?P<title>.+?)\1'),
115 webpage, 'title', group='title')
116 description = self._og_search_description(
117 webpage, default=None) or self._html_search_meta(
118 'twitter:description', webpage, default=None) or self._search_regex(
119 r'</h1>\s*<p>([^<]+)', webpage, 'description', fatal=False)
120 uploader = self._search_regex(
121 (r'<input[^>]+name="contentOwnerId"[^>]+value="([^"]+)"',
122 r'<span[^>]+class="nickname"[^>]*>([^<]+)'),
123 webpage, 'uploader', fatal=False)
124 if not duration:
125 duration = parse_duration(self._search_regex(
126 r'<dt>Runtime:?</dt>\s*<dd>([^<]+)</dd>',
127 webpage, 'duration', fatal=False))
128 view_count = str_to_int(self._search_regex(
129 (r'["\']viewsCount["\'][^>]*>(\d+)\s+views',
130 r'<dt>Views:?</dt>\s*<dd>([\d,\.]+)</dd>'),
131 webpage, 'view count', fatal=False))
132 comment_count = str_to_int(self._html_search_regex(
133 r'>Comments? \(([\d,\.]+)\)<',
134 webpage, 'comment count', fatal=False))
135
136 return {
137 'id': video_id,
138 'display_id': display_id,
139 'title': title,
140 'description': description,
141 'thumbnail': thumbnail,
142 'uploader': uploader,
143 'duration': duration,
144 'view_count': view_count,
145 'comment_count': comment_count,
146 'age_limit': 18,
147 'formats': formats,
148 }
149
150
151 class XTubeUserIE(InfoExtractor):
152 IE_DESC = 'XTube user profile'
153 _VALID_URL = r'https?://(?:www\.)?xtube\.com/profile/(?P<id>[^/]+-\d+)'
154 _TEST = {
155 'url': 'http://www.xtube.com/profile/greenshowers-4056496',
156 'info_dict': {
157 'id': 'greenshowers-4056496',
158 'age_limit': 18,
159 },
160 'playlist_mincount': 154,
161 }
162
163 def _real_extract(self, url):
164 user_id = self._match_id(url)
165
166 entries = []
167 for pagenum in itertools.count(1):
168 request = sanitized_Request(
169 'http://www.xtube.com/profile/%s/videos/%d' % (user_id, pagenum),
170 headers={
171 'Cookie': 'popunder=4',
172 'X-Requested-With': 'XMLHttpRequest',
173 'Referer': url,
174 })
175
176 page = self._download_json(
177 request, user_id, 'Downloading videos JSON page %d' % pagenum)
178
179 html = page.get('html')
180 if not html:
181 break
182
183 for video_id in orderedSet([video_id for _, video_id in re.findall(
184 r'data-plid=(["\'])(.+?)\1', html)]):
185 entries.append(self.url_result('xtube:%s' % video_id, XTubeIE.ie_key()))
186
187 page_count = int_or_none(page.get('pageCount'))
188 if not page_count or pagenum == page_count:
189 break
190
191 playlist = self.playlist_result(entries, user_id)
192 playlist['age_limit'] = 18
193 return playlist