mixcloud.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. from __future__ import unicode_literals
  2. import functools
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_urllib_parse_unquote,
  7. compat_urlparse,
  8. )
  9. from ..utils import (
  10. clean_html,
  11. ExtractorError,
  12. HEADRequest,
  13. OnDemandPagedList,
  14. NO_DEFAULT,
  15. parse_count,
  16. str_to_int,
  17. )
  18. class MixcloudIE(InfoExtractor):
  19. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/([^/]+)/(?!stream|uploads|favorites|listens|playlists)([^/]+)'
  20. IE_NAME = 'mixcloud'
  21. _TESTS = [{
  22. 'url': 'http://www.mixcloud.com/dholbach/cryptkeeper/',
  23. 'info_dict': {
  24. 'id': 'dholbach-cryptkeeper',
  25. 'ext': 'm4a',
  26. 'title': 'Cryptkeeper',
  27. 'description': 'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.',
  28. 'uploader': 'Daniel Holbach',
  29. 'uploader_id': 'dholbach',
  30. 'thumbnail': 're:https?://.*\.jpg',
  31. 'view_count': int,
  32. 'like_count': int,
  33. },
  34. }, {
  35. 'url': 'http://www.mixcloud.com/gillespeterson/caribou-7-inch-vinyl-mix-chat/',
  36. 'info_dict': {
  37. 'id': 'gillespeterson-caribou-7-inch-vinyl-mix-chat',
  38. 'ext': 'mp3',
  39. 'title': 'Caribou 7 inch Vinyl Mix & Chat',
  40. 'description': 'md5:2b8aec6adce69f9d41724647c65875e8',
  41. 'uploader': 'Gilles Peterson Worldwide',
  42. 'uploader_id': 'gillespeterson',
  43. 'thumbnail': 're:https?://.*/images/',
  44. 'view_count': int,
  45. 'like_count': int,
  46. },
  47. }]
  48. def _check_url(self, url, track_id, ext):
  49. try:
  50. # We only want to know if the request succeed
  51. # don't download the whole file
  52. self._request_webpage(
  53. HEADRequest(url), track_id,
  54. 'Trying %s URL' % ext)
  55. return True
  56. except ExtractorError:
  57. return False
  58. def _real_extract(self, url):
  59. mobj = re.match(self._VALID_URL, url)
  60. uploader = mobj.group(1)
  61. cloudcast_name = mobj.group(2)
  62. track_id = compat_urllib_parse_unquote('-'.join((uploader, cloudcast_name)))
  63. webpage = self._download_webpage(url, track_id)
  64. message = self._html_search_regex(
  65. r'(?s)<div[^>]+class="global-message cloudcast-disabled-notice-light"[^>]*>(.+?)<(?:a|/div)',
  66. webpage, 'error message', default=None)
  67. preview_url = self._search_regex(
  68. r'\s(?:data-preview-url|m-preview)="([^"]+)"',
  69. webpage, 'preview url', default=None if message else NO_DEFAULT)
  70. if message:
  71. raise ExtractorError('%s said: %s' % (self.IE_NAME, message), expected=True)
  72. song_url = re.sub(r'audiocdn(\d+)', r'stream\1', preview_url)
  73. song_url = song_url.replace('/previews/', '/c/originals/')
  74. if not self._check_url(song_url, track_id, 'mp3'):
  75. song_url = song_url.replace('.mp3', '.m4a').replace('originals/', 'm4a/64/')
  76. if not self._check_url(song_url, track_id, 'm4a'):
  77. raise ExtractorError('Unable to extract track url')
  78. PREFIX = (
  79. r'm-play-on-spacebar[^>]+'
  80. r'(?:\s+[a-zA-Z0-9-]+(?:="[^"]+")?)*?\s+')
  81. title = self._html_search_regex(
  82. PREFIX + r'm-title="([^"]+)"', webpage, 'title')
  83. thumbnail = self._proto_relative_url(self._html_search_regex(
  84. PREFIX + r'm-thumbnail-url="([^"]+)"', webpage, 'thumbnail',
  85. fatal=False))
  86. uploader = self._html_search_regex(
  87. PREFIX + r'm-owner-name="([^"]+)"',
  88. webpage, 'uploader', fatal=False)
  89. uploader_id = self._search_regex(
  90. r'\s+"profile": "([^"]+)",', webpage, 'uploader id', fatal=False)
  91. description = self._og_search_description(webpage)
  92. like_count = parse_count(self._search_regex(
  93. r'\bbutton-favorite[^>]+>.*?<span[^>]+class=["\']toggle-number[^>]+>\s*([^<]+)',
  94. webpage, 'like count', fatal=False))
  95. view_count = str_to_int(self._search_regex(
  96. [r'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"',
  97. r'/listeners/?">([0-9,.]+)</a>'],
  98. webpage, 'play count', fatal=False))
  99. return {
  100. 'id': track_id,
  101. 'title': title,
  102. 'url': song_url,
  103. 'description': description,
  104. 'thumbnail': thumbnail,
  105. 'uploader': uploader,
  106. 'uploader_id': uploader_id,
  107. 'view_count': view_count,
  108. 'like_count': like_count,
  109. }
  110. class MixcloudPlaylistBaseIE(InfoExtractor):
  111. _PAGE_SIZE = 24
  112. def _fetch_tracks_page(self, path, video_id, page_name, current_page):
  113. resp = self._download_webpage(
  114. 'https://www.mixcloud.com/%s/' % path, video_id,
  115. note='Download %s (page %d)' % (page_name, current_page + 1),
  116. errnote='Unable to download %s' % page_name,
  117. query={'page': (current_page + 1), 'list': 'main', '_ajax': '1'},
  118. headers={'X-Requested-With': 'XMLHttpRequest'})
  119. for url in re.findall(r'm-play-button m-url="(?P<url>[^"]+)"', resp):
  120. yield self.url_result(
  121. compat_urlparse.urljoin('https://www.mixcloud.com', clean_html(url)),
  122. MixcloudIE.ie_key())
  123. def _get_user_description(self, page_content):
  124. return self._html_search_regex(
  125. r'<div[^>]+class="description-text"[^>]*>(.+?)</div>',
  126. page_content, 'user description', fatal=False)
  127. class MixcloudUserIE(MixcloudPlaylistBaseIE):
  128. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/(?P<type>uploads|favorites|listens)?/?$'
  129. IE_NAME = 'mixcloud:user'
  130. _TESTS = [{
  131. 'url': 'http://www.mixcloud.com/dholbach/',
  132. 'info_dict': {
  133. 'id': 'dholbach_uploads',
  134. 'title': 'Daniel Holbach (uploads)',
  135. 'description': 'md5:327af72d1efeb404a8216c27240d1370',
  136. },
  137. 'playlist_mincount': 11,
  138. }, {
  139. 'url': 'http://www.mixcloud.com/dholbach/uploads/',
  140. 'info_dict': {
  141. 'id': 'dholbach_uploads',
  142. 'title': 'Daniel Holbach (uploads)',
  143. 'description': 'md5:327af72d1efeb404a8216c27240d1370',
  144. },
  145. 'playlist_mincount': 11,
  146. }, {
  147. 'url': 'http://www.mixcloud.com/dholbach/favorites/',
  148. 'info_dict': {
  149. 'id': 'dholbach_favorites',
  150. 'title': 'Daniel Holbach (favorites)',
  151. 'description': 'md5:327af72d1efeb404a8216c27240d1370',
  152. },
  153. 'params': {
  154. 'playlist_items': '1-100',
  155. },
  156. 'playlist_mincount': 100,
  157. }, {
  158. 'url': 'http://www.mixcloud.com/dholbach/listens/',
  159. 'info_dict': {
  160. 'id': 'dholbach_listens',
  161. 'title': 'Daniel Holbach (listens)',
  162. 'description': 'md5:327af72d1efeb404a8216c27240d1370',
  163. },
  164. 'params': {
  165. 'playlist_items': '1-100',
  166. },
  167. 'playlist_mincount': 100,
  168. }]
  169. def _real_extract(self, url):
  170. mobj = re.match(self._VALID_URL, url)
  171. user_id = mobj.group('user')
  172. list_type = mobj.group('type')
  173. # if only a profile URL was supplied, default to download all uploads
  174. if list_type is None:
  175. list_type = 'uploads'
  176. video_id = '%s_%s' % (user_id, list_type)
  177. profile = self._download_webpage(
  178. 'https://www.mixcloud.com/%s/' % user_id, video_id,
  179. note='Downloading user profile',
  180. errnote='Unable to download user profile')
  181. username = self._og_search_title(profile)
  182. description = self._get_user_description(profile)
  183. entries = OnDemandPagedList(
  184. functools.partial(
  185. self._fetch_tracks_page,
  186. '%s/%s' % (user_id, list_type), video_id, 'list of %s' % list_type),
  187. self._PAGE_SIZE, use_cache=True)
  188. return self.playlist_result(
  189. entries, video_id, '%s (%s)' % (username, list_type), description)
  190. class MixcloudPlaylistIE(MixcloudPlaylistBaseIE):
  191. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/playlists/(?P<playlist>[^/]+)/?$'
  192. IE_NAME = 'mixcloud:playlist'
  193. _TESTS = [{
  194. 'url': 'https://www.mixcloud.com/RedBullThre3style/playlists/tokyo-finalists-2015/',
  195. 'info_dict': {
  196. 'id': 'RedBullThre3style_tokyo-finalists-2015',
  197. 'title': 'National Champions 2015',
  198. 'description': 'md5:6ff5fb01ac76a31abc9b3939c16243a3',
  199. },
  200. 'playlist_mincount': 16,
  201. }, {
  202. 'url': 'https://www.mixcloud.com/maxvibes/playlists/jazzcat-on-ness-radio/',
  203. 'info_dict': {
  204. 'id': 'maxvibes_jazzcat-on-ness-radio',
  205. 'title': 'Jazzcat on Ness Radio',
  206. 'description': 'md5:7bbbf0d6359a0b8cda85224be0f8f263',
  207. },
  208. 'playlist_mincount': 23
  209. }]
  210. def _real_extract(self, url):
  211. mobj = re.match(self._VALID_URL, url)
  212. user_id = mobj.group('user')
  213. playlist_id = mobj.group('playlist')
  214. video_id = '%s_%s' % (user_id, playlist_id)
  215. profile = self._download_webpage(
  216. url, user_id,
  217. note='Downloading playlist page',
  218. errnote='Unable to download playlist page')
  219. description = self._get_user_description(profile)
  220. playlist_title = self._html_search_regex(
  221. r'<span[^>]+class="[^"]*list-playlist-title[^"]*"[^>]*>(.*?)</span>',
  222. profile, 'playlist title')
  223. entries = OnDemandPagedList(
  224. functools.partial(
  225. self._fetch_tracks_page,
  226. '%s/playlists/%s' % (user_id, playlist_id), video_id, 'tracklist'),
  227. self._PAGE_SIZE)
  228. return self.playlist_result(entries, video_id, playlist_title, description)