[tube8] Fix extraction and extract all formats (Closes #8281)
[youtube-dl] / youtube_dl / extractor / tube8.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import compat_str
7 from ..utils import (
8     int_or_none,
9     sanitized_Request,
10     str_to_int,
11 )
12 from ..aes import aes_decrypt_text
13
14
15 class Tube8IE(InfoExtractor):
16     _VALID_URL = r'https?://(?:www\.)?tube8\.com/(?:[^/]+/)+(?P<display_id>[^/]+)/(?P<id>\d+)'
17     _TESTS = [
18         {
19             'url': 'http://www.tube8.com/teen/kasia-music-video/229795/',
20             'md5': '44bf12b98313827dd52d35b8706a4ea0',
21             'info_dict': {
22                 'id': '229795',
23                 'display_id': 'kasia-music-video',
24                 'ext': 'mp4',
25                 'description': 'hot teen Kasia grinding',
26                 'uploader': 'unknown',
27                 'title': 'Kasia music video',
28                 'age_limit': 18,
29             }
30         },
31         {
32             'url': 'http://www.tube8.com/shemale/teen/blonde-cd-gets-kidnapped-by-two-blacks-and-punished-for-being-a-slutty-girl/19569151/',
33             'only_matching': True,
34         },
35     ]
36
37     def _real_extract(self, url):
38         mobj = re.match(self._VALID_URL, url)
39         video_id = mobj.group('id')
40         display_id = mobj.group('display_id')
41
42         req = sanitized_Request(url)
43         req.add_header('Cookie', 'age_verified=1')
44         webpage = self._download_webpage(req, display_id)
45
46         flashvars = self._parse_json(
47             self._search_regex(
48                 r'flashvars\s*=\s*({.+?});\r?\n', webpage, 'flashvars'),
49             video_id)
50
51         formats = []
52         for key, video_url in flashvars.items():
53             if not isinstance(video_url, compat_str) or not video_url.startswith('http'):
54                 continue
55             height = self._search_regex(
56                 r'quality_(\d+)[pP]', key, 'height', default=None)
57             if not height:
58                 continue
59             if flashvars.get('encrypted') is True:
60                 video_url = aes_decrypt_text(
61                     video_url, flashvars['video_title'], 32).decode('utf-8')
62             formats.append({
63                 'url': video_url,
64                 'format_id': '%sp' % height,
65                 'height': int(height),
66             })
67         self._sort_formats(formats)
68
69         thumbnail = flashvars.get('image_url')
70
71         title = self._html_search_regex(
72             r'videoTitle\s*=\s*"([^"]+)', webpage, 'title')
73         description = self._html_search_regex(
74             r'>Description:</strong>\s*(.+?)\s*<', webpage, 'description', fatal=False)
75         uploader = self._html_search_regex(
76             r'<span class="username">\s*(.+?)\s*<',
77             webpage, 'uploader', fatal=False)
78
79         like_count = int_or_none(self._html_search_regex(
80             r'rupVar\s*=\s*"(\d+)"', webpage, 'like count', fatal=False))
81         dislike_count = int_or_none(self._html_search_regex(
82             r'rdownVar\s*=\s*"(\d+)"', webpage, 'dislike count', fatal=False))
83         view_count = self._html_search_regex(
84             r'<strong>Views: </strong>([\d,\.]+)\s*</li>', webpage, 'view count', fatal=False)
85         if view_count:
86             view_count = str_to_int(view_count)
87         comment_count = self._html_search_regex(
88             r'<span id="allCommentsCount">(\d+)</span>', webpage, 'comment count', fatal=False)
89         if comment_count:
90             comment_count = str_to_int(comment_count)
91
92         return {
93             'id': video_id,
94             'display_id': display_id,
95             'title': title,
96             'description': description,
97             'thumbnail': thumbnail,
98             'uploader': uploader,
99             'view_count': view_count,
100             'like_count': like_count,
101             'dislike_count': dislike_count,
102             'comment_count': comment_count,
103             'age_limit': 18,
104             'formats': formats,
105         }