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.

139 lines
5.5 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 ..utils import (
  8. ExtractorError,
  9. url_basename,
  10. )
  11. class AudiomackIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:www\.)?audiomack\.com/song/(?P<id>[\w/-]+)'
  13. IE_NAME = 'audiomack'
  14. _TESTS = [
  15. # hosted on audiomack
  16. {
  17. 'url': 'http://www.audiomack.com/song/roosh-williams/extraordinary',
  18. 'info_dict':
  19. {
  20. 'id': '310086',
  21. 'ext': 'mp3',
  22. 'uploader': 'Roosh Williams',
  23. 'title': 'Extraordinary'
  24. }
  25. },
  26. # audiomack wrapper around soundcloud song
  27. {
  28. 'add_ie': ['Soundcloud'],
  29. 'url': 'http://www.audiomack.com/song/xclusiveszone/take-kare',
  30. 'info_dict': {
  31. 'id': '172419696',
  32. 'ext': 'mp3',
  33. 'description': 'md5:1fc3272ed7a635cce5be1568c2822997',
  34. 'title': 'Young Thug ft Lil Wayne - Take Kare',
  35. 'uploader': 'Young Thug World',
  36. 'upload_date': '20141016',
  37. }
  38. },
  39. ]
  40. def _real_extract(self, url):
  41. # URLs end with [uploader name]/[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)
  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('Invalid url %s', 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 {'_type': 'url', 'url': api_response['url'], 'ie_key': 'Soundcloud'}
  57. return {
  58. 'id': 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/(?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': 15,
  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. 'playlist': [{
  81. 'info_dict': {
  82. 'title': '9.-heaven-or-hell-chimaca-ft-zuse-prod-by-dj-fu',
  83. 'id': '9.-heaven-or-hell-chimaca-ft-zuse-prod-by-dj-fu',
  84. 'ext': 'mp3',
  85. }
  86. }],
  87. 'params': {
  88. 'playliststart': 8,
  89. 'playlistend': 8,
  90. }
  91. }
  92. ]
  93. def _real_extract(self, url):
  94. # URLs end with [uploader name]/[uploader title]
  95. # this title is whatever the user types in, and is rarely
  96. # the proper song title. Real metadata is in the api response
  97. album_url_tag = self._match_id(url)
  98. result = {'_type': 'playlist', 'entries': []}
  99. # There is no one endpoint for album metadata - instead it is included/repeated in each song's metadata
  100. # Therefore we don't know how many songs the album has and must infi-loop until failure
  101. for track_no in itertools.count():
  102. # Get song's metadata
  103. api_response = self._download_json(
  104. 'http://www.audiomack.com/api/music/url/album/%s/%d?extended=1&_=%d'
  105. % (album_url_tag, track_no, time.time()), album_url_tag,
  106. note='Querying song information (%d)' % (track_no + 1))
  107. # Total failure, only occurs when url is totally wrong
  108. # Won't happen in middle of valid playlist (next case)
  109. if 'url' not in api_response or 'error' in api_response:
  110. raise ExtractorError('Invalid url for track %d of album url %s' % (track_no, url))
  111. # URL is good but song id doesn't exist - usually means end of playlist
  112. elif not api_response['url']:
  113. break
  114. else:
  115. # Pull out the album metadata and add to result (if it exists)
  116. for resultkey, apikey in [('id', 'album_id'), ('title', 'album_title')]:
  117. if apikey in api_response and resultkey not in result:
  118. result[resultkey] = api_response[apikey]
  119. song_id = url_basename(api_response['url']).rpartition('.')[0]
  120. result['entries'].append({
  121. 'id': api_response.get('id', song_id),
  122. 'uploader': api_response.get('artist'),
  123. 'title': api_response.get('title', song_id),
  124. 'url': api_response['url'],
  125. })
  126. return result