]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/samplefocus.py
[ie/youtube] Suppress "Unavailable videos are hidden" warning (#10159)
[yt-dlp.git] / yt_dlp / extractor / samplefocus.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 extract_attributes,
6 get_element_by_attribute,
7 int_or_none,
8 )
9
10
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'],
28 },
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,
35 }]
36
37 def _real_extract(self, url):
38 display_id = self._match_id(url)
39 webpage = self._download_webpage(url, display_id)
40
41 sample_id = self._search_regex(
42 r'<input[^>]+id=(["\'])sample_id\1[^>]+value=(?:["\'])(?P<id>\d+)',
43 webpage, 'sample id', group='id')
44
45 title = self._og_search_title(webpage, fatal=False) or self._html_search_regex(
46 r'<h1>(.+?)</h1>', webpage, 'title')
47
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']
53
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')
57
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,
64 })
65
66 uploader_id = uploader = None
67 mobj = re.search(r'>By <a[^>]+href="/users/([^"]+)"[^>]*>([^<]+)', webpage)
68 if mobj:
69 uploader_id, uploader = mobj.groups()
70
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)
76
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))
81
82 return {
83 'id': sample_id,
84 'title': title,
85 'url': mp3_url,
86 'display_id': display_id,
87 'thumbnail': thumbnail,
88 'uploader': uploader,
89 'license': self._html_search_regex(
90 r'<a[^>]+href=(["\'])/license\1[^>]*>(?P<license>[^<]+)<',
91 webpage, 'license', fatal=False, group='license'),
92 'uploader_id': uploader_id,
93 'like_count': extract_count(f'sample-{sample_id}-favorites'),
94 'comment_count': extract_count('comments'),
95 'comments': comments,
96 'categories': categories,
97 }