audiomack.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import itertools
  2. import time
  3. from .common import InfoExtractor
  4. from .soundcloud import SoundcloudIE
  5. from ..utils import (
  6. ExtractorError,
  7. url_basename,
  8. )
  9. class AudiomackIE(InfoExtractor):
  10. _VALID_URL = r'https?://(?:www\.)?audiomack\.com/(?:song/|(?=.+/song/))(?P<id>[\w/-]+)'
  11. IE_NAME = 'audiomack'
  12. _TESTS = [
  13. # hosted on audiomack
  14. {
  15. 'url': 'http://www.audiomack.com/song/roosh-williams/extraordinary',
  16. 'info_dict':
  17. {
  18. 'id': '310086',
  19. 'ext': 'mp3',
  20. 'uploader': 'Roosh Williams',
  21. 'title': 'Extraordinary',
  22. },
  23. },
  24. # audiomack wrapper around soundcloud song
  25. # Needs new test URL.
  26. {
  27. 'add_ie': ['Soundcloud'],
  28. 'url': 'http://www.audiomack.com/song/hip-hop-daily/black-mamba-freestyle',
  29. 'info_dict': {
  30. 'id': '258901379',
  31. 'ext': 'mp3',
  32. 'description': 'mamba day freestyle for the legend Kobe Bryant ',
  33. 'title': 'Black Mamba Freestyle [Prod. By Danny Wolf]',
  34. 'uploader': 'ILOVEMAKONNEN',
  35. 'upload_date': '20160414',
  36. },
  37. 'skip': 'Song has been removed from the site',
  38. },
  39. ]
  40. def _real_extract(self, url):
  41. # URLs end with [uploader name]/song/[uploader title]
  42. # this title is whatever the user types in, and is rarely
  43. # the proper song title. Real metadata is in the api response
  44. album_url_tag = self._match_id(url).replace('/song/', '/')
  45. # Request the extended version of the api for extra fields like artist and title
  46. api_response = self._download_json(
  47. 'http://www.audiomack.com/api/music/url/song/%s?extended=1&_=%d' % (
  48. album_url_tag, time.time()),
  49. album_url_tag)
  50. # API is inconsistent with errors
  51. if 'url' not in api_response or not api_response['url'] or 'error' in api_response:
  52. raise ExtractorError(f'Invalid url {url}')
  53. # Audiomack wraps a lot of soundcloud tracks in their branded wrapper
  54. # if so, pass the work off to the soundcloud extractor
  55. if SoundcloudIE.suitable(api_response['url']):
  56. return self.url_result(api_response['url'], SoundcloudIE.ie_key())
  57. return {
  58. 'id': str(api_response.get('id', album_url_tag)),
  59. 'uploader': api_response.get('artist'),
  60. 'title': api_response.get('title'),
  61. 'url': api_response['url'],
  62. }
  63. class AudiomackAlbumIE(InfoExtractor):
  64. _VALID_URL = r'https?://(?:www\.)?audiomack\.com/(?:album/|(?=.+/album/))(?P<id>[\w/-]+)'
  65. IE_NAME = 'audiomack:album'
  66. _TESTS = [
  67. # Standard album playlist
  68. {
  69. 'url': 'http://www.audiomack.com/album/flytunezcom/tha-tour-part-2-mixtape',
  70. 'playlist_count': 11,
  71. 'info_dict':
  72. {
  73. 'id': '812251',
  74. 'title': 'Tha Tour: Part 2 (Official Mixtape)',
  75. },
  76. },
  77. # Album playlist ripped from fakeshoredrive with no metadata
  78. {
  79. 'url': 'http://www.audiomack.com/album/fakeshoredrive/ppp-pistol-p-project',
  80. 'info_dict': {
  81. 'title': 'PPP (Pistol P Project)',
  82. 'id': '837572',
  83. },
  84. 'playlist': [{
  85. 'info_dict': {
  86. 'title': 'PPP (Pistol P Project) - 8. Real (prod by SYK SENSE )',
  87. 'id': '837576',
  88. 'ext': 'mp3',
  89. 'uploader': 'Lil Herb a.k.a. G Herbo',
  90. },
  91. }, {
  92. 'info_dict': {
  93. 'title': 'PPP (Pistol P Project) - 10. 4 Minutes Of Hell Part 4 (prod by DY OF 808 MAFIA)',
  94. 'id': '837580',
  95. 'ext': 'mp3',
  96. 'uploader': 'Lil Herb a.k.a. G Herbo',
  97. },
  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=f'Querying song information ({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(f'Invalid url for track {track_no} of album url {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] = str(api_response[apikey])
  127. song_id = url_basename(api_response['url']).rpartition('.')[0]
  128. result['entries'].append({
  129. 'id': 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