audiomack.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from .soundcloud import SoundcloudIE
  5. from ..utils import ExtractorError
  6. import time
  7. class AudiomackIE(InfoExtractor):
  8. _VALID_URL = r'https?://(?:www\.)?audiomack\.com/song/(?P<id>[\w/-]+)'
  9. IE_NAME = 'audiomack'
  10. _TESTS = [
  11. # hosted on audiomack
  12. {
  13. 'url': 'http://www.audiomack.com/song/roosh-williams/extraordinary',
  14. 'info_dict':
  15. {
  16. 'id': '310086',
  17. 'ext': 'mp3',
  18. 'artist': 'Roosh Williams',
  19. 'title': 'Extraordinary'
  20. }
  21. },
  22. # audiomack wrapper around soundcloud song
  23. {
  24. 'add_ie': ['Soundcloud'],
  25. 'url': 'http://www.audiomack.com/song/xclusiveszone/take-kare',
  26. 'info_dict': {
  27. 'id': '172419696',
  28. 'ext': 'mp3',
  29. 'description': 'md5:1fc3272ed7a635cce5be1568c2822997',
  30. 'title': 'Young Thug ft Lil Wayne - Take Kare',
  31. 'uploader': 'Young Thug World',
  32. 'upload_date': '20141016',
  33. }
  34. },
  35. ]
  36. @staticmethod
  37. def create_song_dictionary(api_response, album_url_tag, track_no=0):
  38. # All keys are the same in audiomack api and InfoExtractor format
  39. entry = {key: api_response[key] for key in ['title', 'artist', 'id', 'url'] if key in api_response}
  40. # Fudge values in the face of missing metadata
  41. if 'id' not in entry:
  42. entry['id'] = track_no
  43. if 'title' not in entry:
  44. entry['title'] = album_url_tag
  45. return entry
  46. def _real_extract(self, url):
  47. # URLs end with [uploader name]/[uploader title]
  48. # this title is whatever the user types in, and is rarely
  49. # the proper song title. Real metadata is in the api response
  50. album_url_tag = self._match_id(url)
  51. # Request the extended version of the api for extra fields like artist and title
  52. api_response = self._download_json(
  53. 'http://www.audiomack.com/api/music/url/song/%s?extended=1&_=%d' % (
  54. album_url_tag, time.time()),
  55. album_url_tag)
  56. # API is inconsistent with errors
  57. if 'url' not in api_response or not api_response['url'] or 'error' in api_response:
  58. raise ExtractorError('Invalid url %s', url)
  59. # Audiomack wraps a lot of soundcloud tracks in their branded wrapper
  60. # if so, pass the work off to the soundcloud extractor
  61. if SoundcloudIE.suitable(api_response['url']):
  62. return {'_type': 'url', 'url': api_response['url'], 'ie_key': 'Soundcloud'}
  63. return self.create_song_dictionary(api_response, album_url_tag)
  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. 'playlist_count': 10
  82. }
  83. ]
  84. def _real_extract(self, url):
  85. # URLs end with [uploader name]/[uploader title]
  86. # this title is whatever the user types in, and is rarely
  87. # the proper song title. Real metadata is in the api response
  88. album_url_tag = self._match_id(url)
  89. result = {'_type': 'playlist', 'entries': []}
  90. # There is no one endpoint for album metadata - instead it is included/repeated in each song's metadata
  91. # Therefore we don't know how many songs the album has and must infi-loop until failure
  92. track_no = 0
  93. while True:
  94. # Get song's metadata
  95. api_response = self._download_json('http://www.audiomack.com/api/music/url/album/%s/%d?extended=1&_=%d'
  96. % (album_url_tag, track_no, time.time()), album_url_tag)
  97. # Total failure, only occurs when url is totally wrong
  98. # Won't happen in middle of valid playlist (next case)
  99. if 'url' not in api_response or 'error' in api_response:
  100. raise ExtractorError('Invalid url for track %d of album url %s' % (track_no, url))
  101. # URL is good but song id doesn't exist - usually means end of playlist
  102. elif not api_response['url']:
  103. break
  104. else:
  105. # Pull out the album metadata and add to result (if it exists)
  106. for resultkey, apikey in [('id', 'album_id'), ('title', 'album_title')]:
  107. if apikey in api_response and resultkey not in result:
  108. result[resultkey] = api_response[apikey]
  109. result['entries'].append(AudiomackIE.create_song_dictionary(api_response, album_url_tag, track_no))
  110. track_no += 1
  111. return result