logo

youtube-dl

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

hearthisat.py (5244B)


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