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.

107 lines
4.0 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. compat_urllib_parse,
  6. ExtractorError,
  7. HEADRequest,
  8. int_or_none,
  9. parse_iso8601,
  10. )
  11. class MixcloudIE(InfoExtractor):
  12. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/([^/]+)/([^/]+)'
  13. IE_NAME = 'mixcloud'
  14. _TEST = {
  15. 'url': 'http://www.mixcloud.com/dholbach/cryptkeeper/',
  16. 'info_dict': {
  17. 'id': 'dholbach-cryptkeeper',
  18. 'ext': 'mp3',
  19. 'title': 'Cryptkeeper',
  20. 'description': 'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.',
  21. 'uploader': 'Daniel Holbach',
  22. 'uploader_id': 'dholbach',
  23. 'upload_date': '20111115',
  24. 'timestamp': 1321359578,
  25. 'thumbnail': 're:https?://.*\.jpg',
  26. 'view_count': int,
  27. 'like_count': int,
  28. },
  29. }
  30. def check_urls(self, url_list):
  31. """Returns 1st active url from list"""
  32. for url in url_list:
  33. try:
  34. # We only want to know if the request succeed
  35. # don't download the whole file
  36. self._request_webpage(HEADRequest(url), None, False)
  37. return url
  38. except ExtractorError:
  39. url = None
  40. return None
  41. def _get_url(self, template_url):
  42. return self.check_urls(template_url % i for i in range(30))
  43. def _real_extract(self, url):
  44. mobj = re.match(self._VALID_URL, url)
  45. uploader = mobj.group(1)
  46. cloudcast_name = mobj.group(2)
  47. track_id = compat_urllib_parse.unquote('-'.join((uploader, cloudcast_name)))
  48. webpage = self._download_webpage(url, track_id)
  49. preview_url = self._search_regex(
  50. r'\s(?:data-preview-url|m-preview)="(.+?)"', webpage, 'preview url')
  51. song_url = preview_url.replace('/previews/', '/c/originals/')
  52. template_url = re.sub(r'(stream\d*)', 'stream%d', song_url)
  53. final_song_url = self._get_url(template_url)
  54. if final_song_url is None:
  55. self.to_screen('Trying with m4a extension')
  56. template_url = template_url.replace('.mp3', '.m4a').replace('originals/', 'm4a/64/')
  57. final_song_url = self._get_url(template_url)
  58. if final_song_url is None:
  59. raise ExtractorError('Unable to extract track url')
  60. PREFIX = (
  61. r'<div class="cloudcast-play-button-container"'
  62. r'(?:\s+[a-zA-Z0-9-]+(?:="[^"]+")?)*?\s+')
  63. title = self._html_search_regex(
  64. PREFIX + r'm-title="([^"]+)"', webpage, 'title')
  65. thumbnail = self._proto_relative_url(self._html_search_regex(
  66. PREFIX + r'm-thumbnail-url="([^"]+)"', webpage, 'thumbnail',
  67. fatal=False))
  68. uploader = self._html_search_regex(
  69. PREFIX + r'm-owner-name="([^"]+)"',
  70. webpage, 'uploader', fatal=False)
  71. uploader_id = self._search_regex(
  72. r'\s+"profile": "([^"]+)",', webpage, 'uploader id', fatal=False)
  73. description = self._og_search_description(webpage)
  74. like_count = int_or_none(self._search_regex(
  75. r'<meta itemprop="interactionCount" content="UserLikes:([0-9]+)"',
  76. webpage, 'like count', fatal=False))
  77. view_count = int_or_none(self._search_regex(
  78. r'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"',
  79. webpage, 'play count', fatal=False))
  80. timestamp = parse_iso8601(self._search_regex(
  81. r'<time itemprop="dateCreated" datetime="([^"]+)">',
  82. webpage, 'upload date'))
  83. return {
  84. 'id': track_id,
  85. 'title': title,
  86. 'url': final_song_url,
  87. 'description': description,
  88. 'thumbnail': thumbnail,
  89. 'uploader': uploader,
  90. 'uploader_id': uploader_id,
  91. 'timestamp': timestamp,
  92. 'view_count': view_count,
  93. 'like_count': like_count,
  94. }