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.

80 lines
2.8 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. unified_strdate,
  6. compat_urllib_parse,
  7. ExtractorError,
  8. )
  9. class MixcloudIE(InfoExtractor):
  10. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/([^/]+)/([^/]+)'
  11. IE_NAME = 'mixcloud'
  12. _TEST = {
  13. 'url': 'http://www.mixcloud.com/dholbach/cryptkeeper/',
  14. 'info_dict': {
  15. 'id': 'dholbach-cryptkeeper',
  16. 'ext': 'mp3',
  17. 'title': 'Cryptkeeper',
  18. 'description': 'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.',
  19. 'uploader': 'Daniel Holbach',
  20. 'uploader_id': 'dholbach',
  21. 'upload_date': '20111115',
  22. },
  23. }
  24. def check_urls(self, url_list):
  25. """Returns 1st active url from list"""
  26. for url in url_list:
  27. try:
  28. # We only want to know if the request succeed
  29. # don't download the whole file
  30. self._request_webpage(url, None, False)
  31. return url
  32. except ExtractorError:
  33. url = None
  34. return None
  35. def _get_url(self, template_url):
  36. return self.check_urls(template_url % i for i in range(30))
  37. def _real_extract(self, url):
  38. mobj = re.match(self._VALID_URL, url)
  39. uploader = mobj.group(1)
  40. cloudcast_name = mobj.group(2)
  41. track_id = compat_urllib_parse.unquote('-'.join((uploader, cloudcast_name)))
  42. webpage = self._download_webpage(url, track_id)
  43. api_url = 'http://api.mixcloud.com/%s/%s/' % (uploader, cloudcast_name)
  44. info = self._download_json(
  45. api_url, track_id, 'Downloading cloudcast info')
  46. preview_url = self._search_regex(
  47. r'\s(?:data-preview-url|m-preview)="(.+?)"', webpage, 'preview url')
  48. song_url = preview_url.replace('/previews/', '/c/originals/')
  49. template_url = re.sub(r'(stream\d*)', 'stream%d', song_url)
  50. final_song_url = self._get_url(template_url)
  51. if final_song_url is None:
  52. self.to_screen('Trying with m4a extension')
  53. template_url = template_url.replace('.mp3', '.m4a').replace('originals/', 'm4a/64/')
  54. final_song_url = self._get_url(template_url)
  55. if final_song_url is None:
  56. raise ExtractorError(u'Unable to extract track url')
  57. return {
  58. 'id': track_id,
  59. 'title': info['name'],
  60. 'url': final_song_url,
  61. 'description': info.get('description'),
  62. 'thumbnail': info['pictures'].get('extra_large'),
  63. 'uploader': info['user']['name'],
  64. 'uploader_id': info['user']['username'],
  65. 'upload_date': unified_strdate(info['created_time']),
  66. 'view_count': info['play_count'],
  67. }