[extremetube] Fix extraction (Closes #7163)
[youtube-dl] / youtube_dl / extractor / extremetube.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import compat_urllib_request
7 from ..utils import (
8     int_or_none,
9     str_to_int,
10 )
11
12
13 class ExtremeTubeIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:www\.)?(?P<url>extremetube\.com/.*?video/.+?(?P<id>[0-9]+))(?:[/?&]|$)'
15     _TESTS = [{
16         'url': 'http://www.extremetube.com/video/music-video-14-british-euro-brit-european-cumshots-swallow-652431',
17         'md5': '344d0c6d50e2f16b06e49ca011d8ac69',
18         'info_dict': {
19             'id': '652431',
20             'ext': 'mp4',
21             'title': 'Music Video 14 british euro brit european cumshots swallow',
22             'uploader': 'unknown',
23             'view_count': int,
24             'age_limit': 18,
25         }
26     }, {
27         'url': 'http://www.extremetube.com/gay/video/abcde-1234',
28         'only_matching': True,
29     }]
30
31     def _real_extract(self, url):
32         mobj = re.match(self._VALID_URL, url)
33         video_id = mobj.group('id')
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(
41             r'<h1 [^>]*?title="([^"]+)"[^>]*>', webpage, 'title')
42         uploader = self._html_search_regex(
43             r'Uploaded by:\s*</strong>\s*(.+?)\s*</div>',
44             webpage, 'uploader', fatal=False)
45         view_count = str_to_int(self._html_search_regex(
46             r'Views:\s*</strong>\s*<span>([\d,\.]+)</span>',
47             webpage, 'view count', fatal=False))
48
49         flash_vars = self._parse_json(
50             self._search_regex(
51                 r'var\s+flashvars\s*=\s*({.+?});', webpage, 'flash vars'),
52             video_id)
53
54         formats = []
55         for quality_key, video_url in flash_vars.items():
56             height = int_or_none(self._search_regex(
57                 r'quality_(\d+)[pP]$', quality_key, 'height', default=None))
58             if not height:
59                 continue
60             f = {
61                 'url': video_url,
62             }
63             mobj = re.search(
64                 r'/(?P<height>\d{3,4})[pP]_(?P<bitrate>\d+)[kK]_\d+', video_url)
65             if mobj:
66                 height = int(mobj.group('height'))
67                 bitrate = int(mobj.group('bitrate'))
68                 f.update({
69                     'format_id': '%dp-%dk' % (height, bitrate),
70                     'height': height,
71                     'tbr': bitrate,
72                 })
73             else:
74                 f.update({
75                     'format_id': '%dp' % height,
76                     'height': height,
77                 })
78             formats.append(f)
79         self._sort_formats(formats)
80
81         return {
82             'id': video_id,
83             'title': video_title,
84             'formats': formats,
85             'uploader': uploader,
86             'view_count': view_count,
87             'age_limit': 18,
88         }