mixcloud.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. from __future__ import unicode_literals
  2. import base64
  3. import functools
  4. import itertools
  5. import re
  6. from .common import InfoExtractor
  7. from ..compat import (
  8. compat_chr,
  9. compat_ord,
  10. compat_urllib_parse_unquote,
  11. compat_urlparse,
  12. )
  13. from ..utils import (
  14. clean_html,
  15. ExtractorError,
  16. OnDemandPagedList,
  17. str_to_int,
  18. )
  19. class MixcloudIE(InfoExtractor):
  20. _VALID_URL = r'https?://(?:(?:www|beta|m)\.)?mixcloud\.com/([^/]+)/(?!stream|uploads|favorites|listens|playlists)([^/]+)'
  21. IE_NAME = 'mixcloud'
  22. _TESTS = [{
  23. 'url': 'http://www.mixcloud.com/dholbach/cryptkeeper/',
  24. 'info_dict': {
  25. 'id': 'dholbach-cryptkeeper',
  26. 'ext': 'm4a',
  27. 'title': 'Cryptkeeper',
  28. 'description': 'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.',
  29. 'uploader': 'Daniel Holbach',
  30. 'uploader_id': 'dholbach',
  31. 'thumbnail': r're:https?://.*\.jpg',
  32. 'view_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?://.*',
  44. 'view_count': int,
  45. },
  46. }, {
  47. 'url': 'https://beta.mixcloud.com/RedLightRadio/nosedrip-15-red-light-radio-01-18-2016/',
  48. 'only_matching': True,
  49. }]
  50. # See https://www.mixcloud.com/media/js2/www_js_2.9e23256562c080482435196ca3975ab5.js
  51. def _decrypt_play_info(self, play_info, video_id):
  52. KEYS = (
  53. 'pleasedontdownloadourmusictheartistswontgetpaid',
  54. '(function() { return new Date().toLocaleDateString(); })()'
  55. )
  56. play_info = base64.b64decode(play_info.encode('ascii'))
  57. for num, key in enumerate(KEYS, start=1):
  58. try:
  59. return self._parse_json(
  60. ''.join([
  61. compat_chr(compat_ord(ch) ^ compat_ord(key[idx % len(key)]))
  62. for idx, ch in enumerate(play_info)]),
  63. video_id)
  64. except ExtractorError:
  65. if num == len(KEYS):
  66. raise
  67. def _real_extract(self, url):
  68. mobj = re.match(self._VALID_URL, url)
  69. uploader = mobj.group(1)
  70. cloudcast_name = mobj.group(2)
  71. track_id = compat_urllib_parse_unquote('-'.join((uploader, cloudcast_name)))
  72. webpage = self._download_webpage(url, track_id)
  73. message = self._html_search_regex(
  74. r'(?s)<div[^>]+class="global-message cloudcast-disabled-notice-light"[^>]*>(.+?)<(?:a|/div)',
  75. webpage, 'error message', default=None)
  76. encrypted_play_info = self._search_regex(
  77. r'm-play-info="([^"]+)"', webpage, 'play info')
  78. play_info = self._decrypt_play_info(encrypted_play_info, track_id)
  79. if message and 'stream_url' not in play_info:
  80. raise ExtractorError('%s said: %s' % (self.IE_NAME, message), expected=True)
  81. song_url = play_info['stream_url']
  82. title = self._html_search_regex(r'm-title="([^"]+)"', webpage, 'title')
  83. thumbnail = self._proto_relative_url(self._html_search_regex(
  84. r'm-thumbnail-url="([^"]+)"', webpage, 'thumbnail', fatal=False))
  85. uploader = self._html_search_regex(
  86. r'm-owner-name="([^"]+)"', webpage, 'uploader', fatal=False)
  87. uploader_id = self._search_regex(
  88. r'\s+"profile": "([^"]+)",', webpage, 'uploader id', fatal=False)
  89. description = self._og_search_description(webpage)
  90. view_count = str_to_int(self._search_regex(
  91. [r'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"',
  92. r'/listeners/?">([0-9,.]+)</a>',
  93. r'(?:m|data)-tooltip=["\']([\d,.]+) plays'],
  94. webpage, 'play count', default=None))
  95. return {
  96. 'id': track_id,
  97. 'title': title,
  98. 'url': song_url,
  99. 'description': description,
  100. 'thumbnail': thumbnail,
  101. 'uploader': uploader,
  102. 'uploader_id': uploader_id,
  103. 'view_count': view_count,
  104. }
  105. class MixcloudPlaylistBaseIE(InfoExtractor):
  106. _PAGE_SIZE = 24
  107. def _find_urls_in_page(self, page):
  108. for url in re.findall(r'm-play-button m-url="(?P<url>[^"]+)"', page):
  109. yield self.url_result(
  110. compat_urlparse.urljoin('https://www.mixcloud.com', clean_html(url)),
  111. MixcloudIE.ie_key())
  112. def _fetch_tracks_page(self, path, video_id, page_name, current_page, real_page_number=None):
  113. real_page_number = real_page_number or current_page + 1
  114. return self._download_webpage(
  115. 'https://www.mixcloud.com/%s/' % path, video_id,
  116. note='Download %s (page %d)' % (page_name, current_page + 1),
  117. errnote='Unable to download %s' % page_name,
  118. query={'page': real_page_number, 'list': 'main', '_ajax': '1'},
  119. headers={'X-Requested-With': 'XMLHttpRequest'})
  120. def _tracks_page_func(self, page, video_id, page_name, current_page):
  121. resp = self._fetch_tracks_page(page, video_id, page_name, current_page)
  122. for item in self._find_urls_in_page(resp):
  123. yield item
  124. def _get_user_description(self, page_content):
  125. return self._html_search_regex(
  126. r'<div[^>]+class="profile-bio"[^>]*>(.+?)</div>',
  127. page_content, 'user description', fatal=False)
  128. class MixcloudUserIE(MixcloudPlaylistBaseIE):
  129. _VALID_URL = r'https?://(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/(?P<type>uploads|favorites|listens)?/?$'
  130. IE_NAME = 'mixcloud:user'
  131. _TESTS = [{
  132. 'url': 'http://www.mixcloud.com/dholbach/',
  133. 'info_dict': {
  134. 'id': 'dholbach_uploads',
  135. 'title': 'Daniel Holbach (uploads)',
  136. 'description': 'md5:def36060ac8747b3aabca54924897e47',
  137. },
  138. 'playlist_mincount': 11,
  139. }, {
  140. 'url': 'http://www.mixcloud.com/dholbach/uploads/',
  141. 'info_dict': {
  142. 'id': 'dholbach_uploads',
  143. 'title': 'Daniel Holbach (uploads)',
  144. 'description': 'md5:def36060ac8747b3aabca54924897e47',
  145. },
  146. 'playlist_mincount': 11,
  147. }, {
  148. 'url': 'http://www.mixcloud.com/dholbach/favorites/',
  149. 'info_dict': {
  150. 'id': 'dholbach_favorites',
  151. 'title': 'Daniel Holbach (favorites)',
  152. 'description': 'md5:def36060ac8747b3aabca54924897e47',
  153. },
  154. 'params': {
  155. 'playlist_items': '1-100',
  156. },
  157. 'playlist_mincount': 100,
  158. }, {
  159. 'url': 'http://www.mixcloud.com/dholbach/listens/',
  160. 'info_dict': {
  161. 'id': 'dholbach_listens',
  162. 'title': 'Daniel Holbach (listens)',
  163. 'description': 'md5:def36060ac8747b3aabca54924897e47',
  164. },
  165. 'params': {
  166. 'playlist_items': '1-100',
  167. },
  168. 'playlist_mincount': 100,
  169. }]
  170. def _real_extract(self, url):
  171. mobj = re.match(self._VALID_URL, url)
  172. user_id = mobj.group('user')
  173. list_type = mobj.group('type')
  174. # if only a profile URL was supplied, default to download all uploads
  175. if list_type is None:
  176. list_type = 'uploads'
  177. video_id = '%s_%s' % (user_id, list_type)
  178. profile = self._download_webpage(
  179. 'https://www.mixcloud.com/%s/' % user_id, video_id,
  180. note='Downloading user profile',
  181. errnote='Unable to download user profile')
  182. username = self._og_search_title(profile)
  183. description = self._get_user_description(profile)
  184. entries = OnDemandPagedList(
  185. functools.partial(
  186. self._tracks_page_func,
  187. '%s/%s' % (user_id, list_type), video_id, 'list of %s' % list_type),
  188. self._PAGE_SIZE, use_cache=True)
  189. return self.playlist_result(
  190. entries, video_id, '%s (%s)' % (username, list_type), description)
  191. class MixcloudPlaylistIE(MixcloudPlaylistBaseIE):
  192. _VALID_URL = r'https?://(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/playlists/(?P<playlist>[^/]+)/?$'
  193. IE_NAME = 'mixcloud:playlist'
  194. _TESTS = [{
  195. 'url': 'https://www.mixcloud.com/RedBullThre3style/playlists/tokyo-finalists-2015/',
  196. 'info_dict': {
  197. 'id': 'RedBullThre3style_tokyo-finalists-2015',
  198. 'title': 'National Champions 2015',
  199. 'description': 'md5:6ff5fb01ac76a31abc9b3939c16243a3',
  200. },
  201. 'playlist_mincount': 16,
  202. }, {
  203. 'url': 'https://www.mixcloud.com/maxvibes/playlists/jazzcat-on-ness-radio/',
  204. 'only_matching': True,
  205. }]
  206. def _real_extract(self, url):
  207. mobj = re.match(self._VALID_URL, url)
  208. user_id = mobj.group('user')
  209. playlist_id = mobj.group('playlist')
  210. video_id = '%s_%s' % (user_id, playlist_id)
  211. webpage = self._download_webpage(
  212. url, user_id,
  213. note='Downloading playlist page',
  214. errnote='Unable to download playlist page')
  215. title = self._html_search_regex(
  216. r'<a[^>]+class="parent active"[^>]*><b>\d+</b><span[^>]*>([^<]+)',
  217. webpage, 'playlist title',
  218. default=None) or self._og_search_title(webpage, fatal=False)
  219. description = self._get_user_description(webpage)
  220. entries = OnDemandPagedList(
  221. functools.partial(
  222. self._tracks_page_func,
  223. '%s/playlists/%s' % (user_id, playlist_id), video_id, 'tracklist'),
  224. self._PAGE_SIZE)
  225. return self.playlist_result(entries, video_id, title, description)
  226. class MixcloudStreamIE(MixcloudPlaylistBaseIE):
  227. _VALID_URL = r'https?://(?:www\.)?mixcloud\.com/(?P<id>[^/]+)/stream/?$'
  228. IE_NAME = 'mixcloud:stream'
  229. _TEST = {
  230. 'url': 'https://www.mixcloud.com/FirstEar/stream/',
  231. 'info_dict': {
  232. 'id': 'FirstEar',
  233. 'title': 'First Ear',
  234. 'description': 'Curators of good music\nfirstearmusic.com',
  235. },
  236. 'playlist_mincount': 192,
  237. }
  238. def _real_extract(self, url):
  239. user_id = self._match_id(url)
  240. webpage = self._download_webpage(url, user_id)
  241. entries = []
  242. prev_page_url = None
  243. def _handle_page(page):
  244. entries.extend(self._find_urls_in_page(page))
  245. return self._search_regex(
  246. r'm-next-page-url="([^"]+)"', page,
  247. 'next page URL', default=None)
  248. next_page_url = _handle_page(webpage)
  249. for idx in itertools.count(0):
  250. if not next_page_url or prev_page_url == next_page_url:
  251. break
  252. prev_page_url = next_page_url
  253. current_page = int(self._search_regex(
  254. r'\?page=(\d+)', next_page_url, 'next page number'))
  255. next_page_url = _handle_page(self._fetch_tracks_page(
  256. '%s/stream' % user_id, user_id, 'stream', idx,
  257. real_page_number=current_page))
  258. username = self._og_search_title(webpage)
  259. description = self._get_user_description(webpage)
  260. return self.playlist_result(entries, user_id, username, description)