You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
4.2 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_urllib_request,
  7. compat_urlparse,
  8. )
  9. from ..utils import (
  10. HEADRequest,
  11. str_to_int,
  12. urlencode_postdata,
  13. urlhandle_detect_ext,
  14. )
  15. class HearThisAtIE(InfoExtractor):
  16. _VALID_URL = r'https?://(?:www\.)?hearthis\.at/(?P<artist>[^/]+)/(?P<title>[A-Za-z0-9\-]+)/?$'
  17. _PLAYLIST_URL = 'https://hearthis.at/playlist.php'
  18. _TEST = {
  19. 'url': 'https://hearthis.at/moofi/dr-kreep',
  20. 'md5': 'ab6ec33c8fed6556029337c7885eb4e0',
  21. 'info_dict': {
  22. 'id': '150939',
  23. 'ext': 'wav',
  24. 'title': 'Moofi - Dr. Kreep',
  25. 'thumbnail': 're:^https?://.*\.jpg$',
  26. 'timestamp': 1421564134,
  27. 'description': 'Creepy Patch. Mutable Instruments Braids Vowel + Formant Mode.',
  28. 'upload_date': '20150118',
  29. 'comment_count': int,
  30. 'view_count': int,
  31. 'like_count': int,
  32. 'duration': 71,
  33. 'categories': ['Experimental'],
  34. }
  35. }
  36. def _real_extract(self, url):
  37. m = re.match(self._VALID_URL, url)
  38. display_id = '{artist:s} - {title:s}'.format(**m.groupdict())
  39. webpage = self._download_webpage(url, display_id)
  40. track_id = self._search_regex(
  41. r'intTrackId\s*=\s*(\d+)', webpage, 'track ID')
  42. payload = urlencode_postdata({'tracks[]': track_id})
  43. req = compat_urllib_request.Request(self._PLAYLIST_URL, payload)
  44. req.add_header('Content-type', 'application/x-www-form-urlencoded')
  45. track = self._download_json(req, track_id, 'Downloading playlist')[0]
  46. title = '{artist:s} - {title:s}'.format(**track)
  47. categories = None
  48. if track.get('category'):
  49. categories = [track['category']]
  50. description = self._og_search_description(webpage)
  51. thumbnail = self._og_search_thumbnail(webpage)
  52. meta_span = r'<span[^>]+class="%s".*?</i>([^<]+)</span>'
  53. view_count = str_to_int(self._search_regex(
  54. meta_span % 'plays_count', webpage, 'view count', fatal=False))
  55. like_count = str_to_int(self._search_regex(
  56. meta_span % 'likes_count', webpage, 'like count', fatal=False))
  57. comment_count = str_to_int(self._search_regex(
  58. meta_span % 'comment_count', webpage, 'comment count', fatal=False))
  59. duration = str_to_int(self._search_regex(
  60. r'data-length="(\d+)', webpage, 'duration', fatal=False))
  61. timestamp = str_to_int(self._search_regex(
  62. r'<span[^>]+class="calctime"[^>]+data-time="(\d+)', webpage, 'timestamp', fatal=False))
  63. formats = []
  64. mp3_url = self._search_regex(
  65. r'(?s)<a class="player-link"\s+(?:[a-zA-Z0-9_:-]+="[^"]+"\s+)*?data-mp3="([^"]+)"',
  66. webpage, 'mp3 URL', fatal=False)
  67. if mp3_url:
  68. formats.append({
  69. 'format_id': 'mp3',
  70. 'vcodec': 'none',
  71. 'acodec': 'mp3',
  72. 'url': mp3_url,
  73. })
  74. download_path = self._search_regex(
  75. r'<a class="[^"]*download_fct[^"]*"\s+href="([^"]+)"',
  76. webpage, 'download URL', default=None)
  77. if download_path:
  78. download_url = compat_urlparse.urljoin(url, download_path)
  79. ext_req = HEADRequest(download_url)
  80. ext_handle = self._request_webpage(
  81. ext_req, display_id, note='Determining extension')
  82. ext = urlhandle_detect_ext(ext_handle)
  83. formats.append({
  84. 'format_id': 'download',
  85. 'vcodec': 'none',
  86. 'ext': ext,
  87. 'url': download_url,
  88. 'preference': 2, # Usually better quality
  89. })
  90. self._sort_formats(formats)
  91. return {
  92. 'id': track_id,
  93. 'display_id': display_id,
  94. 'title': title,
  95. 'formats': formats,
  96. 'thumbnail': thumbnail,
  97. 'description': description,
  98. 'duration': duration,
  99. 'timestamp': timestamp,
  100. 'view_count': view_count,
  101. 'comment_count': comment_count,
  102. 'like_count': like_count,
  103. 'categories': categories,
  104. }