logo

youtube-dl

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

audiomack.py (6055B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import itertools
  4. import time
  5. from .common import InfoExtractor
  6. from .soundcloud import SoundcloudIE
  7. from ..compat import compat_str
  8. from ..utils import (
  9. ExtractorError,
  10. url_basename,
  11. )
  12. class AudiomackIE(InfoExtractor):
  13. _VALID_URL = r'https?://(?:www\.)?audiomack\.com/(?:song/|(?=.+/song/))(?P<id>[\w/-]+)'
  14. IE_NAME = 'audiomack'
  15. _TESTS = [
  16. # hosted on audiomack
  17. {
  18. 'url': 'http://www.audiomack.com/song/roosh-williams/extraordinary',
  19. 'info_dict':
  20. {
  21. 'id': '310086',
  22. 'ext': 'mp3',
  23. 'uploader': 'Roosh Williams',
  24. 'title': 'Extraordinary'
  25. }
  26. },
  27. # audiomack wrapper around soundcloud song
  28. # Needs new test URL.
  29. {
  30. 'add_ie': ['Soundcloud'],
  31. 'url': 'http://www.audiomack.com/song/hip-hop-daily/black-mamba-freestyle',
  32. 'only_matching': True,
  33. # 'info_dict': {
  34. # 'id': '258901379',
  35. # 'ext': 'mp3',
  36. # 'description': 'mamba day freestyle for the legend Kobe Bryant ',
  37. # 'title': 'Black Mamba Freestyle [Prod. By Danny Wolf]',
  38. # 'uploader': 'ILOVEMAKONNEN',
  39. # 'upload_date': '20160414',
  40. # }
  41. },
  42. ]
  43. def _real_extract(self, url):
  44. # URLs end with [uploader name]/song/[uploader title]
  45. # this title is whatever the user types in, and is rarely
  46. # the proper song title. Real metadata is in the api response
  47. album_url_tag = self._match_id(url).replace('/song/', '/')
  48. # Request the extended version of the api for extra fields like artist and title
  49. api_response = self._download_json(
  50. 'http://www.audiomack.com/api/music/url/song/%s?extended=1&_=%d' % (
  51. album_url_tag, time.time()),
  52. album_url_tag)
  53. # API is inconsistent with errors
  54. if 'url' not in api_response or not api_response['url'] or 'error' in api_response:
  55. raise ExtractorError('Invalid url %s' % url)
  56. # Audiomack wraps a lot of soundcloud tracks in their branded wrapper
  57. # if so, pass the work off to the soundcloud extractor
  58. if SoundcloudIE.suitable(api_response['url']):
  59. return self.url_result(api_response['url'], SoundcloudIE.ie_key())
  60. return {
  61. 'id': compat_str(api_response.get('id', album_url_tag)),
  62. 'uploader': api_response.get('artist'),
  63. 'title': api_response.get('title'),
  64. 'url': api_response['url'],
  65. }
  66. class AudiomackAlbumIE(InfoExtractor):
  67. _VALID_URL = r'https?://(?:www\.)?audiomack\.com/(?:album/|(?=.+/album/))(?P<id>[\w/-]+)'
  68. IE_NAME = 'audiomack:album'
  69. _TESTS = [
  70. # Standard album playlist
  71. {
  72. 'url': 'http://www.audiomack.com/album/flytunezcom/tha-tour-part-2-mixtape',
  73. 'playlist_count': 11,
  74. 'info_dict':
  75. {
  76. 'id': '812251',
  77. 'title': 'Tha Tour: Part 2 (Official Mixtape)'
  78. }
  79. },
  80. # Album playlist ripped from fakeshoredrive with no metadata
  81. {
  82. 'url': 'http://www.audiomack.com/album/fakeshoredrive/ppp-pistol-p-project',
  83. 'info_dict': {
  84. 'title': 'PPP (Pistol P Project)',
  85. 'id': '837572',
  86. },
  87. 'playlist': [{
  88. 'info_dict': {
  89. 'title': 'PPP (Pistol P Project) - 10. 4 Minutes Of Hell Part 4 (prod by DY OF 808 MAFIA)',
  90. 'id': '837580',
  91. 'ext': 'mp3',
  92. 'uploader': 'Lil Herb a.k.a. G Herbo',
  93. }
  94. }],
  95. 'params': {
  96. 'playliststart': 2,
  97. 'playlistend': 2,
  98. }
  99. }
  100. ]
  101. def _real_extract(self, url):
  102. # URLs end with [uploader name]/album/[uploader title]
  103. # this title is whatever the user types in, and is rarely
  104. # the proper song title. Real metadata is in the api response
  105. album_url_tag = self._match_id(url).replace('/album/', '/')
  106. result = {'_type': 'playlist', 'entries': []}
  107. # There is no one endpoint for album metadata - instead it is included/repeated in each song's metadata
  108. # Therefore we don't know how many songs the album has and must infi-loop until failure
  109. for track_no in itertools.count():
  110. # Get song's metadata
  111. api_response = self._download_json(
  112. 'http://www.audiomack.com/api/music/url/album/%s/%d?extended=1&_=%d'
  113. % (album_url_tag, track_no, time.time()), album_url_tag,
  114. note='Querying song information (%d)' % (track_no + 1))
  115. # Total failure, only occurs when url is totally wrong
  116. # Won't happen in middle of valid playlist (next case)
  117. if 'url' not in api_response or 'error' in api_response:
  118. raise ExtractorError('Invalid url for track %d of album url %s' % (track_no, url))
  119. # URL is good but song id doesn't exist - usually means end of playlist
  120. elif not api_response['url']:
  121. break
  122. else:
  123. # Pull out the album metadata and add to result (if it exists)
  124. for resultkey, apikey in [('id', 'album_id'), ('title', 'album_title')]:
  125. if apikey in api_response and resultkey not in result:
  126. result[resultkey] = compat_str(api_response[apikey])
  127. song_id = url_basename(api_response['url']).rpartition('.')[0]
  128. result['entries'].append({
  129. 'id': compat_str(api_response.get('id', song_id)),
  130. 'uploader': api_response.get('artist'),
  131. 'title': api_response.get('title', song_id),
  132. 'url': api_response['url'],
  133. })
  134. return result