]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/xtube.py
PEP8: more applied
[yt-dlp.git] / youtube_dl / extractor / xtube.py
CommitLineData
c5ba203e
AS
1from __future__ import unicode_literals
2
dcc2a706 3import re
aa488e13 4import json
dcc2a706 5
6from .common import InfoExtractor
7from ..utils import (
dcc2a706 8 compat_urllib_request,
607dbbad
S
9 parse_duration,
10 str_to_int,
dcc2a706 11)
12
607dbbad 13
dcc2a706 14class XTubeIE(InfoExtractor):
607dbbad 15 _VALID_URL = r'https?://(?:www\.)?(?P<url>xtube\.com/watch\.php\?v=(?P<videoid>[^/?&]+))'
dcc2a706 16 _TEST = {
c5ba203e 17 'url': 'http://www.xtube.com/watch.php?v=kVTUy_G222_',
c5ba203e
AS
18 'md5': '092fbdd3cbe292c920ef6fc6a8a9cdab',
19 'info_dict': {
607dbbad
S
20 'id': 'kVTUy_G222_',
21 'ext': 'mp4',
22 'title': 'strange erotica',
3adba6fa 23 'description': 'http://www.xtube.com an ET kind of thing',
607dbbad
S
24 'uploader': 'greenshowers',
25 'duration': 450,
26 'age_limit': 18,
dcc2a706 27 }
28 }
29
30 def _real_extract(self, url):
31 mobj = re.match(self._VALID_URL, url)
32 video_id = mobj.group('videoid')
33 url = 'http://www.' + mobj.group('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
607dbbad
S
39 video_title = self._html_search_regex(r'<p class="title">([^<]+)', webpage, 'title')
40 video_uploader = self._html_search_regex(
41 r'so_s\.addVariable\("owner_u", "([^"]+)', webpage, 'uploader', fatal=False)
42 video_description = self._html_search_regex(
43 r'<p class="fieldsDesc">([^<]+)', webpage, 'description', fatal=False)
607dbbad
S
44 duration = parse_duration(self._html_search_regex(
45 r'<span class="bold">Runtime:</span> ([^<]+)</p>', webpage, 'duration', fatal=False))
46 view_count = self._html_search_regex(
47 r'<span class="bold">Views:</span> ([\d,\.]+)</p>', webpage, 'view count', fatal=False)
48 if view_count:
49 view_count = str_to_int(view_count)
50 comment_count = self._html_search_regex(
51 r'<div id="commentBar">([\d,\.]+) Comments</div>', webpage, 'comment count', fatal=False)
52 if comment_count:
53 comment_count = str_to_int(comment_count)
54
aa488e13
S
55 player_quality_option = json.loads(self._html_search_regex(
56 r'playerQualityOption = ({.+?});', webpage, 'player quality option'))
57
58 QUALITIES = ['3gp', 'mp4_normal', 'mp4_high', 'flv', 'mp4_ultra', 'mp4_720', 'mp4_1080']
59 formats = [
60 {
56dd5572 61 'url': furl,
aa488e13
S
62 'format_id': format_id,
63 'preference': QUALITIES.index(format_id) if format_id in QUALITIES else -1,
56dd5572 64 } for format_id, furl in player_quality_option.items()
aa488e13
S
65 ]
66 self._sort_formats(formats)
dcc2a706 67
68 return {
69 'id': video_id,
70 'title': video_title,
71 'uploader': video_uploader,
72 'description': video_description,
607dbbad
S
73 'duration': duration,
74 'view_count': view_count,
75 'comment_count': comment_count,
aa488e13 76 'formats': formats,
dcc2a706 77 'age_limit': 18,
9f5809b3 78 }
79
22a6f150 80
9f5809b3 81class XTubeUserIE(InfoExtractor):
82 IE_DESC = 'XTube user profile'
83 _VALID_URL = r'https?://(?:www\.)?xtube\.com/community/profile\.php\?(.*?)user=(?P<username>[^&#]+)(?:$|[&#])'
22a6f150
PH
84 _TEST = {
85 'url': 'http://www.xtube.com/community/profile.php?user=greenshowers',
86 'info_dict': {
87 'id': 'greenshowers',
88 },
89 'playlist_mincount': 155,
90 }
9f5809b3 91
92 def _real_extract(self, url):
93 mobj = re.match(self._VALID_URL, url)
94 username = mobj.group('username')
95
96 profile_page = self._download_webpage(
97 url, username, note='Retrieving profile page')
98
99 video_count = int(self._search_regex(
8bcc8756 100 r'<strong>%s\'s Videos \(([0-9]+)\)</strong>' %username, profile_page,
9f5809b3 101 'video count'))
102
103 PAGE_SIZE = 25
104 urls = []
105 page_count = (video_count + PAGE_SIZE + 1) // PAGE_SIZE
106 for n in range(1, page_count + 1):
107 lpage_url = 'http://www.xtube.com/user_videos.php?page=%d&u=%s' % (n, username)
108 lpage = self._download_webpage(
109 lpage_url, username,
110 note='Downloading page %d/%d' % (n, page_count))
111 urls.extend(
112 re.findall(r'addthis:url="([^"]+)"', lpage))
113
114 return {
115 '_type': 'playlist',
116 'id': username,
117 'entries': [{
118 '_type': 'url',
119 'url': eurl,
120 'ie_key': 'XTube',
121 } for eurl in urls]
122 }