logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

beatport.py (3419B)


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