beatport.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import int_or_none
  4. class BeatportIE(InfoExtractor):
  5. _VALID_URL = r'https?://(?:www\.|pro\.)?beatport\.com/track/(?P<display_id>[^/]+)/(?P<id>[0-9]+)'
  6. _TESTS = [{
  7. 'url': 'https://beatport.com/track/synesthesia-original-mix/5379371',
  8. 'md5': 'b3c34d8639a2f6a7f734382358478887',
  9. 'info_dict': {
  10. 'id': '5379371',
  11. 'display_id': 'synesthesia-original-mix',
  12. 'ext': 'mp4',
  13. 'title': 'Froxic - Synesthesia (Original Mix)',
  14. },
  15. }, {
  16. 'url': 'https://beatport.com/track/love-and-war-original-mix/3756896',
  17. 'md5': 'e44c3025dfa38c6577fbaeb43da43514',
  18. 'info_dict': {
  19. 'id': '3756896',
  20. 'display_id': 'love-and-war-original-mix',
  21. 'ext': 'mp3',
  22. 'title': 'Wolfgang Gartner - Love & War (Original Mix)',
  23. },
  24. }, {
  25. 'url': 'https://beatport.com/track/birds-original-mix/4991738',
  26. 'md5': 'a1fd8e8046de3950fd039304c186c05f',
  27. 'info_dict': {
  28. 'id': '4991738',
  29. 'display_id': 'birds-original-mix',
  30. 'ext': 'mp4',
  31. 'title': "Tos, Middle Milk, Mumblin' Johnsson - Birds (Original Mix)",
  32. },
  33. }]
  34. def _real_extract(self, url):
  35. mobj = self._match_valid_url(url)
  36. track_id = mobj.group('id')
  37. display_id = mobj.group('display_id')
  38. webpage = self._download_webpage(url, display_id)
  39. playables = self._parse_json(
  40. self._search_regex(
  41. r'window\.Playables\s*=\s*({.+?});', webpage,
  42. 'playables info', flags=re.DOTALL),
  43. track_id)
  44. track = next(t for t in playables['tracks'] if t['id'] == int(track_id))
  45. title = ', '.join(a['name'] for a in track['artists']) + ' - ' + track['name']
  46. if track['mix']:
  47. title += ' (' + track['mix'] + ')'
  48. formats = []
  49. for ext, info in track['preview'].items():
  50. if not info['url']:
  51. continue
  52. fmt = {
  53. 'url': info['url'],
  54. 'ext': ext,
  55. 'format_id': ext,
  56. 'vcodec': 'none',
  57. }
  58. if ext == 'mp3':
  59. fmt['acodec'] = 'mp3'
  60. fmt['abr'] = 96
  61. fmt['asr'] = 44100
  62. elif ext == 'mp4':
  63. fmt['acodec'] = 'aac'
  64. fmt['abr'] = 96
  65. fmt['asr'] = 44100
  66. formats.append(fmt)
  67. images = []
  68. for name, info in track['images'].items():
  69. image_url = info.get('url')
  70. if name == 'dynamic' or not image_url:
  71. continue
  72. image = {
  73. 'id': name,
  74. 'url': image_url,
  75. 'height': int_or_none(info.get('height')),
  76. 'width': int_or_none(info.get('width')),
  77. }
  78. images.append(image)
  79. return {
  80. 'id': str(track.get('id')) or track_id,
  81. 'display_id': track.get('slug') or display_id,
  82. 'title': title,
  83. 'formats': formats,
  84. 'thumbnails': images,
  85. }