mixcloud.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. int_or_none,
  11. str_to_int,
  12. parse_iso8601,
  13. )
  14. class MixcloudIE(InfoExtractor):
  15. _VALID_URL = r'^(?:https?://)?(?:www\.)?mixcloud\.com/([^/]+)/([^/]+)'
  16. IE_NAME = 'mixcloud'
  17. _TEST = {
  18. 'url': 'http://www.mixcloud.com/dholbach/cryptkeeper/',
  19. 'info_dict': {
  20. 'id': 'dholbach-cryptkeeper',
  21. 'ext': 'mp3',
  22. 'title': 'Cryptkeeper',
  23. 'description': 'After quite a long silence from myself, finally another Drum\'n\'Bass mix with my favourite current dance floor bangers.',
  24. 'uploader': 'Daniel Holbach',
  25. 'uploader_id': 'dholbach',
  26. 'upload_date': '20111115',
  27. 'timestamp': 1321359578,
  28. 'thumbnail': 're:https?://.*\.jpg',
  29. 'view_count': int,
  30. 'like_count': int,
  31. },
  32. }
  33. def _get_url(self, track_id, template_url):
  34. server_count = 30
  35. for i in range(server_count):
  36. url = template_url % i
  37. try:
  38. # We only want to know if the request succeed
  39. # don't download the whole file
  40. self._request_webpage(
  41. HEADRequest(url), track_id,
  42. 'Checking URL %d/%d ...' % (i + 1, server_count + 1))
  43. return url
  44. except ExtractorError:
  45. pass
  46. return None
  47. def _real_extract(self, url):
  48. mobj = re.match(self._VALID_URL, url)
  49. uploader = mobj.group(1)
  50. cloudcast_name = mobj.group(2)
  51. track_id = compat_urllib_parse.unquote('-'.join((uploader, cloudcast_name)))
  52. webpage = self._download_webpage(url, track_id)
  53. preview_url = self._search_regex(
  54. r'\s(?:data-preview-url|m-preview)="(.+?)"', webpage, 'preview url')
  55. song_url = preview_url.replace('/previews/', '/c/originals/')
  56. template_url = re.sub(r'(stream\d*)', 'stream%d', song_url)
  57. final_song_url = self._get_url(track_id, template_url)
  58. if final_song_url is None:
  59. self.to_screen('Trying with m4a extension')
  60. template_url = template_url.replace('.mp3', '.m4a').replace('originals/', 'm4a/64/')
  61. final_song_url = self._get_url(track_id, template_url)
  62. if final_song_url is None:
  63. raise ExtractorError('Unable to extract track url')
  64. PREFIX = (
  65. r'<span class="play-button[^"]*?"'
  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'<meta itemprop="interactionCount" content="UserLikes:([0-9]+)"',
  80. r'/favorites/?">([0-9]+)<'],
  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. timestamp = parse_iso8601(self._search_regex(
  87. r'<time itemprop="dateCreated" datetime="([^"]+)">',
  88. webpage, 'upload date', default=None))
  89. return {
  90. 'id': track_id,
  91. 'title': title,
  92. 'url': final_song_url,
  93. 'description': description,
  94. 'thumbnail': thumbnail,
  95. 'uploader': uploader,
  96. 'uploader_id': uploader_id,
  97. 'timestamp': timestamp,
  98. 'view_count': view_count,
  99. 'like_count': like_count,
  100. }