[tube8] Fix extraction and modernize
[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 ..utils import (
8     compat_urllib_parse_urlparse,
9     compat_urllib_request,
10     int_or_none,
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/(?:[^/]+/){2}(?P<id>\d+)'
18     _TEST = {
19         'url': 'http://www.tube8.com/teen/kasia-music-video/229795/',
20         'file': '229795.mp4',
21         'md5': 'e9e0b0c86734e5e3766e653509475db0',
22         'info_dict': {
23             'description': 'hot teen Kasia grinding',
24             'uploader': 'unknown',
25             'title': 'Kasia music video',
26             'age_limit': 18,
27         }
28     }
29
30     def _real_extract(self, url):
31         mobj = re.match(self._VALID_URL, url)
32         video_id = mobj.group('id')
33
34         req = compat_urllib_request.Request(url)
35         req.add_header('Cookie', 'age_verified=1')
36         webpage = self._download_webpage(req, video_id)
37
38         flashvars = json.loads(self._html_search_regex(
39             r'var flashvars\s*=\s*({.+?})', webpage, 'flashvars'))
40
41         video_url = flashvars['video_url']
42         if flashvars.get('encrypted') is True:
43             video_url = aes_decrypt_text(video_url, flashvars['video_title'], 32).decode('utf-8')
44         path = compat_urllib_parse_urlparse(video_url).path
45         format_id = '-'.join(path.split('/')[4].split('_')[:2])
46
47         thumbnail = flashvars.get('image_url')
48
49         title = self._html_search_regex(
50             r'videotitle\s*=\s*"([^"]+)', webpage, 'title')
51         description = self._html_search_regex(
52             r'>Description:</strong>(.+?)<', webpage, 'description', fatal=False)
53         uploader = self._html_search_regex(
54             r'<strong class="video-username">(?:<a href="[^"]+">)?([^<]+)(?:</a>)?</strong>',
55             webpage, 'uploader', fatal=False)
56
57         like_count = int_or_none(self._html_search_regex(
58             r"rupVar\s*=\s*'(\d+)'", webpage, 'like count', fatal=False))
59         dislike_count = int_or_none(self._html_search_regex(
60             r"rdownVar\s*=\s*'(\d+)'", webpage, 'dislike count', fatal=False))
61         view_count = self._html_search_regex(
62             r'<strong>Views: </strong>([\d,\.]+)</li>', webpage, 'view count', fatal=False)
63         if view_count:
64             view_count = str_to_int(view_count)
65         comment_count = self._html_search_regex(
66             r'<span id="allCommentsCount">(\d+)</span>', webpage, 'comment count', fatal=False)
67         if comment_count:
68             comment_count = str_to_int(comment_count)
69
70         return {
71             'id': video_id,
72             'url': video_url,
73             'title': title,
74             'description': description,
75             'thumbnail': thumbnail,
76             'uploader': uploader,
77             'format_id': format_id,
78             'view_count': view_count,
79             'like_count': like_count,
80             'dislike_count': dislike_count,
81             'comment_count': comment_count,
82             'age_limit': 18,
83         }