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