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
3.9 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. )
  12. class MixcloudIE(InfoExtractor):
  13. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/([^/]+)/([^/]+)'
  14. IE_NAME = 'mixcloud'
  15. _TESTS = [{
  16. 'url': 'http://www.mixcloud.com/dholbach/cryptkeeper/',
  17. 'info_dict': {
  18. 'id': 'dholbach-cryptkeeper',
  19. 'ext': 'mp3',
  20. 'title': 'Cryptkeeper',
  21. 'description': 'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.',
  22. 'uploader': 'Daniel Holbach',
  23. 'uploader_id': 'dholbach',
  24. 'thumbnail': 're:https?://.*\.jpg',
  25. 'view_count': int,
  26. 'like_count': int,
  27. },
  28. }, {
  29. 'url': 'http://www.mixcloud.com/gillespeterson/caribou-7-inch-vinyl-mix-chat/',
  30. 'info_dict': {
  31. 'id': 'gillespeterson-caribou-7-inch-vinyl-mix-chat',
  32. 'ext': 'mp3',
  33. 'title': 'Caribou 7 inch Vinyl Mix & Chat',
  34. 'description': 'md5:2b8aec6adce69f9d41724647c65875e8',
  35. 'uploader': 'Gilles Peterson Worldwide',
  36. 'uploader_id': 'gillespeterson',
  37. 'thumbnail': 're:https?://.*/images/',
  38. 'view_count': int,
  39. 'like_count': int,
  40. },
  41. }]
  42. def _check_url(self, url, track_id, ext):
  43. try:
  44. # We only want to know if the request succeed
  45. # don't download the whole file
  46. self._request_webpage(
  47. HEADRequest(url), track_id,
  48. 'Trying %s URL' % ext)
  49. return True
  50. except ExtractorError:
  51. return False
  52. def _real_extract(self, url):
  53. mobj = re.match(self._VALID_URL, url)
  54. uploader = mobj.group(1)
  55. cloudcast_name = mobj.group(2)
  56. track_id = compat_urllib_parse.unquote('-'.join((uploader, cloudcast_name)))
  57. webpage = self._download_webpage(url, track_id)
  58. preview_url = self._search_regex(
  59. r'\s(?:data-preview-url|m-preview)="([^"]+)"', webpage, 'preview url')
  60. song_url = preview_url.replace('/previews/', '/c/originals/')
  61. if not self._check_url(song_url, track_id, 'mp3'):
  62. song_url = song_url.replace('.mp3', '.m4a').replace('originals/', 'm4a/64/')
  63. if not self._check_url(song_url, track_id, 'm4a'):
  64. raise ExtractorError('Unable to extract track url')
  65. PREFIX = (
  66. r'm-play-on-spacebar[^>]+'
  67. r'(?:\s+[a-zA-Z0-9-]+(?:="[^"]+")?)*?\s+')
  68. title = self._html_search_regex(
  69. PREFIX + r'm-title="([^"]+)"', webpage, 'title')
  70. thumbnail = self._proto_relative_url(self._html_search_regex(
  71. PREFIX + r'm-thumbnail-url="([^"]+)"', webpage, 'thumbnail',
  72. fatal=False))
  73. uploader = self._html_search_regex(
  74. PREFIX + r'm-owner-name="([^"]+)"',
  75. webpage, 'uploader', fatal=False)
  76. uploader_id = self._search_regex(
  77. r'\s+"profile": "([^"]+)",', webpage, 'uploader id', fatal=False)
  78. description = self._og_search_description(webpage)
  79. like_count = str_to_int(self._search_regex(
  80. r'\bbutton-favorite\b[^>]+m-ajax-toggle-count="([^"]+)"',
  81. webpage, 'like count', fatal=False))
  82. view_count = str_to_int(self._search_regex(
  83. [r'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"',
  84. r'/listeners/?">([0-9,.]+)</a>'],
  85. webpage, 'play count', fatal=False))
  86. return {
  87. 'id': track_id,
  88. 'title': title,
  89. 'url': song_url,
  90. 'description': description,
  91. 'thumbnail': thumbnail,
  92. 'uploader': uploader,
  93. 'uploader_id': uploader_id,
  94. 'view_count': view_count,
  95. 'like_count': like_count,
  96. }