[spankwire] Support new cdn video url 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                 'url': 'http://www.spankwire.com/Buckcherry-s-X-Rated-Music-Video-Crazy-Bitch/video103545/',
22                 'md5': '8bbfde12b101204b39e4b9fe7eb67095',
23                     'info_dict': {
24                                     'id': '103545',
25                                     'ext': 'mp4',
26                                     'title': 'Buckcherry`s X Rated Music Video Crazy Bitch',
27                                     'description': 'Crazy Bitch X rated music video.',
28                                     'uploader': 'oreusz',
29                                     'uploader_id': '124697',
30                                     'upload_date': '20070507',
31                                     'age_limit': 18,
32                     }
33               },
34               {
35                 'url': 'http://www.spankwire.com/Titcums-Compiloation-I/video1921551/',
36                 'md5': '09b3c20833308b736ae8902db2f8d7e6',
37                     'info_dict': {
38                                     'id': '1921551',
39                                     'ext': 'mp4',
40                                     'title': 'Titcums Compiloation I',
41                                     'description': 'cum on tits',
42                                     'uploader': 'dannyh78999',
43                                     'uploader_id': '3056053',
44                                     'upload_date': '20150822',
45                                     'age_limit': 18,
46                     }
47               }]
48
49     def _real_extract(self, url):
50         mobj = re.match(self._VALID_URL, url)
51         video_id = mobj.group('videoid')
52         url = 'http://www.' + mobj.group('url')
53
54         req = compat_urllib_request.Request(url)
55         req.add_header('Cookie', 'age_verified=1')
56         webpage = self._download_webpage(req, video_id)
57
58         title = self._html_search_regex(
59             r'<h1>([^<]+)', webpage, 'title')
60         description = self._html_search_regex(
61             r'(?s)<div\s+id="descriptionContent">(.+?)</div>',
62             webpage, 'description', fatal=False)
63         thumbnail = self._html_search_regex(
64             r'playerData\.screenShot\s*=\s*["\']([^"\']+)["\']',
65             webpage, 'thumbnail', fatal=False)
66
67         uploader = self._html_search_regex(
68             r'by:\s*<a [^>]*>(.+?)</a>',
69             webpage, 'uploader', fatal=False)
70         uploader_id = self._html_search_regex(
71             r'by:\s*<a href="/user/viewProfile\?.*?UserId=(\d+).*?"',
72             webpage, 'uploader id', fatal=False)
73         upload_date = unified_strdate(self._html_search_regex(
74             r'</a> on (.+?) at \d+:\d+',
75             webpage, 'upload date', fatal=False))
76
77         view_count = str_to_int(self._html_search_regex(
78             r'<div id="viewsCounter"><span>([\d,\.]+)</span> views</div>',
79             webpage, 'view count', fatal=False))
80         comment_count = str_to_int(self._html_search_regex(
81             r'<span\s+id="spCommentCount"[^>]*>([\d,\.]+)</span>',
82             webpage, 'comment count', fatal=False))
83
84         video_urls = list(map(
85             compat_urllib_parse_unquote,
86             re.findall(r'playerData\.cdnPath[0-9]{3,}\s*=\s*(?:encodeURIComponent\()?["\']([^"\']+)["\']', webpage)))
87         if webpage.find('flashvars\.encrypted = "true"') != -1:
88             password = self._search_regex(
89                 r'flashvars\.video_title = "([^"]+)',
90                 webpage, 'password').replace('+', ' ')
91             video_urls = list(map(
92                 lambda s: aes_decrypt_text(s, password, 32).decode('utf-8'),
93                 video_urls))
94
95         formats = []
96         for video_url in video_urls:
97             path = compat_urllib_parse_urlparse(video_url).path
98             format = path.split('/')[4].split('_')[:2]
99             if format[0] == 'mp4':
100                 format_id, quality = format
101                 format = "-".join(format)
102                 if quality == 'normal':
103                     height = 180
104                 elif quality == 'high':
105                     height = 240
106                 elif quality == 'ultra':
107                     height = 480
108                 elif quality == '720p':
109                     height = 720
110                 formats.append({
111                     'url': video_url,
112                     'format': format,
113                     'height': height,
114                     'format_id': format,
115                 })
116             else:
117                 resolution, bitrate_str = format
118                 format = "-".join(format)
119                 height = int(resolution.rstrip('Pp'))
120                 tbr = int(bitrate_str.rstrip('Kk'))
121                 formats.append({
122                     'url': video_url,
123                     'resolution': resolution,
124                     'format': format,
125                     'tbr': tbr,
126                     'height': height,
127                     'format_id': format,
128                 })
129         self._sort_formats(formats)
130
131         age_limit = self._rta_search(webpage)
132
133         return {
134             'id': video_id,
135             'title': title,
136             'description': description,
137             'thumbnail': thumbnail,
138             'uploader': uploader,
139             'uploader_id': uploader_id,
140             'upload_date': upload_date,
141             'view_count': view_count,
142             'comment_count': comment_count,
143             'formats': formats,
144             'age_limit': age_limit,
145         }