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