beatportpro.py 3.3 KB

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