audiomack.py 6.0 KB

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