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.

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