[spankwire] Simplify and properly format
[youtube-dl] / youtube_dl / extractor / spankwire.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import (
7     compat_urllib_parse_unquote,
8     compat_urllib_parse_urlparse,
9     compat_urllib_request,
10 )
11 from ..utils import (
12     str_to_int,
13     unified_strdate,
14 )
15 from ..aes import aes_decrypt_text
16
17
18 class SpankwireIE(InfoExtractor):
19     _VALID_URL = r'https?://(?:www\.)?(?P<url>spankwire\.com/[^/]*/video(?P<videoid>[0-9]+)/?)'
20     _TESTS = [{
21         # download URL pattern: */<height>P_<tbr>K_<video_id>.mp4
22         'url': 'http://www.spankwire.com/Buckcherry-s-X-Rated-Music-Video-Crazy-Bitch/video103545/',
23         'md5': '8bbfde12b101204b39e4b9fe7eb67095',
24         'info_dict': {
25             'id': '103545',
26             'ext': 'mp4',
27             'title': 'Buckcherry`s X Rated Music Video Crazy Bitch',
28             'description': 'Crazy Bitch X rated music video.',
29             'uploader': 'oreusz',
30             'uploader_id': '124697',
31             'upload_date': '20070507',
32             'age_limit': 18,
33         }
34     }, {
35         # download URL pattern: */mp4_<format_id>_<video_id>.mp4
36         'url': 'http://www.spankwire.com/Titcums-Compiloation-I/video1921551/',
37         'md5': '09b3c20833308b736ae8902db2f8d7e6',
38         'info_dict': {
39             'id': '1921551',
40             'ext': 'mp4',
41             'title': 'Titcums Compiloation I',
42             'description': 'cum on tits',
43             'uploader': 'dannyh78999',
44             'uploader_id': '3056053',
45             'upload_date': '20150822',
46             'age_limit': 18,
47         },
48     }]
49
50     def _real_extract(self, url):
51         mobj = re.match(self._VALID_URL, url)
52         video_id = mobj.group('videoid')
53         url = 'http://www.' + mobj.group('url')
54
55         req = compat_urllib_request.Request(url)
56         req.add_header('Cookie', 'age_verified=1')
57         webpage = self._download_webpage(req, video_id)
58
59         title = self._html_search_regex(
60             r'<h1>([^<]+)', webpage, 'title')
61         description = self._html_search_regex(
62             r'(?s)<div\s+id="descriptionContent">(.+?)</div>',
63             webpage, 'description', fatal=False)
64         thumbnail = self._html_search_regex(
65             r'playerData\.screenShot\s*=\s*["\']([^"\']+)["\']',
66             webpage, 'thumbnail', fatal=False)
67
68         uploader = self._html_search_regex(
69             r'by:\s*<a [^>]*>(.+?)</a>',
70             webpage, 'uploader', fatal=False)
71         uploader_id = self._html_search_regex(
72             r'by:\s*<a href="/user/viewProfile\?.*?UserId=(\d+).*?"',
73             webpage, 'uploader id', fatal=False)
74         upload_date = unified_strdate(self._html_search_regex(
75             r'</a> on (.+?) at \d+:\d+',
76             webpage, 'upload date', fatal=False))
77
78         view_count = str_to_int(self._html_search_regex(
79             r'<div id="viewsCounter"><span>([\d,\.]+)</span> views</div>',
80             webpage, 'view count', fatal=False))
81         comment_count = str_to_int(self._html_search_regex(
82             r'<span\s+id="spCommentCount"[^>]*>([\d,\.]+)</span>',
83             webpage, 'comment count', fatal=False))
84
85         videos = re.findall(
86             r'playerData\.cdnPath([0-9]{3,})\s*=\s*(?:encodeURIComponent\()?["\']([^"\']+)["\']', webpage)
87         heights = [int(video[0]) for video in videos]
88         video_urls = list(map(compat_urllib_parse_unquote, [video[1] for video in videos]))
89         if webpage.find('flashvars\.encrypted = "true"') != -1:
90             password = self._search_regex(
91                 r'flashvars\.video_title = "([^"]+)',
92                 webpage, 'password').replace('+', ' ')
93             video_urls = list(map(
94                 lambda s: aes_decrypt_text(s, password, 32).decode('utf-8'),
95                 video_urls))
96
97         formats = []
98         for height, video_url in zip(heights, video_urls):
99             path = compat_urllib_parse_urlparse(video_url).path
100             _, quality = path.split('/')[4].split('_')[:2]
101             f = {
102                 'url': video_url,
103                 'height': height,
104             }
105             tbr = self._search_regex(r'^(\d+)[Kk]$', quality, 'tbr', default=None)
106             if tbr:
107                 f.update({
108                     'tbr': int(tbr),
109                     'format_id': '%dp' % height,
110                 })
111             else:
112                 f['format_id'] = quality
113             formats.append(f)
114         self._sort_formats(formats)
115
116         age_limit = self._rta_search(webpage)
117
118         return {
119             'id': video_id,
120             'title': title,
121             'description': description,
122             'thumbnail': thumbnail,
123             'uploader': uploader,
124             'uploader_id': uploader_id,
125             'upload_date': upload_date,
126             'view_count': view_count,
127             'comment_count': comment_count,
128             'formats': formats,
129             'age_limit': age_limit,
130         }