samplefocus.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. extract_attributes,
  5. get_element_by_attribute,
  6. int_or_none,
  7. )
  8. class SampleFocusIE(InfoExtractor):
  9. _VALID_URL = r'https?://(?:www\.)?samplefocus\.com/samples/(?P<id>[^/?&#]+)'
  10. _TESTS = [{
  11. 'url': 'https://samplefocus.com/samples/lil-peep-sad-emo-guitar',
  12. 'md5': '48c8d62d60be467293912e0e619a5120',
  13. 'info_dict': {
  14. 'id': '40316',
  15. 'display_id': 'lil-peep-sad-emo-guitar',
  16. 'ext': 'mp3',
  17. 'title': 'Lil Peep Sad Emo Guitar',
  18. 'thumbnail': r're:^https?://.+\.png',
  19. 'license': 'Standard License',
  20. 'uploader': 'CapsCtrl',
  21. 'uploader_id': 'capsctrl',
  22. 'like_count': int,
  23. 'comment_count': int,
  24. 'categories': ['Samples', 'Guitar', 'Electric guitar'],
  25. },
  26. }, {
  27. 'url': 'https://samplefocus.com/samples/dababy-style-bass-808',
  28. 'only_matching': True,
  29. }, {
  30. 'url': 'https://samplefocus.com/samples/young-chop-kick',
  31. 'only_matching': True,
  32. }]
  33. def _real_extract(self, url):
  34. display_id = self._match_id(url)
  35. webpage = self._download_webpage(url, display_id)
  36. sample_id = self._search_regex(
  37. r'<input[^>]+id=(["\'])sample_id\1[^>]+value=(?:["\'])(?P<id>\d+)',
  38. webpage, 'sample id', group='id')
  39. title = self._og_search_title(webpage, fatal=False) or self._html_search_regex(
  40. r'<h1>(.+?)</h1>', webpage, 'title')
  41. mp3_url = self._search_regex(
  42. r'<input[^>]+id=(["\'])sample_mp3\1[^>]+value=(["\'])(?P<url>(?:(?!\2).)+)',
  43. webpage, 'mp3', fatal=False, group='url') or extract_attributes(self._search_regex(
  44. r'<meta[^>]+itemprop=(["\'])contentUrl\1[^>]*>',
  45. webpage, 'mp3 url', group=0))['content']
  46. thumbnail = self._og_search_thumbnail(webpage) or self._html_search_regex(
  47. r'<img[^>]+class=(?:["\'])waveform responsive-img[^>]+src=(["\'])(?P<url>(?:(?!\1).)+)',
  48. webpage, 'mp3', fatal=False, group='url')
  49. comments = []
  50. for author_id, author, body in re.findall(r'(?s)<p[^>]+class="comment-author"><a[^>]+href="/users/([^"]+)">([^"]+)</a>.+?<p[^>]+class="comment-body">([^>]+)</p>', webpage):
  51. comments.append({
  52. 'author': author,
  53. 'author_id': author_id,
  54. 'text': body,
  55. })
  56. uploader_id = uploader = None
  57. mobj = re.search(r'>By <a[^>]+href="/users/([^"]+)"[^>]*>([^<]+)', webpage)
  58. if mobj:
  59. uploader_id, uploader = mobj.groups()
  60. breadcrumb = get_element_by_attribute('typeof', 'BreadcrumbList', webpage)
  61. categories = []
  62. if breadcrumb:
  63. for _, name in re.findall(r'<span[^>]+property=(["\'])name\1[^>]*>([^<]+)', breadcrumb):
  64. categories.append(name)
  65. def extract_count(klass):
  66. return int_or_none(self._html_search_regex(
  67. rf'<span[^>]+class=(?:["\'])?{klass}-count[^>]*>(\d+)',
  68. webpage, klass, fatal=False))
  69. return {
  70. 'id': sample_id,
  71. 'title': title,
  72. 'url': mp3_url,
  73. 'display_id': display_id,
  74. 'thumbnail': thumbnail,
  75. 'uploader': uploader,
  76. 'license': self._html_search_regex(
  77. r'<a[^>]+href=(["\'])/license\1[^>]*>(?P<license>[^<]+)<',
  78. webpage, 'license', fatal=False, group='license'),
  79. 'uploader_id': uploader_id,
  80. 'like_count': extract_count(f'sample-{sample_id}-favorites'),
  81. 'comment_count': extract_count('comments'),
  82. 'comments': comments,
  83. 'categories': categories,
  84. }