]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/xtube.py
[nexx] Improve JS embed extraction
[yt-dlp.git] / youtube_dl / 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:|
21 https?://(?:www\.)?xtube\.com/(?:watch\.php\?.*\bv=|video-watch/(?P<display_id>[^/]+)-)
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)',
50 'description': 'md5:ca0d47afff4a9b2942e4b41aa970fd93',
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,
86be3cdc 67 }]
dcc2a706 68
69 def _real_extract(self, url):
86be3cdc
S
70 mobj = re.match(self._VALID_URL, url)
71 video_id = mobj.group('id')
72 display_id = mobj.group('display_id')
73
74 if not display_id:
75 display_id = video_id
86be3cdc 76
085f169f
S
77 if video_id.isdigit() and len(video_id) < 11:
78 url_pattern = 'http://www.xtube.com/video-watch/-%s'
79 else:
80 url_pattern = 'http://www.xtube.com/watch.php?v=%s'
81
82 webpage = self._download_webpage(
83 url_pattern % video_id, display_id, headers={
84 'Cookie': 'age_verified=1; cookiesAccepted=1',
85 })
dcc2a706 86
1b734adb 87 sources = self._parse_json(self._search_regex(
24eb7c25
YCH
88 r'(["\'])?sources\1?\s*:\s*(?P<sources>{.+?}),',
89 webpage, 'sources', group='sources'), video_id,
90 transform_source=js_to_json)
1b734adb
S
91
92 formats = []
93 for format_id, format_url in sources.items():
94 formats.append({
95 'url': format_url,
96 'format_id': format_id,
97 'height': int_or_none(format_id),
98 })
24eb7c25 99 self._remove_duplicate_formats(formats)
1b734adb
S
100 self._sort_formats(formats)
101
102 title = self._search_regex(
f6d6ca1d 103 (r'<h1>\s*(?P<title>[^<]+?)\s*</h1>', r'videoTitle\s*:\s*(["\'])(?P<title>.+?)\1'),
1b734adb 104 webpage, 'title', group='title')
86be3cdc
S
105 description = self._search_regex(
106 r'</h1>\s*<p>([^<]+)', webpage, 'description', fatal=False)
1b734adb
S
107 uploader = self._search_regex(
108 (r'<input[^>]+name="contentOwnerId"[^>]+value="([^"]+)"',
109 r'<span[^>]+class="nickname"[^>]*>([^<]+)'),
110 webpage, 'uploader', fatal=False)
111 duration = parse_duration(self._search_regex(
9150d1eb 112 r'<dt>Runtime:?</dt>\s*<dd>([^<]+)</dd>',
1b734adb 113 webpage, 'duration', fatal=False))
86be3cdc 114 view_count = str_to_int(self._search_regex(
9150d1eb 115 r'<dt>Views:?</dt>\s*<dd>([\d,\.]+)</dd>',
16ea8179
S
116 webpage, 'view count', fatal=False))
117 comment_count = str_to_int(self._html_search_regex(
86be3cdc 118 r'>Comments? \(([\d,\.]+)\)<',
16ea8179 119 webpage, 'comment count', fatal=False))
aa488e13 120
dcc2a706 121 return {
122 'id': video_id,
86be3cdc 123 'display_id': display_id,
86be3cdc
S
124 'title': title,
125 'description': description,
126 'uploader': uploader,
607dbbad
S
127 'duration': duration,
128 'view_count': view_count,
129 'comment_count': comment_count,
dcc2a706 130 'age_limit': 18,
1b734adb 131 'formats': formats,
9f5809b3 132 }
133
22a6f150 134
9f5809b3 135class XTubeUserIE(InfoExtractor):
136 IE_DESC = 'XTube user profile'
34dd81c0 137 _VALID_URL = r'https?://(?:www\.)?xtube\.com/profile/(?P<id>[^/]+-\d+)'
22a6f150 138 _TEST = {
34dd81c0 139 'url': 'http://www.xtube.com/profile/greenshowers-4056496',
22a6f150 140 'info_dict': {
34dd81c0 141 'id': 'greenshowers-4056496',
05900629 142 'age_limit': 18,
22a6f150
PH
143 },
144 'playlist_mincount': 155,
145 }
9f5809b3 146
147 def _real_extract(self, url):
34dd81c0
S
148 user_id = self._match_id(url)
149
150 entries = []
151 for pagenum in itertools.count(1):
152 request = sanitized_Request(
153 'http://www.xtube.com/profile/%s/videos/%d' % (user_id, pagenum),
154 headers={
155 'Cookie': 'popunder=4',
156 'X-Requested-With': 'XMLHttpRequest',
157 'Referer': url,
158 })
159
160 page = self._download_json(
161 request, user_id, 'Downloading videos JSON page %d' % pagenum)
162
163 html = page.get('html')
164 if not html:
165 break
166
f4db0917
S
167 for video_id in orderedSet([video_id for _, video_id in re.findall(
168 r'data-plid=(["\'])(.+?)\1', html)]):
34dd81c0
S
169 entries.append(self.url_result('xtube:%s' % video_id, XTubeIE.ie_key()))
170
171 page_count = int_or_none(page.get('pageCount'))
172 if not page_count or pagenum == page_count:
173 break
174
175 playlist = self.playlist_result(entries, user_id)
176 playlist['age_limit'] = 18
177 return playlist