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.

144 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 ..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. 'info_dict': {
  81. 'title': 'PPP (Pistol P Project)',
  82. 'id': '837572',
  83. },
  84. 'playlist': [{
  85. 'info_dict': {
  86. 'title': 'PPP (Pistol P Project) - 9. Heaven or Hell (CHIMACA) ft Zuse (prod by DJ FU)',
  87. 'id': '837577',
  88. 'ext': 'mp3',
  89. 'uploader': 'Lil Herb a.k.a. G Herbo',
  90. }
  91. }],
  92. 'params': {
  93. 'playliststart': 9,
  94. 'playlistend': 9,
  95. }
  96. }
  97. ]
  98. def _real_extract(self, url):
  99. # URLs end with [uploader name]/[uploader title]
  100. # this title is whatever the user types in, and is rarely
  101. # the proper song title. Real metadata is in the api response
  102. album_url_tag = self._match_id(url)
  103. result = {'_type': 'playlist', 'entries': []}
  104. # There is no one endpoint for album metadata - instead it is included/repeated in each song's metadata
  105. # Therefore we don't know how many songs the album has and must infi-loop until failure
  106. for track_no in itertools.count():
  107. # Get song's metadata
  108. api_response = self._download_json(
  109. 'http://www.audiomack.com/api/music/url/album/%s/%d?extended=1&_=%d'
  110. % (album_url_tag, track_no, time.time()), album_url_tag,
  111. note='Querying song information (%d)' % (track_no + 1))
  112. # Total failure, only occurs when url is totally wrong
  113. # Won't happen in middle of valid playlist (next case)
  114. if 'url' not in api_response or 'error' in api_response:
  115. raise ExtractorError('Invalid url for track %d of album url %s' % (track_no, url))
  116. # URL is good but song id doesn't exist - usually means end of playlist
  117. elif not api_response['url']:
  118. break
  119. else:
  120. # Pull out the album metadata and add to result (if it exists)
  121. for resultkey, apikey in [('id', 'album_id'), ('title', 'album_title')]:
  122. if apikey in api_response and resultkey not in result:
  123. result[resultkey] = api_response[apikey]
  124. song_id = url_basename(api_response['url']).rpartition('.')[0]
  125. result['entries'].append({
  126. 'id': api_response.get('id', song_id),
  127. 'uploader': api_response.get('artist'),
  128. 'title': api_response.get('title', song_id),
  129. 'url': api_response['url'],
  130. })
  131. return result