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