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