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