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.

76 lines
2.8 KiB

  1. import json
  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 = u'mixcloud'
  11. _TEST = {
  12. u'url': u'http://www.mixcloud.com/dholbach/cryptkeeper/',
  13. u'file': u'dholbach-cryptkeeper.mp3',
  14. u'info_dict': {
  15. u'title': u'Cryptkeeper',
  16. u'description': u'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.',
  17. u'uploader': u'Daniel Holbach',
  18. u'uploader_id': u'dholbach',
  19. u'upload_date': u'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. api_url = 'http://api.mixcloud.com/%s/%s/' % (uploader, cloudcast_name)
  41. webpage = self._download_webpage(url, track_id)
  42. json_data = self._download_webpage(api_url, track_id,
  43. u'Downloading cloudcast info')
  44. info = json.loads(json_data)
  45. preview_url = self._search_regex(r'data-preview-url="(.+?)"', webpage, u'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. }