[spankwire] Fix extraction (closes #18924, closes #20648)
[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 ..utils import (
7     float_or_none,
8     int_or_none,
9     merge_dicts,
10     str_or_none,
11     str_to_int,
12     url_or_none,
13 )
14
15
16 class SpankwireIE(InfoExtractor):
17     _VALID_URL = r'''(?x)
18                     https?://
19                         (?:www\.)?spankwire\.com/
20                         (?:
21                             [^/]+/video|
22                             EmbedPlayer\.aspx/?\?.*?\bArticleId=
23                         )
24                         (?P<id>\d+)
25                     '''
26     _TESTS = [{
27         # download URL pattern: */<height>P_<tbr>K_<video_id>.mp4
28         'url': 'http://www.spankwire.com/Buckcherry-s-X-Rated-Music-Video-Crazy-Bitch/video103545/',
29         'md5': '5aa0e4feef20aad82cbcae3aed7ab7cd',
30         'info_dict': {
31             'id': '103545',
32             'ext': 'mp4',
33             'title': 'Buckcherry`s X Rated Music Video Crazy Bitch',
34             'description': 'Crazy Bitch X rated music video.',
35             'duration': 222,
36             'uploader': 'oreusz',
37             'uploader_id': '124697',
38             'timestamp': 1178587885,
39             'upload_date': '20070508',
40             'average_rating': float,
41             'view_count': int,
42             'comment_count': int,
43             'age_limit': 18,
44             'categories': list,
45             'tags': list,
46         },
47     }, {
48         # download URL pattern: */mp4_<format_id>_<video_id>.mp4
49         'url': 'http://www.spankwire.com/Titcums-Compiloation-I/video1921551/',
50         'md5': '09b3c20833308b736ae8902db2f8d7e6',
51         'info_dict': {
52             'id': '1921551',
53             'ext': 'mp4',
54             'title': 'Titcums Compiloation I',
55             'description': 'cum on tits',
56             'uploader': 'dannyh78999',
57             'uploader_id': '3056053',
58             'upload_date': '20150822',
59             'age_limit': 18,
60         },
61         'params': {
62             'proxy': '127.0.0.1:8118'
63         },
64         'skip': 'removed',
65     }, {
66         'url': 'https://www.spankwire.com/EmbedPlayer.aspx/?ArticleId=156156&autostart=true',
67         'only_matching': True,
68     }]
69
70     def _real_extract(self, url):
71         video_id = self._match_id(url)
72
73         video = self._download_json(
74             'https://www.spankwire.com/api/video/%s.json' % video_id, video_id)
75
76         title = video['title']
77
78         formats = []
79         videos = video.get('videos')
80         if isinstance(videos, dict):
81             for format_id, format_url in videos.items():
82                 video_url = url_or_none(format_url)
83                 if not format_url:
84                     continue
85                 height = int_or_none(self._search_regex(
86                     r'(\d+)[pP]', format_id, 'height', default=None))
87                 m = re.search(
88                     r'/(?P<height>\d+)[pP]_(?P<tbr>\d+)[kK]', video_url)
89                 if m:
90                     tbr = int(m.group('tbr'))
91                     height = height or int(m.group('height'))
92                 else:
93                     tbr = None
94                 formats.append({
95                     'url': video_url,
96                     'format_id': '%dp' % height if height else format_id,
97                     'height': height,
98                     'tbr': tbr,
99                 })
100         m3u8_url = url_or_none(video.get('HLS'))
101         if m3u8_url:
102             formats.extend(self._extract_m3u8_formats(
103                 m3u8_url, video_id, 'mp4', entry_protocol='m3u8_native',
104                 m3u8_id='hls', fatal=False))
105         self._sort_formats(formats, ('height', 'tbr', 'width', 'format_id'))
106
107         view_count = str_to_int(video.get('viewed'))
108
109         thumbnails = []
110         for preference, t in enumerate(('', '2x'), start=0):
111             thumbnail_url = url_or_none(video.get('poster%s' % t))
112             if not thumbnail_url:
113                 continue
114             thumbnails.append({
115                 'url': thumbnail_url,
116                 'preference': preference,
117             })
118
119         def extract_names(key):
120             entries_list = video.get(key)
121             if not isinstance(entries_list, list):
122                 return
123             entries = []
124             for entry in entries_list:
125                 name = str_or_none(entry.get('name'))
126                 if name:
127                     entries.append(name)
128             return entries
129
130         categories = extract_names('categories')
131         tags = extract_names('tags')
132
133         uploader = None
134         info = {}
135
136         webpage = self._download_webpage(
137             'https://www.spankwire.com/_/video%s/' % video_id, video_id,
138             fatal=False)
139         if webpage:
140             info = self._search_json_ld(webpage, video_id, default={})
141             thumbnail_url = None
142             if 'thumbnail' in info:
143                 thumbnail_url = url_or_none(info['thumbnail'])
144                 del info['thumbnail']
145             if not thumbnail_url:
146                 thumbnail_url = self._og_search_thumbnail(webpage)
147             if thumbnail_url:
148                 thumbnails.append({
149                     'url': thumbnail_url,
150                     'preference': 10,
151                 })
152             uploader = self._html_search_regex(
153                 r'(?s)by\s*<a[^>]+\bclass=["\']uploaded__by[^>]*>(.+?)</a>',
154                 webpage, 'uploader', fatal=False)
155             if not view_count:
156                 view_count = str_to_int(self._search_regex(
157                     r'data-views=["\']([\d,.]+)', webpage, 'view count',
158                     fatal=False))
159
160         return merge_dicts({
161             'id': video_id,
162             'title': title,
163             'description': video.get('description'),
164             'duration': int_or_none(video.get('duration')),
165             'thumbnails': thumbnails,
166             'uploader': uploader,
167             'uploader_id': str_or_none(video.get('userId')),
168             'timestamp': int_or_none(video.get('time_approved_on')),
169             'average_rating': float_or_none(video.get('rating')),
170             'view_count': view_count,
171             'comment_count': int_or_none(video.get('comments')),
172             'age_limit': 18,
173             'categories': categories,
174             'tags': tags,
175             'formats': formats,
176         }, info)