logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

samplefocus.py (3874B)


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