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 _get_url(self, track_id, template_url):
  31. server_count = 30
  32. for i in range(server_count):
  33. url = template_url % i
  34. try:
  35. # We only want to know if the request succeed
  36. # don't download the whole file
  37. self._request_webpage(
  38. HEADRequest(url), track_id,
  39. 'Checking URL %d/%d ...' % (i + 1, server_count + 1))
  40. return url
  41. except ExtractorError:
  42. pass
  43. return None
  44. def _real_extract(self, url):
  45. mobj = re.match(self._VALID_URL, url)
  46. uploader = mobj.group(1)
  47. cloudcast_name = mobj.group(2)
  48. track_id = compat_urllib_parse.unquote('-'.join((uploader, cloudcast_name)))
  49. webpage = self._download_webpage(url, track_id)
  50. preview_url = self._search_regex(
  51. r'\s(?:data-preview-url|m-preview)="(.+?)"', webpage, 'preview url')
  52. song_url = preview_url.replace('/previews/', '/c/originals/')
  53. template_url = re.sub(r'(stream\d*)', 'stream%d', song_url)
  54. final_song_url = self._get_url(track_id, template_url)
  55. if final_song_url is None:
  56. self.to_screen('Trying with m4a extension')
  57. template_url = template_url.replace('.mp3', '.m4a').replace('originals/', 'm4a/64/')
  58. final_song_url = self._get_url(track_id, template_url)
  59. if final_song_url is None:
  60. raise ExtractorError('Unable to extract track url')
  61. PREFIX = (
  62. r'<span class="play-button[^"]*?"'
  63. r'(?:\s+[a-zA-Z0-9-]+(?:="[^"]+")?)*?\s+')
  64. title = self._html_search_regex(
  65. PREFIX + r'm-title="([^"]+)"', webpage, 'title')
  66. thumbnail = self._proto_relative_url(self._html_search_regex(
  67. PREFIX + r'm-thumbnail-url="([^"]+)"', webpage, 'thumbnail',
  68. fatal=False))
  69. uploader = self._html_search_regex(
  70. PREFIX + r'm-owner-name="([^"]+)"',
  71. webpage, 'uploader', fatal=False)
  72. uploader_id = self._search_regex(
  73. r'\s+"profile": "([^"]+)",', webpage, 'uploader id', fatal=False)
  74. description = self._og_search_description(webpage)
  75. like_count = int_or_none(self._search_regex(
  76. r'<meta itemprop="interactionCount" content="UserLikes:([0-9]+)"',
  77. webpage, 'like count', fatal=False))
  78. view_count = int_or_none(self._search_regex(
  79. r'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"',
  80. webpage, 'play count', fatal=False))
  81. timestamp = parse_iso8601(self._search_regex(
  82. r'<time itemprop="dateCreated" datetime="([^"]+)">',
  83. webpage, 'upload date'))
  84. return {
  85. 'id': track_id,
  86. 'title': title,
  87. 'url': final_song_url,
  88. 'description': description,
  89. 'thumbnail': thumbnail,
  90. 'uploader': uploader,
  91. 'uploader_id': uploader_id,
  92. 'timestamp': timestamp,
  93. 'view_count': view_count,
  94. 'like_count': like_count,
  95. }