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.

310 lines
11 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. 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. @staticmethod
  52. def _decrypt_play_info(play_info):
  53. KEY = 'pleasedontdownloadourmusictheartistswontgetpaid'
  54. play_info = base64.b64decode(play_info.encode('ascii'))
  55. return ''.join([
  56. compat_chr(compat_ord(ch) ^ compat_ord(KEY[idx % len(KEY)]))
  57. for idx, ch in enumerate(play_info)])
  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. encrypted_play_info = self._search_regex(
  68. r'm-play-info="([^"]+)"', webpage, 'play info')
  69. play_info = self._parse_json(
  70. self._decrypt_play_info(encrypted_play_info), track_id)
  71. if message and 'stream_url' not in play_info:
  72. raise ExtractorError('%s said: %s' % (self.IE_NAME, message), expected=True)
  73. song_url = play_info['stream_url']
  74. title = self._html_search_regex(r'm-title="([^"]+)"', webpage, 'title')
  75. thumbnail = self._proto_relative_url(self._html_search_regex(
  76. r'm-thumbnail-url="([^"]+)"', webpage, 'thumbnail', fatal=False))
  77. uploader = self._html_search_regex(
  78. r'm-owner-name="([^"]+)"', webpage, 'uploader', fatal=False)
  79. uploader_id = self._search_regex(
  80. r'\s+"profile": "([^"]+)",', webpage, 'uploader id', fatal=False)
  81. description = self._og_search_description(webpage)
  82. view_count = str_to_int(self._search_regex(
  83. [r'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"',
  84. r'/listeners/?">([0-9,.]+)</a>',
  85. r'm-tooltip=["\']([\d,.]+) plays'],
  86. webpage, 'play count', default=None))
  87. return {
  88. 'id': track_id,
  89. 'title': title,
  90. 'url': song_url,
  91. 'description': description,
  92. 'thumbnail': thumbnail,
  93. 'uploader': uploader,
  94. 'uploader_id': uploader_id,
  95. 'view_count': view_count,
  96. }
  97. class MixcloudPlaylistBaseIE(InfoExtractor):
  98. _PAGE_SIZE = 24
  99. def _find_urls_in_page(self, page):
  100. for url in re.findall(r'm-play-button m-url="(?P<url>[^"]+)"', page):
  101. yield self.url_result(
  102. compat_urlparse.urljoin('https://www.mixcloud.com', clean_html(url)),
  103. MixcloudIE.ie_key())
  104. def _fetch_tracks_page(self, path, video_id, page_name, current_page, real_page_number=None):
  105. real_page_number = real_page_number or current_page + 1
  106. return self._download_webpage(
  107. 'https://www.mixcloud.com/%s/' % path, video_id,
  108. note='Download %s (page %d)' % (page_name, current_page + 1),
  109. errnote='Unable to download %s' % page_name,
  110. query={'page': real_page_number, 'list': 'main', '_ajax': '1'},
  111. headers={'X-Requested-With': 'XMLHttpRequest'})
  112. def _tracks_page_func(self, page, video_id, page_name, current_page):
  113. resp = self._fetch_tracks_page(page, video_id, page_name, current_page)
  114. for item in self._find_urls_in_page(resp):
  115. yield item
  116. def _get_user_description(self, page_content):
  117. return self._html_search_regex(
  118. r'<div[^>]+class="description-text"[^>]*>(.+?)</div>',
  119. page_content, 'user description', fatal=False)
  120. class MixcloudUserIE(MixcloudPlaylistBaseIE):
  121. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/(?P<type>uploads|favorites|listens)?/?$'
  122. IE_NAME = 'mixcloud:user'
  123. _TESTS = [{
  124. 'url': 'http://www.mixcloud.com/dholbach/',
  125. 'info_dict': {
  126. 'id': 'dholbach_uploads',
  127. 'title': 'Daniel Holbach (uploads)',
  128. 'description': 'md5:327af72d1efeb404a8216c27240d1370',
  129. },
  130. 'playlist_mincount': 11,
  131. }, {
  132. 'url': 'http://www.mixcloud.com/dholbach/uploads/',
  133. 'info_dict': {
  134. 'id': 'dholbach_uploads',
  135. 'title': 'Daniel Holbach (uploads)',
  136. 'description': 'md5:327af72d1efeb404a8216c27240d1370',
  137. },
  138. 'playlist_mincount': 11,
  139. }, {
  140. 'url': 'http://www.mixcloud.com/dholbach/favorites/',
  141. 'info_dict': {
  142. 'id': 'dholbach_favorites',
  143. 'title': 'Daniel Holbach (favorites)',
  144. 'description': 'md5:327af72d1efeb404a8216c27240d1370',
  145. },
  146. 'params': {
  147. 'playlist_items': '1-100',
  148. },
  149. 'playlist_mincount': 100,
  150. }, {
  151. 'url': 'http://www.mixcloud.com/dholbach/listens/',
  152. 'info_dict': {
  153. 'id': 'dholbach_listens',
  154. 'title': 'Daniel Holbach (listens)',
  155. 'description': 'md5:327af72d1efeb404a8216c27240d1370',
  156. },
  157. 'params': {
  158. 'playlist_items': '1-100',
  159. },
  160. 'playlist_mincount': 100,
  161. }]
  162. def _real_extract(self, url):
  163. mobj = re.match(self._VALID_URL, url)
  164. user_id = mobj.group('user')
  165. list_type = mobj.group('type')
  166. # if only a profile URL was supplied, default to download all uploads
  167. if list_type is None:
  168. list_type = 'uploads'
  169. video_id = '%s_%s' % (user_id, list_type)
  170. profile = self._download_webpage(
  171. 'https://www.mixcloud.com/%s/' % user_id, video_id,
  172. note='Downloading user profile',
  173. errnote='Unable to download user profile')
  174. username = self._og_search_title(profile)
  175. description = self._get_user_description(profile)
  176. entries = OnDemandPagedList(
  177. functools.partial(
  178. self._tracks_page_func,
  179. '%s/%s' % (user_id, list_type), video_id, 'list of %s' % list_type),
  180. self._PAGE_SIZE, use_cache=True)
  181. return self.playlist_result(
  182. entries, video_id, '%s (%s)' % (username, list_type), description)
  183. class MixcloudPlaylistIE(MixcloudPlaylistBaseIE):
  184. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/playlists/(?P<playlist>[^/]+)/?$'
  185. IE_NAME = 'mixcloud:playlist'
  186. _TESTS = [{
  187. 'url': 'https://www.mixcloud.com/RedBullThre3style/playlists/tokyo-finalists-2015/',
  188. 'info_dict': {
  189. 'id': 'RedBullThre3style_tokyo-finalists-2015',
  190. 'title': 'National Champions 2015',
  191. 'description': 'md5:6ff5fb01ac76a31abc9b3939c16243a3',
  192. },
  193. 'playlist_mincount': 16,
  194. }, {
  195. 'url': 'https://www.mixcloud.com/maxvibes/playlists/jazzcat-on-ness-radio/',
  196. 'info_dict': {
  197. 'id': 'maxvibes_jazzcat-on-ness-radio',
  198. 'title': 'Jazzcat on Ness Radio',
  199. 'description': 'md5:7bbbf0d6359a0b8cda85224be0f8f263',
  200. },
  201. 'playlist_mincount': 23
  202. }]
  203. def _real_extract(self, url):
  204. mobj = re.match(self._VALID_URL, url)
  205. user_id = mobj.group('user')
  206. playlist_id = mobj.group('playlist')
  207. video_id = '%s_%s' % (user_id, playlist_id)
  208. profile = self._download_webpage(
  209. url, user_id,
  210. note='Downloading playlist page',
  211. errnote='Unable to download playlist page')
  212. description = self._get_user_description(profile)
  213. playlist_title = self._html_search_regex(
  214. r'<span[^>]+class="[^"]*list-playlist-title[^"]*"[^>]*>(.*?)</span>',
  215. profile, 'playlist title')
  216. entries = OnDemandPagedList(
  217. functools.partial(
  218. self._tracks_page_func,
  219. '%s/playlists/%s' % (user_id, playlist_id), video_id, 'tracklist'),
  220. self._PAGE_SIZE)
  221. return self.playlist_result(entries, video_id, playlist_title, description)
  222. class MixcloudStreamIE(MixcloudPlaylistBaseIE):
  223. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/(?P<id>[^/]+)/stream/?$'
  224. IE_NAME = 'mixcloud:stream'
  225. _TEST = {
  226. 'url': 'https://www.mixcloud.com/FirstEar/stream/',
  227. 'info_dict': {
  228. 'id': 'FirstEar',
  229. 'title': 'First Ear',
  230. 'description': 'Curators of good music\nfirstearmusic.com',
  231. },
  232. 'playlist_mincount': 192,
  233. }
  234. def _real_extract(self, url):
  235. user_id = self._match_id(url)
  236. webpage = self._download_webpage(url, user_id)
  237. entries = []
  238. prev_page_url = None
  239. def _handle_page(page):
  240. entries.extend(self._find_urls_in_page(page))
  241. return self._search_regex(
  242. r'm-next-page-url="([^"]+)"', page,
  243. 'next page URL', default=None)
  244. next_page_url = _handle_page(webpage)
  245. for idx in itertools.count(0):
  246. if not next_page_url or prev_page_url == next_page_url:
  247. break
  248. prev_page_url = next_page_url
  249. current_page = int(self._search_regex(
  250. r'\?page=(\d+)', next_page_url, 'next page number'))
  251. next_page_url = _handle_page(self._fetch_tracks_page(
  252. '%s/stream' % user_id, user_id, 'stream', idx,
  253. real_page_number=current_page))
  254. username = self._og_search_title(webpage)
  255. description = self._get_user_description(webpage)
  256. return self.playlist_result(entries, user_id, username, description)