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.

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