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.1 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import compat_urllib_parse_unquote
  5. from ..utils import (
  6. ExtractorError,
  7. HEADRequest,
  8. parse_count,
  9. str_to_int,
  10. )
  11. class MixcloudIE(InfoExtractor):
  12. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/([^/]+)/([^/]+)'
  13. IE_NAME = 'mixcloud'
  14. _TESTS = [{
  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. 'thumbnail': 're:https?://.*\.jpg',
  24. 'view_count': int,
  25. 'like_count': int,
  26. },
  27. }, {
  28. 'url': 'http://www.mixcloud.com/gillespeterson/caribou-7-inch-vinyl-mix-chat/',
  29. 'info_dict': {
  30. 'id': 'gillespeterson-caribou-7-inch-vinyl-mix-chat',
  31. 'ext': 'mp3',
  32. 'title': 'Caribou 7 inch Vinyl Mix & Chat',
  33. 'description': 'md5:2b8aec6adce69f9d41724647c65875e8',
  34. 'uploader': 'Gilles Peterson Worldwide',
  35. 'uploader_id': 'gillespeterson',
  36. 'thumbnail': 're:https?://.*/images/',
  37. 'view_count': int,
  38. 'like_count': int,
  39. },
  40. }]
  41. def _check_url(self, url, track_id, ext):
  42. try:
  43. # We only want to know if the request succeed
  44. # don't download the whole file
  45. self._request_webpage(
  46. HEADRequest(url), track_id,
  47. 'Trying %s URL' % ext)
  48. return True
  49. except ExtractorError:
  50. return False
  51. def _real_extract(self, url):
  52. mobj = re.match(self._VALID_URL, url)
  53. uploader = mobj.group(1)
  54. cloudcast_name = mobj.group(2)
  55. track_id = compat_urllib_parse_unquote('-'.join((uploader, cloudcast_name)))
  56. webpage = self._download_webpage(url, track_id)
  57. preview_url = self._search_regex(
  58. r'\s(?:data-preview-url|m-preview)="([^"]+)"', webpage, 'preview url')
  59. song_url = re.sub(r'audiocdn(\d+)', r'stream\1', preview_url)
  60. song_url = song_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 = parse_count(self._search_regex(
  80. r'\bbutton-favorite[^>]+>.*?<span[^>]+class=["\']toggle-number[^>]+>\s*([^<]+)',
  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. }