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.

145 lines
5.7 KiB

10 years ago
  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/(?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. {
  29. 'add_ie': ['Soundcloud'],
  30. 'url': 'http://www.audiomack.com/song/hip-hop-daily/black-mamba-freestyle',
  31. 'info_dict': {
  32. 'id': '258901379',
  33. 'ext': 'mp3',
  34. 'description': 'mamba day freestyle for the legend Kobe Bryant ',
  35. 'title': 'Black Mamba Freestyle [Prod. By Danny Wolf]',
  36. 'uploader': 'ILOVEMAKONNEN',
  37. 'upload_date': '20160414',
  38. }
  39. },
  40. ]
  41. def _real_extract(self, url):
  42. # URLs end with [uploader name]/[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)
  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/(?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': 15,
  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) - 9. Heaven or Hell (CHIMACA) ft Zuse (prod by DJ FU)',
  88. 'id': '837577',
  89. 'ext': 'mp3',
  90. 'uploader': 'Lil Herb a.k.a. G Herbo',
  91. }
  92. }],
  93. 'params': {
  94. 'playliststart': 9,
  95. 'playlistend': 9,
  96. }
  97. }
  98. ]
  99. def _real_extract(self, url):
  100. # URLs end with [uploader name]/[uploader title]
  101. # this title is whatever the user types in, and is rarely
  102. # the proper song title. Real metadata is in the api response
  103. album_url_tag = self._match_id(url)
  104. result = {'_type': 'playlist', 'entries': []}
  105. # There is no one endpoint for album metadata - instead it is included/repeated in each song's metadata
  106. # Therefore we don't know how many songs the album has and must infi-loop until failure
  107. for track_no in itertools.count():
  108. # Get song's metadata
  109. api_response = self._download_json(
  110. 'http://www.audiomack.com/api/music/url/album/%s/%d?extended=1&_=%d'
  111. % (album_url_tag, track_no, time.time()), album_url_tag,
  112. note='Querying song information (%d)' % (track_no + 1))
  113. # Total failure, only occurs when url is totally wrong
  114. # Won't happen in middle of valid playlist (next case)
  115. if 'url' not in api_response or 'error' in api_response:
  116. raise ExtractorError('Invalid url for track %d of album url %s' % (track_no, url))
  117. # URL is good but song id doesn't exist - usually means end of playlist
  118. elif not api_response['url']:
  119. break
  120. else:
  121. # Pull out the album metadata and add to result (if it exists)
  122. for resultkey, apikey in [('id', 'album_id'), ('title', 'album_title')]:
  123. if apikey in api_response and resultkey not in result:
  124. result[resultkey] = api_response[apikey]
  125. song_id = url_basename(api_response['url']).rpartition('.')[0]
  126. result['entries'].append({
  127. 'id': compat_str(api_response.get('id', song_id)),
  128. 'uploader': api_response.get('artist'),
  129. 'title': api_response.get('title', song_id),
  130. 'url': api_response['url'],
  131. })
  132. return result