]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/xtube.py
Merge pull request #3927 from qrtt1/master
[youtube-dl] / youtube_dl / extractor / xtube.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5
6 from .common import InfoExtractor
7 from ..compat import (
8     compat_urllib_request,
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': 'http://www.xtube.com 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         mobj = re.match(self._VALID_URL, url)
34         video_id = mobj.group('id')
35         url = 'http://www.' + mobj.group('url')
36
37         req = compat_urllib_request.Request(url)
38         req.add_header('Cookie', 'age_verified=1')
39         webpage = self._download_webpage(req, video_id)
40
41         video_title = self._html_search_regex(r'<p class="title">([^<]+)', webpage, 'title')
42         video_uploader = self._html_search_regex(
43             r'so_s\.addVariable\("owner_u", "([^"]+)', webpage, 'uploader', fatal=False)
44         video_description = self._html_search_regex(
45             r'<p class="fieldsDesc">([^<]+)', webpage, 'description', fatal=False)
46         duration = parse_duration(self._html_search_regex(
47             r'<span class="bold">Runtime:</span> ([^<]+)</p>', webpage, 'duration', fatal=False))
48         view_count = self._html_search_regex(
49             r'<span class="bold">Views:</span> ([\d,\.]+)</p>', webpage, 'view count', fatal=False)
50         if view_count:
51             view_count = str_to_int(view_count)
52         comment_count = self._html_search_regex(
53             r'<div id="commentBar">([\d,\.]+) Comments</div>', webpage, 'comment count', fatal=False)
54         if comment_count:
55             comment_count = str_to_int(comment_count)
56
57         player_quality_option = json.loads(self._html_search_regex(
58             r'playerQualityOption = ({.+?});', webpage, 'player quality option'))
59
60         QUALITIES = ['3gp', 'mp4_normal', 'mp4_high', 'flv', 'mp4_ultra', 'mp4_720', 'mp4_1080']
61         formats = [
62             {
63                 'url': furl,
64                 'format_id': format_id,
65                 'preference': QUALITIES.index(format_id) if format_id in QUALITIES else -1,
66             } for format_id, furl in player_quality_option.items()
67         ]
68         self._sort_formats(formats)
69
70         return {
71             'id': video_id,
72             'title': video_title,
73             'uploader': video_uploader,
74             'description': video_description,
75             'duration': duration,
76             'view_count': view_count,
77             'comment_count': comment_count,
78             'formats': formats,
79             'age_limit': 18,
80         }
81
82
83 class XTubeUserIE(InfoExtractor):
84     IE_DESC = 'XTube user profile'
85     _VALID_URL = r'https?://(?:www\.)?xtube\.com/community/profile\.php\?(.*?)user=(?P<username>[^&#]+)(?:$|[&#])'
86     _TEST = {
87         'url': 'http://www.xtube.com/community/profile.php?user=greenshowers',
88         'info_dict': {
89             'id': 'greenshowers',
90         },
91         'playlist_mincount': 155,
92     }
93
94     def _real_extract(self, url):
95         mobj = re.match(self._VALID_URL, url)
96         username = mobj.group('username')
97
98         profile_page = self._download_webpage(
99             url, username, note='Retrieving profile page')
100
101         video_count = int(self._search_regex(
102             r'<strong>%s\'s Videos \(([0-9]+)\)</strong>' % username, profile_page,
103             'video count'))
104
105         PAGE_SIZE = 25
106         urls = []
107         page_count = (video_count + PAGE_SIZE + 1) // PAGE_SIZE
108         for n in range(1, page_count + 1):
109             lpage_url = 'http://www.xtube.com/user_videos.php?page=%d&u=%s' % (n, username)
110             lpage = self._download_webpage(
111                 lpage_url, username,
112                 note='Downloading page %d/%d' % (n, page_count))
113             urls.extend(
114                 re.findall(r'addthis:url="([^"]+)"', lpage))
115
116         return {
117             '_type': 'playlist',
118             'id': username,
119             'entries': [{
120                 '_type': 'url',
121                 'url': eurl,
122                 'ie_key': 'XTube',
123             } for eurl in urls]
124         }