]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/xtube.py
Prepare to release.
[youtubedl] / youtube_dl / 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 orderedSet,
10 parse_duration,
11 sanitized_Request,
12 str_to_int,
13 )
14
15
16 class XTubeIE(InfoExtractor):
17 _VALID_URL = r'''(?x)
18 (?:
19 xtube:|
20 https?://(?:www\.)?xtube\.com/(?:watch\.php\?.*\bv=|video-watch/(?P<display_id>[^/]+)-)
21 )
22 (?P<id>[^/?&#]+)
23 '''
24
25 _TESTS = [{
26 # old URL schema
27 'url': 'http://www.xtube.com/watch.php?v=kVTUy_G222_',
28 'md5': '092fbdd3cbe292c920ef6fc6a8a9cdab',
29 'info_dict': {
30 'id': 'kVTUy_G222_',
31 'ext': 'mp4',
32 'title': 'strange erotica',
33 'description': 'contains:an ET kind of thing',
34 'uploader': 'greenshowers',
35 'duration': 450,
36 'view_count': int,
37 'comment_count': int,
38 'age_limit': 18,
39 }
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,
47 }, {
48 'url': 'xtube:kVTUy_G222_',
49 'only_matching': True,
50 }]
51
52 def _real_extract(self, url):
53 mobj = re.match(self._VALID_URL, url)
54 video_id = mobj.group('id')
55 display_id = mobj.group('display_id')
56
57 if not display_id:
58 display_id = video_id
59
60 if video_id.isdigit() and len(video_id) < 11:
61 url_pattern = 'http://www.xtube.com/video-watch/-%s'
62 else:
63 url_pattern = 'http://www.xtube.com/watch.php?v=%s'
64
65 webpage = self._download_webpage(
66 url_pattern % video_id, display_id, headers={
67 'Cookie': 'age_verified=1; cookiesAccepted=1',
68 })
69
70 sources = self._parse_json(self._search_regex(
71 r'(["\'])sources\1\s*:\s*(?P<sources>{.+?}),',
72 webpage, 'sources', group='sources'), video_id)
73
74 formats = []
75 for format_id, format_url in sources.items():
76 formats.append({
77 'url': format_url,
78 'format_id': format_id,
79 'height': int_or_none(format_id),
80 })
81 self._sort_formats(formats)
82
83 title = self._search_regex(
84 (r'<h1>\s*(?P<title>[^<]+?)\s*</h1>', r'videoTitle\s*:\s*(["\'])(?P<title>.+?)\1'),
85 webpage, 'title', group='title')
86 description = self._search_regex(
87 r'</h1>\s*<p>([^<]+)', webpage, 'description', fatal=False)
88 uploader = self._search_regex(
89 (r'<input[^>]+name="contentOwnerId"[^>]+value="([^"]+)"',
90 r'<span[^>]+class="nickname"[^>]*>([^<]+)'),
91 webpage, 'uploader', fatal=False)
92 duration = parse_duration(self._search_regex(
93 r'<dt>Runtime:?</dt>\s*<dd>([^<]+)</dd>',
94 webpage, 'duration', fatal=False))
95 view_count = str_to_int(self._search_regex(
96 r'<dt>Views:?</dt>\s*<dd>([\d,\.]+)</dd>',
97 webpage, 'view count', fatal=False))
98 comment_count = str_to_int(self._html_search_regex(
99 r'>Comments? \(([\d,\.]+)\)<',
100 webpage, 'comment count', fatal=False))
101
102 return {
103 'id': video_id,
104 'display_id': display_id,
105 'title': title,
106 'description': description,
107 'uploader': uploader,
108 'duration': duration,
109 'view_count': view_count,
110 'comment_count': comment_count,
111 'age_limit': 18,
112 'formats': formats,
113 }
114
115
116 class XTubeUserIE(InfoExtractor):
117 IE_DESC = 'XTube user profile'
118 _VALID_URL = r'https?://(?:www\.)?xtube\.com/profile/(?P<id>[^/]+-\d+)'
119 _TEST = {
120 'url': 'http://www.xtube.com/profile/greenshowers-4056496',
121 'info_dict': {
122 'id': 'greenshowers-4056496',
123 'age_limit': 18,
124 },
125 'playlist_mincount': 155,
126 }
127
128 def _real_extract(self, url):
129 user_id = self._match_id(url)
130
131 entries = []
132 for pagenum in itertools.count(1):
133 request = sanitized_Request(
134 'http://www.xtube.com/profile/%s/videos/%d' % (user_id, pagenum),
135 headers={
136 'Cookie': 'popunder=4',
137 'X-Requested-With': 'XMLHttpRequest',
138 'Referer': url,
139 })
140
141 page = self._download_json(
142 request, user_id, 'Downloading videos JSON page %d' % pagenum)
143
144 html = page.get('html')
145 if not html:
146 break
147
148 for video_id in orderedSet([video_id for _, video_id in re.findall(
149 r'data-plid=(["\'])(.+?)\1', html)]):
150 entries.append(self.url_result('xtube:%s' % video_id, XTubeIE.ie_key()))
151
152 page_count = int_or_none(page.get('pageCount'))
153 if not page_count or pagenum == page_count:
154 break
155
156 playlist = self.playlist_result(entries, user_id)
157 playlist['age_limit'] = 18
158 return playlist