Merge remote-tracking branch 'dstftw/generic-webpage-unescape'
[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 ..utils import (
8     compat_urllib_request,
9     parse_duration,
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<videoid>[^/?&]+))'
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': 'surreal gay themed erotica...almost 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         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
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)
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
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             {
61                 'url': url,
62                 'format_id': format_id,
63                 'preference': QUALITIES.index(format_id) if format_id in QUALITIES else -1,
64             } for format_id, url in player_quality_option.items()
65         ]
66         self._sort_formats(formats)
67
68         return {
69             'id': video_id,
70             'title': video_title,
71             'uploader': video_uploader,
72             'description': video_description,
73             'duration': duration,
74             'view_count': view_count,
75             'comment_count': comment_count,
76             'formats': formats,
77             'age_limit': 18,
78         }