ebc8c1f4f11544fc2110aadfb1fbf5876502f3bc
[youtube-dl] / youtube_dl / extractor / tube8.py
1 import os
2 import re
3
4 from .common import InfoExtractor
5 from ..utils import (
6     compat_urllib_parse_urlparse,
7     compat_urllib_request,
8     compat_urllib_parse,
9     unescapeHTML,
10 )
11 from ..aes import (
12     aes_decrypt_text
13 )
14
15 class Tube8IE(InfoExtractor):
16     _VALID_URL = r'^(?:https?://)?(?:www\.)?(?P<url>tube8\.com/[^/]+/[^/]+/(?P<videoid>[0-9]+)/?)'
17     _TEST = {
18         u'url': u'http://www.tube8.com/teen/kasia-music-video/229795/',
19         u'file': u'229795.mp4',
20         u'md5': u'e9e0b0c86734e5e3766e653509475db0',
21         u'info_dict': {
22             u"description": u"hot teen Kasia grinding", 
23             u"uploader": u"unknown", 
24             u"title": u"Kasia music video",
25         }
26     }
27
28     def _real_extract(self, url):
29         mobj = re.match(self._VALID_URL, url)
30         video_id = mobj.group('videoid')
31         url = 'http://www.' + mobj.group('url')
32
33         req = compat_urllib_request.Request(url)
34         req.add_header('Cookie', 'age_verified=1')
35         webpage = self._download_webpage(req, video_id)
36
37         video_title = self._html_search_regex(r'videotitle      ="([^"]+)', webpage, u'title')
38         video_description = self._html_search_regex(r'>Description:</strong>(.+?)<', webpage, u'description', fatal=False)
39         video_uploader = self._html_search_regex(r'>Submitted by:</strong>(?:\s|<[^>]*>)*(.+?)<', webpage, u'uploader', fatal=False)
40         thumbnail = self._html_search_regex(r'"image_url":"([^"]+)', webpage, u'thumbnail', fatal=False)
41         if thumbnail:
42             thumbnail = thumbnail.replace('\\/', '/')
43
44         video_url = self._html_search_regex(r'"video_url":"([^"]+)', webpage, u'video_url')
45         if webpage.find('"encrypted":true')!=-1:
46             password = self._html_search_regex(r'"video_title":"([^"]+)', webpage, u'password')
47             video_url = aes_decrypt_text(video_url, password, 32).decode('utf-8')
48         path = compat_urllib_parse_urlparse( video_url ).path
49         extension = os.path.splitext( path )[1][1:]
50         format = path.split('/')[4].split('_')[:2]
51         format = "-".join( format )
52
53         return {
54             'id': video_id,
55             'uploader': video_uploader,
56             'title': video_title,
57             'thumbnail': thumbnail,
58             'description': video_description,
59             'url': video_url,
60             'ext': extension,
61             'format': format,
62             'format_id': format,
63         }