779e4f46a1dd5315c6a9be3dad09e65c07a205b2
[youtube-dl] / youtube_dl / extractor / xtube.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import (
7     compat_urllib_request,
8     compat_urllib_parse_unquote,
9 )
10 from ..utils import (
11     parse_duration,
12     str_to_int,
13 )
14
15
16 class XTubeIE(InfoExtractor):
17     _VALID_URL = r'https?://(?:www\.)?(?P<url>xtube\.com/watch\.php\?v=(?P<id>[^/?&#]+))'
18     _TEST = {
19         'url': 'http://www.xtube.com/watch.php?v=kVTUy_G222_',
20         'md5': '092fbdd3cbe292c920ef6fc6a8a9cdab',
21         'info_dict': {
22             'id': 'kVTUy_G222_',
23             'ext': 'mp4',
24             'title': 'strange erotica',
25             'description': 'contains:an ET kind of thing',
26             'uploader': 'greenshowers',
27             'duration': 450,
28             'age_limit': 18,
29         }
30     }
31
32     def _real_extract(self, url):
33         video_id = self._match_id(url)
34
35         req = compat_urllib_request.Request(url)
36         req.add_header('Cookie', 'age_verified=1')
37         webpage = self._download_webpage(req, video_id)
38
39         video_title = self._html_search_regex(
40             r'<p class="title">([^<]+)', webpage, 'title')
41         video_uploader = self._html_search_regex(
42             [r"var\s+contentOwnerId\s*=\s*'([^']+)",
43              r'By:\s*<a href="/community/profile\.php\?user=([^"]+)'],
44             webpage, 'uploader', fatal=False)
45         video_description = self._html_search_regex(
46             r'<p class="fieldsDesc">([^<]+)',
47             webpage, 'description', fatal=False)
48         duration = parse_duration(self._html_search_regex(
49             r'<span class="bold">Runtime:</span> ([^<]+)</p>',
50             webpage, 'duration', fatal=False))
51         view_count = str_to_int(self._html_search_regex(
52             r'<span class="bold">Views:</span> ([\d,\.]+)</p>',
53             webpage, 'view count', fatal=False))
54         comment_count = str_to_int(self._html_search_regex(
55             r'<div id="commentBar">([\d,\.]+) Comments</div>',
56             webpage, 'comment count', fatal=False))
57
58         formats = []
59         for format_id, video_url in re.findall(
60                 r'flashvars\.quality_(.+?)\s*=\s*"([^"]+)"', webpage):
61             fmt = {
62                 'url': compat_urllib_parse_unquote(video_url),
63                 'format_id': format_id,
64             }
65             m = re.search(r'^(?P<height>\d+)[pP]', format_id)
66             if m:
67                 fmt['height'] = int(m.group('height'))
68             formats.append(fmt)
69
70         if not formats:
71             video_url = compat_urllib_parse_unquote(self._search_regex(
72                 r'flashvars\.video_url\s*=\s*"([^"]+)"',
73                 webpage, 'video URL'))
74             formats.append({'url': video_url})
75
76         self._sort_formats(formats)
77
78         return {
79             'id': video_id,
80             'title': video_title,
81             'uploader': video_uploader,
82             'description': video_description,
83             'duration': duration,
84             'view_count': view_count,
85             'comment_count': comment_count,
86             'formats': formats,
87             'age_limit': 18,
88         }
89
90
91 class XTubeUserIE(InfoExtractor):
92     IE_DESC = 'XTube user profile'
93     _VALID_URL = r'https?://(?:www\.)?xtube\.com/community/profile\.php\?(.*?)user=(?P<username>[^&#]+)(?:$|[&#])'
94     _TEST = {
95         'url': 'http://www.xtube.com/community/profile.php?user=greenshowers',
96         'info_dict': {
97             'id': 'greenshowers',
98             'age_limit': 18,
99         },
100         'playlist_mincount': 155,
101     }
102
103     def _real_extract(self, url):
104         mobj = re.match(self._VALID_URL, url)
105         username = mobj.group('username')
106
107         profile_page = self._download_webpage(
108             url, username, note='Retrieving profile page')
109
110         video_count = int(self._search_regex(
111             r'<strong>%s\'s Videos \(([0-9]+)\)</strong>' % username, profile_page,
112             'video count'))
113
114         PAGE_SIZE = 25
115         urls = []
116         page_count = (video_count + PAGE_SIZE + 1) // PAGE_SIZE
117         for n in range(1, page_count + 1):
118             lpage_url = 'http://www.xtube.com/user_videos.php?page=%d&u=%s' % (n, username)
119             lpage = self._download_webpage(
120                 lpage_url, username,
121                 note='Downloading page %d/%d' % (n, page_count))
122             urls.extend(
123                 re.findall(r'addthis:url="([^"]+)"', lpage))
124
125         return {
126             '_type': 'playlist',
127             'id': username,
128             'age_limit': 18,
129             'entries': [{
130                 '_type': 'url',
131                 'url': eurl,
132                 'ie_key': 'XTube',
133             } for eurl in urls]
134         }