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.

106 lines
4.0 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 = re.sub(r'audiocdn(\d+)', r'stream\1', preview_url)
  59. song_url = song_url.replace('/previews/', '/c/originals/')
  60. if not self._check_url(song_url, track_id, 'mp3'):
  61. song_url = song_url.replace('.mp3', '.m4a').replace('originals/', 'm4a/64/')
  62. if not self._check_url(song_url, track_id, 'm4a'):
  63. raise ExtractorError('Unable to extract track url')
  64. PREFIX = (
  65. r'm-play-on-spacebar[^>]+'
  66. r'(?:\s+[a-zA-Z0-9-]+(?:="[^"]+")?)*?\s+')
  67. title = self._html_search_regex(
  68. PREFIX + r'm-title="([^"]+)"', webpage, 'title')
  69. thumbnail = self._proto_relative_url(self._html_search_regex(
  70. PREFIX + r'm-thumbnail-url="([^"]+)"', webpage, 'thumbnail',
  71. fatal=False))
  72. uploader = self._html_search_regex(
  73. PREFIX + r'm-owner-name="([^"]+)"',
  74. webpage, 'uploader', fatal=False)
  75. uploader_id = self._search_regex(
  76. r'\s+"profile": "([^"]+)",', webpage, 'uploader id', fatal=False)
  77. description = self._og_search_description(webpage)
  78. like_count = str_to_int(self._search_regex(
  79. r'\bbutton-favorite\b[^>]+m-ajax-toggle-count="([^"]+)"',
  80. webpage, 'like count', fatal=False))
  81. view_count = str_to_int(self._search_regex(
  82. [r'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"',
  83. r'/listeners/?">([0-9,.]+)</a>'],
  84. webpage, 'play count', fatal=False))
  85. return {
  86. 'id': track_id,
  87. 'title': title,
  88. 'url': song_url,
  89. 'description': description,
  90. 'thumbnail': thumbnail,
  91. 'uploader': uploader,
  92. 'uploader_id': uploader_id,
  93. 'view_count': view_count,
  94. 'like_count': like_count,
  95. }