[ie/wistia] Support password-protected videos (#11100)
[yt-dlp3.git] / yt_dlp / extractor / samplefocus.py
blob3db3ce14246036349f80e76294aaa0fb26ef2467
1 import re
3 from .common import InfoExtractor
4 from ..utils import (
5 extract_attributes,
6 get_element_by_attribute,
7 int_or_none,
11 class SampleFocusIE(InfoExtractor):
12 _VALID_URL = r'https?://(?:www\.)?samplefocus\.com/samples/(?P<id>[^/?&#]+)'
13 _TESTS = [{
14 'url': 'https://samplefocus.com/samples/lil-peep-sad-emo-guitar',
15 'md5': '48c8d62d60be467293912e0e619a5120',
16 'info_dict': {
17 'id': '40316',
18 'display_id': 'lil-peep-sad-emo-guitar',
19 'ext': 'mp3',
20 'title': 'Lil Peep Sad Emo Guitar',
21 'thumbnail': r're:^https?://.+\.png',
22 'license': 'Standard License',
23 'uploader': 'CapsCtrl',
24 'uploader_id': 'capsctrl',
25 'like_count': int,
26 'comment_count': int,
27 'categories': ['Samples', 'Guitar', 'Electric guitar'],
29 }, {
30 'url': 'https://samplefocus.com/samples/dababy-style-bass-808',
31 'only_matching': True,
32 }, {
33 'url': 'https://samplefocus.com/samples/young-chop-kick',
34 'only_matching': True,
37 def _real_extract(self, url):
38 display_id = self._match_id(url)
39 webpage = self._download_webpage(url, display_id, impersonate=True)
41 sample_id = self._search_regex(
42 r'<input[^>]+id=(["\'])sample_id\1[^>]+value=(?:["\'])(?P<id>\d+)',
43 webpage, 'sample id', group='id')
45 title = self._og_search_title(webpage, fatal=False) or self._html_search_regex(
46 r'<h1>(.+?)</h1>', webpage, 'title')
48 mp3_url = self._search_regex(
49 r'<input[^>]+id=(["\'])sample_mp3\1[^>]+value=(["\'])(?P<url>(?:(?!\2).)+)',
50 webpage, 'mp3', fatal=False, group='url') or extract_attributes(self._search_regex(
51 r'<meta[^>]+itemprop=(["\'])contentUrl\1[^>]*>',
52 webpage, 'mp3 url', group=0))['content']
54 thumbnail = self._og_search_thumbnail(webpage) or self._html_search_regex(
55 r'<img[^>]+class=(?:["\'])waveform responsive-img[^>]+src=(["\'])(?P<url>(?:(?!\1).)+)',
56 webpage, 'mp3', fatal=False, group='url')
58 comments = []
59 for author_id, author, body in re.findall(r'(?s)<p[^>]+class="comment-author"><a[^>]+href="/users/([^"]+)">([^"]+)</a>.+?<p[^>]+class="comment-body">([^>]+)</p>', webpage):
60 comments.append({
61 'author': author,
62 'author_id': author_id,
63 'text': body,
66 uploader_id = uploader = None
67 mobj = re.search(r'>By <a[^>]+href="/users/([^"]+)"[^>]*>([^<]+)', webpage)
68 if mobj:
69 uploader_id, uploader = mobj.groups()
71 breadcrumb = get_element_by_attribute('typeof', 'BreadcrumbList', webpage)
72 categories = []
73 if breadcrumb:
74 for _, name in re.findall(r'<span[^>]+property=(["\'])name\1[^>]*>([^<]+)', breadcrumb):
75 categories.append(name)
77 def extract_count(klass):
78 return int_or_none(self._html_search_regex(
79 rf'<span[^>]+class=(?:["\'])?{klass}-count[^>]*>(\d+)',
80 webpage, klass, fatal=False))
82 return {
83 'id': sample_id,
84 'title': title,
85 'formats': [{
86 'url': mp3_url,
87 'ext': 'mp3',
88 'vcodec': 'none',
89 'acodec': 'mp3',
90 'http_headers': {
91 'Referer': url,
93 }],
94 'display_id': display_id,
95 'thumbnail': thumbnail,
96 'uploader': uploader,
97 'license': self._html_search_regex(
98 r'<a[^>]+href=(["\'])/license\1[^>]*>(?P<license>[^<]+)<',
99 webpage, 'license', fatal=False, group='license'),
100 'uploader_id': uploader_id,
101 'like_count': extract_count(f'sample-{sample_id}-favorites'),
102 'comment_count': extract_count('comments'),
103 'comments': comments,
104 'categories': categories,