]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/spankbang.py
debian/control: Mark my GitHub trees as the preferred repos.
[youtubedl] / youtube_dl / extractor / spankbang.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7 ExtractorError,
8 merge_dicts,
9 orderedSet,
10 parse_duration,
11 parse_resolution,
12 str_to_int,
13 url_or_none,
14 urlencode_postdata,
15 )
16
17
18 class SpankBangIE(InfoExtractor):
19 _VALID_URL = r'https?://(?:[^/]+\.)?spankbang\.com/(?P<id>[\da-z]+)/(?:video|play|embed)\b'
20 _TESTS = [{
21 'url': 'http://spankbang.com/3vvn/video/fantasy+solo',
22 'md5': '1cc433e1d6aa14bc376535b8679302f7',
23 'info_dict': {
24 'id': '3vvn',
25 'ext': 'mp4',
26 'title': 'fantasy solo',
27 'description': 'dillion harper masturbates on a bed',
28 'thumbnail': r're:^https?://.*\.jpg$',
29 'uploader': 'silly2587',
30 'timestamp': 1422571989,
31 'upload_date': '20150129',
32 'age_limit': 18,
33 }
34 }, {
35 # 480p only
36 'url': 'http://spankbang.com/1vt0/video/solvane+gangbang',
37 'only_matching': True,
38 }, {
39 # no uploader
40 'url': 'http://spankbang.com/lklg/video/sex+with+anyone+wedding+edition+2',
41 'only_matching': True,
42 }, {
43 # mobile page
44 'url': 'http://m.spankbang.com/1o2de/video/can+t+remember+her+name',
45 'only_matching': True,
46 }, {
47 # 4k
48 'url': 'https://spankbang.com/1vwqx/video/jade+kush+solo+4k',
49 'only_matching': True,
50 }, {
51 'url': 'https://m.spankbang.com/3vvn/play/fantasy+solo/480p/',
52 'only_matching': True,
53 }, {
54 'url': 'https://m.spankbang.com/3vvn/play',
55 'only_matching': True,
56 }, {
57 'url': 'https://spankbang.com/2y3td/embed/',
58 'only_matching': True,
59 }]
60
61 def _real_extract(self, url):
62 video_id = self._match_id(url)
63 webpage = self._download_webpage(
64 url.replace('/%s/embed' % video_id, '/%s/video' % video_id),
65 video_id, headers={'Cookie': 'country=US'})
66
67 if re.search(r'<[^>]+\bid=["\']video_removed', webpage):
68 raise ExtractorError(
69 'Video %s is not available' % video_id, expected=True)
70
71 formats = []
72
73 def extract_format(format_id, format_url):
74 f_url = url_or_none(format_url)
75 if not f_url:
76 return
77 f = parse_resolution(format_id)
78 f.update({
79 'url': f_url,
80 'format_id': format_id,
81 })
82 formats.append(f)
83
84 STREAM_URL_PREFIX = 'stream_url_'
85
86 for mobj in re.finditer(
87 r'%s(?P<id>[^\s=]+)\s*=\s*(["\'])(?P<url>(?:(?!\2).)+)\2'
88 % STREAM_URL_PREFIX, webpage):
89 extract_format(mobj.group('id', 'url'))
90
91 if not formats:
92 stream_key = self._search_regex(
93 r'data-streamkey\s*=\s*(["\'])(?P<value>(?:(?!\1).)+)\1',
94 webpage, 'stream key', group='value')
95
96 sb_csrf_session = self._get_cookies(
97 'https://spankbang.com')['sb_csrf_session'].value
98
99 stream = self._download_json(
100 'https://spankbang.com/api/videos/stream', video_id,
101 'Downloading stream JSON', data=urlencode_postdata({
102 'id': stream_key,
103 'data': 0,
104 'sb_csrf_session': sb_csrf_session,
105 }), headers={
106 'Referer': url,
107 'X-CSRFToken': sb_csrf_session,
108 })
109
110 for format_id, format_url in stream.items():
111 if format_id.startswith(STREAM_URL_PREFIX):
112 if format_url and isinstance(format_url, list):
113 format_url = format_url[0]
114 extract_format(
115 format_id[len(STREAM_URL_PREFIX):], format_url)
116
117 self._sort_formats(formats)
118
119 info = self._search_json_ld(webpage, video_id, default={})
120
121 title = self._html_search_regex(
122 r'(?s)<h1[^>]*>(.+?)</h1>', webpage, 'title', default=None)
123 description = self._search_regex(
124 r'<div[^>]+\bclass=["\']bottom[^>]+>\s*<p>[^<]*</p>\s*<p>([^<]+)',
125 webpage, 'description', default=None)
126 thumbnail = self._og_search_thumbnail(webpage, default=None)
127 uploader = self._html_search_regex(
128 (r'(?s)<li[^>]+class=["\']profile[^>]+>(.+?)</a>',
129 r'class="user"[^>]*><img[^>]+>([^<]+)'),
130 webpage, 'uploader', default=None)
131 duration = parse_duration(self._search_regex(
132 r'<div[^>]+\bclass=["\']right_side[^>]+>\s*<span>([^<]+)',
133 webpage, 'duration', default=None))
134 view_count = str_to_int(self._search_regex(
135 r'([\d,.]+)\s+plays', webpage, 'view count', default=None))
136
137 age_limit = self._rta_search(webpage)
138
139 return merge_dicts({
140 'id': video_id,
141 'title': title or video_id,
142 'description': description,
143 'thumbnail': thumbnail,
144 'uploader': uploader,
145 'duration': duration,
146 'view_count': view_count,
147 'formats': formats,
148 'age_limit': age_limit,
149 }, info
150 )
151
152
153 class SpankBangPlaylistIE(InfoExtractor):
154 _VALID_URL = r'https?://(?:[^/]+\.)?spankbang\.com/(?P<id>[\da-z]+)/playlist/[^/]+'
155 _TEST = {
156 'url': 'https://spankbang.com/ug0k/playlist/big+ass+titties',
157 'info_dict': {
158 'id': 'ug0k',
159 'title': 'Big Ass Titties',
160 },
161 'playlist_mincount': 50,
162 }
163
164 def _real_extract(self, url):
165 playlist_id = self._match_id(url)
166
167 webpage = self._download_webpage(
168 url, playlist_id, headers={'Cookie': 'country=US; mobile=on'})
169
170 entries = [self.url_result(
171 'https://spankbang.com/%s/video' % video_id,
172 ie=SpankBangIE.ie_key(), video_id=video_id)
173 for video_id in orderedSet(re.findall(
174 r'<a[^>]+\bhref=["\']/?([\da-z]+)/play/', webpage))]
175
176 title = self._html_search_regex(
177 r'<h1>([^<]+)\s+playlist</h1>', webpage, 'playlist title',
178 fatal=False)
179
180 return self.playlist_result(entries, playlist_id, title)