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.

68 lines
2.4 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 _real_extract(self, url):
  34. mobj = re.match(self._VALID_URL, url)
  35. uploader = mobj.group(1)
  36. cloudcast_name = mobj.group(2)
  37. track_id = '-'.join((uploader, cloudcast_name))
  38. api_url = 'http://api.mixcloud.com/%s/%s/' % (uploader, cloudcast_name)
  39. webpage = self._download_webpage(url, track_id)
  40. json_data = self._download_webpage(api_url, track_id,
  41. u'Downloading cloudcast info')
  42. info = json.loads(json_data)
  43. preview_url = self._search_regex(r'data-preview-url="(.+?)"', webpage, u'preview url')
  44. song_url = preview_url.replace('/previews/', '/cloudcasts/originals/')
  45. template_url = re.sub(r'(stream\d*)', 'stream%d', song_url)
  46. final_song_url = self.check_urls(template_url % i for i in range(30))
  47. return {
  48. 'id': track_id,
  49. 'title': info['name'],
  50. 'url': final_song_url,
  51. 'ext': 'mp3',
  52. 'description': info.get('description'),
  53. 'thumbnail': info['pictures'].get('extra_large'),
  54. 'uploader': info['user']['name'],
  55. 'uploader_id': info['user']['username'],
  56. 'upload_date': unified_strdate(info['created_time']),
  57. 'view_count': info['play_count'],
  58. }