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.

322 lines
12 KiB

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