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 ..utils import (
  5. compat_urllib_parse,
  6. ExtractorError,
  7. int_or_none,
  8. parse_iso8601,
  9. )
  10. class MixcloudIE(InfoExtractor):
  11. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/([^/]+)/([^/]+)'
  12. IE_NAME = 'mixcloud'
  13. _TEST = {
  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. 'upload_date': '20111115',
  23. 'timestamp': 1321359578,
  24. 'thumbnail': 're:https?://.*\.jpg',
  25. 'view_count': int,
  26. 'like_count': int,
  27. },
  28. }
  29. def check_urls(self, url_list):
  30. """Returns 1st active url from list"""
  31. for url in url_list:
  32. try:
  33. # We only want to know if the request succeed
  34. # don't download the whole file
  35. self._request_webpage(url, None, False)
  36. return url
  37. except ExtractorError:
  38. url = None
  39. return None
  40. def _get_url(self, template_url):
  41. return self.check_urls(template_url % i for i in range(30))
  42. def _real_extract(self, url):
  43. mobj = re.match(self._VALID_URL, url)
  44. uploader = mobj.group(1)
  45. cloudcast_name = mobj.group(2)
  46. track_id = compat_urllib_parse.unquote('-'.join((uploader, cloudcast_name)))
  47. webpage = self._download_webpage(url, track_id)
  48. preview_url = self._search_regex(
  49. r'\s(?:data-preview-url|m-preview)="(.+?)"', webpage, 'preview url')
  50. song_url = preview_url.replace('/previews/', '/c/originals/')
  51. template_url = re.sub(r'(stream\d*)', 'stream%d', song_url)
  52. final_song_url = self._get_url(template_url)
  53. if final_song_url is None:
  54. self.to_screen('Trying with m4a extension')
  55. template_url = template_url.replace('.mp3', '.m4a').replace('originals/', 'm4a/64/')
  56. final_song_url = self._get_url(template_url)
  57. if final_song_url is None:
  58. raise ExtractorError('Unable to extract track url')
  59. PREFIX = (
  60. r'<div class="cloudcast-play-button-container"'
  61. r'(?:\s+[a-zA-Z0-9-]+(?:="[^"]+")?)*?\s+')
  62. title = self._html_search_regex(
  63. PREFIX + r'm-title="([^"]+)"', webpage, 'title')
  64. thumbnail = self._proto_relative_url(self._html_search_regex(
  65. PREFIX + r'm-thumbnail-url="([^"]+)"', webpage, 'thumbnail',
  66. fatal=False))
  67. uploader = self._html_search_regex(
  68. PREFIX + r'm-owner-name="([^"]+)"',
  69. webpage, 'uploader', fatal=False)
  70. uploader_id = self._search_regex(
  71. r'\s+"profile": "([^"]+)",', webpage, 'uploader id', fatal=False)
  72. description = self._og_search_description(webpage)
  73. like_count = int_or_none(self._search_regex(
  74. r'<meta itemprop="interactionCount" content="UserLikes:([0-9]+)"',
  75. webpage, 'like count', fatal=False))
  76. view_count = int_or_none(self._search_regex(
  77. r'<meta itemprop="interactionCount" content="UserPlays:([0-9]+)"',
  78. webpage, 'play count', fatal=False))
  79. timestamp = parse_iso8601(self._search_regex(
  80. r'<time itemprop="dateCreated" datetime="([^"]+)">',
  81. webpage, 'upload date'))
  82. return {
  83. 'id': track_id,
  84. 'title': title,
  85. 'url': final_song_url,
  86. 'description': description,
  87. 'thumbnail': thumbnail,
  88. 'uploader': uploader,
  89. 'uploader_id': uploader_id,
  90. 'timestamp': timestamp,
  91. 'view_count': view_count,
  92. 'like_count': like_count,
  93. }