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.

69 lines
2.4 KiB

  1. import json
  2. import re
  3. import socket
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. compat_http_client,
  7. compat_urllib_error,
  8. compat_urllib_request,
  9. unified_strdate,
  10. )
  11. class MixcloudIE(InfoExtractor):
  12. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/([\w\d-]+)/([\w\d-]+)'
  13. IE_NAME = u'mixcloud'
  14. _TEST = {
  15. u'url': u'http://www.mixcloud.com/dholbach/cryptkeeper/',
  16. u'file': u'dholbach-cryptkeeper.mp3',
  17. u'info_dict': {
  18. u'title': u'Cryptkeeper',
  19. u'description': u'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.',
  20. u'uploader': u'Daniel Holbach',
  21. u'uploader_id': u'dholbach',
  22. u'upload_date': u'20111115',
  23. },
  24. }
  25. def check_urls(self, url_list):
  26. """Returns 1st active url from list"""
  27. for url in url_list:
  28. try:
  29. compat_urllib_request.urlopen(url)
  30. return url
  31. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error):
  32. url = None
  33. return None
  34. def _real_extract(self, url):
  35. mobj = re.match(self._VALID_URL, url)
  36. uploader = mobj.group(1)
  37. cloudcast_name = mobj.group(2)
  38. track_id = '-'.join((uploader, cloudcast_name))
  39. api_url = 'http://api.mixcloud.com/%s/%s/' % (uploader, cloudcast_name)
  40. webpage = self._download_webpage(url, track_id)
  41. json_data = self._download_webpage(api_url, track_id,
  42. u'Downloading cloudcast info')
  43. info = json.loads(json_data)
  44. preview_url = self._search_regex(r'data-preview-url="(.+?)"', webpage, u'preview url')
  45. song_url = preview_url.replace('/previews/', '/cloudcasts/originals/')
  46. template_url = re.sub(r'(stream\d*)', 'stream%d', song_url)
  47. final_song_url = self.check_urls(template_url % i for i in range(30))
  48. return {
  49. 'id': track_id,
  50. 'title': info['name'],
  51. 'url': final_song_url,
  52. 'ext': 'mp3',
  53. 'description': info['description'],
  54. 'thumbnail': info['pictures'].get('extra_large'),
  55. 'uploader': info['user']['name'],
  56. 'uploader_id': info['user']['username'],
  57. 'upload_date': unified_strdate(info['created_time']),
  58. 'view_count': info['play_count'],
  59. }