Merge branch 'Weiqitv' of https://github.com/FounderSG/youtube-dl into FounderSG...
[youtube-dl] / 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 ..compat import compat_urllib_parse_unquote
8 from ..utils import (
9     int_or_none,
10     parse_duration,
11     sanitized_Request,
12     str_to_int,
13 )
14
15
16 class XTubeIE(InfoExtractor):
17     _VALID_URL = r'(?:xtube:|https?://(?:www\.)?xtube\.com/watch\.php\?.*\bv=)(?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 = sanitized_Request('http://www.xtube.com/watch.php?v=%s' % video_id)
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/profile/(?P<id>[^/]+-\d+)'
94     _TEST = {
95         'url': 'http://www.xtube.com/profile/greenshowers-4056496',
96         'info_dict': {
97             'id': 'greenshowers-4056496',
98             'age_limit': 18,
99         },
100         'playlist_mincount': 155,
101     }
102
103     def _real_extract(self, url):
104         user_id = self._match_id(url)
105
106         entries = []
107         for pagenum in itertools.count(1):
108             request = sanitized_Request(
109                 'http://www.xtube.com/profile/%s/videos/%d' % (user_id, pagenum),
110                 headers={
111                     'Cookie': 'popunder=4',
112                     'X-Requested-With': 'XMLHttpRequest',
113                     'Referer': url,
114                 })
115
116             page = self._download_json(
117                 request, user_id, 'Downloading videos JSON page %d' % pagenum)
118
119             html = page.get('html')
120             if not html:
121                 break
122
123             for _, video_id in re.findall(r'data-plid=(["\'])(.+?)\1', html):
124                 entries.append(self.url_result('xtube:%s' % video_id, XTubeIE.ie_key()))
125
126             page_count = int_or_none(page.get('pageCount'))
127             if not page_count or pagenum == page_count:
128                 break
129
130         playlist = self.playlist_result(entries, user_id)
131         playlist['age_limit'] = 18
132         return playlist