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.

109 lines
4.0 KiB

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