[xtube] Fix extraction add more metafields
[youtube-dl] / youtube_dl / extractor / xtube.py
1 from __future__ import unicode_literals
2
3 import os
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     compat_urllib_parse_urlparse,
9     compat_urllib_request,
10     parse_duration,
11     str_to_int,
12 )
13
14
15 class XTubeIE(InfoExtractor):
16     _VALID_URL = r'https?://(?:www\.)?(?P<url>xtube\.com/watch\.php\?v=(?P<videoid>[^/?&]+))'
17     _TEST = {
18         'url': 'http://www.xtube.com/watch.php?v=kVTUy_G222_',
19         'md5': '092fbdd3cbe292c920ef6fc6a8a9cdab',
20         'info_dict': {
21             'id': 'kVTUy_G222_',
22             'ext': 'mp4',
23             'title': 'strange erotica',
24             'description': 'surreal gay themed erotica...almost an ET kind of thing',
25             'uploader': 'greenshowers',
26             'duration': 450,
27             'age_limit': 18,
28         }
29     }
30
31     def _real_extract(self, url):
32         mobj = re.match(self._VALID_URL, url)
33         video_id = mobj.group('videoid')
34         url = 'http://www.' + mobj.group('url')
35
36         req = compat_urllib_request.Request(url)
37         req.add_header('Cookie', 'age_verified=1')
38         webpage = self._download_webpage(req, video_id)
39
40         video_title = self._html_search_regex(r'<p class="title">([^<]+)', webpage, 'title')
41         video_uploader = self._html_search_regex(
42             r'so_s\.addVariable\("owner_u", "([^"]+)', webpage, 'uploader', fatal=False)
43         video_description = self._html_search_regex(
44             r'<p class="fieldsDesc">([^<]+)', webpage, 'description', fatal=False)
45         video_url = self._html_search_regex(r'var videoMp4 = "([^"]+)', webpage, 'video_url').replace('\\/', '/')
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         path = compat_urllib_parse_urlparse(video_url).path
58         extension = os.path.splitext(path)[1][1:]
59         format = path.split('/')[5].split('_')[:2]
60         format[0] += 'p'
61         format[1] += 'k'
62         format = "-".join(format)
63
64         return {
65             'id': video_id,
66             'title': video_title,
67             'uploader': video_uploader,
68             'description': video_description,
69             'duration': duration,
70             'view_count': view_count,
71             'comment_count': comment_count,
72             'url': video_url,
73             'ext': extension,
74             'format': format,
75             'format_id': format,
76             'age_limit': 18,
77         }