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.

124 lines
4.7 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. str_to_int,
  11. parse_iso8601,
  12. )
  13. class MixcloudIE(InfoExtractor):
  14. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/([^/]+)/([^/]+)'
  15. IE_NAME = 'mixcloud'
  16. _TESTS = [{
  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. 'url': 'http://www.mixcloud.com/gillespeterson/caribou-7-inch-vinyl-mix-chat/',
  33. 'info_dict': {
  34. 'id': 'gillespeterson-caribou-7-inch-vinyl-mix-chat',
  35. 'ext': 'm4a',
  36. 'title': 'Electric Relaxation vol. 3',
  37. 'description': 'md5:2b8aec6adce69f9d41724647c65875e8',
  38. 'uploader': 'Daniel Drumz',
  39. 'uploader_id': 'gillespeterson',
  40. 'thumbnail': 're:https?://.*\.jpg',
  41. 'view_count': int,
  42. 'like_count': int,
  43. },
  44. }]
  45. def _get_url(self, track_id, template_url):
  46. server_count = 30
  47. for i in range(server_count):
  48. url = template_url % i
  49. try:
  50. # We only want to know if the request succeed
  51. # don't download the whole file
  52. self._request_webpage(
  53. HEADRequest(url), track_id,
  54. 'Checking URL %d/%d ...' % (i + 1, server_count + 1))
  55. return url
  56. except ExtractorError:
  57. pass
  58. return None
  59. def _real_extract(self, url):
  60. mobj = re.match(self._VALID_URL, url)
  61. uploader = mobj.group(1)
  62. cloudcast_name = mobj.group(2)
  63. track_id = compat_urllib_parse.unquote('-'.join((uploader, cloudcast_name)))
  64. webpage = self._download_webpage(url, track_id)
  65. preview_url = self._search_regex(
  66. r'\s(?:data-preview-url|m-preview)="([^"]+)"', webpage, 'preview url')
  67. song_url = preview_url.replace('/previews/', '/c/originals/')
  68. template_url = re.sub(r'(stream\d*)', 'stream%d', song_url)
  69. final_song_url = self._get_url(track_id, template_url)
  70. if final_song_url is None:
  71. self.to_screen('Trying with m4a extension')
  72. template_url = template_url.replace('.mp3', '.m4a').replace('originals/', 'm4a/64/')
  73. final_song_url = self._get_url(track_id, template_url)
  74. if final_song_url is None:
  75. raise ExtractorError('Unable to extract track url')
  76. PREFIX = (
  77. r'<span class="play-button[^"]*?"'
  78. r'(?:\s+[a-zA-Z0-9-]+(?:="[^"]+")?)*?\s+')
  79. title = self._html_search_regex(
  80. PREFIX + r'm-title="([^"]+)"', webpage, 'title')
  81. thumbnail = self._proto_relative_url(self._html_search_regex(
  82. PREFIX + r'm-thumbnail-url="([^"]+)"', webpage, 'thumbnail',
  83. fatal=False))
  84. uploader = self._html_search_regex(
  85. PREFIX + r'm-owner-name="([^"]+)"',
  86. webpage, 'uploader', fatal=False)
  87. uploader_id = self._search_regex(
  88. r'\s+"profile": "([^"]+)",', webpage, 'uploader id', fatal=False)
  89. description = self._og_search_description(webpage)
  90. like_count = str_to_int(self._search_regex(
  91. [r'<meta itemprop="interactionCount" content="UserLikes:([0-9]+)"',
  92. r'/favorites/?">([0-9]+)<'],
  93. webpage, 'like count', fatal=False))
  94. view_count = str_to_int(self._search_regex(
  95. [r'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"',
  96. r'/listeners/?">([0-9,.]+)</a>'],
  97. webpage, 'play count', fatal=False))
  98. timestamp = parse_iso8601(self._search_regex(
  99. r'<time itemprop="dateCreated" datetime="([^"]+)">',
  100. webpage, 'upload date', default=None))
  101. return {
  102. 'id': track_id,
  103. 'title': title,
  104. 'url': final_song_url,
  105. 'description': description,
  106. 'thumbnail': thumbnail,
  107. 'uploader': uploader,
  108. 'uploader_id': uploader_id,
  109. 'timestamp': timestamp,
  110. 'view_count': view_count,
  111. 'like_count': like_count,
  112. }