eighttracks.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import json
  2. import random
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. ExtractorError,
  6. )
  7. class EightTracksIE(InfoExtractor):
  8. IE_NAME = '8tracks'
  9. _VALID_URL = r'https?://8tracks\.com/(?P<user>[^/]+)/(?P<id>[^/#]+)(?:#.*)?$'
  10. _TEST = {
  11. 'name': 'EightTracks',
  12. 'url': 'http://8tracks.com/ytdl/youtube-dl-test-tracks-a',
  13. 'info_dict': {
  14. 'id': '1336550',
  15. 'display_id': 'youtube-dl-test-tracks-a',
  16. 'description': "test chars: \"'/\\ä↭",
  17. 'title': "youtube-dl test tracks \"'/\\ä↭<>",
  18. },
  19. 'playlist': [
  20. {
  21. 'md5': '96ce57f24389fc8734ce47f4c1abcc55',
  22. 'info_dict': {
  23. 'id': '11885610',
  24. 'ext': 'm4a',
  25. 'title': "youtue-dl project<>\"' - youtube-dl test track 1 \"'/\\\u00e4\u21ad",
  26. 'uploader_id': 'ytdl',
  27. },
  28. },
  29. {
  30. 'md5': '4ab26f05c1f7291ea460a3920be8021f',
  31. 'info_dict': {
  32. 'id': '11885608',
  33. 'ext': 'm4a',
  34. 'title': "youtube-dl project - youtube-dl test track 2 \"'/\\\u00e4\u21ad",
  35. 'uploader_id': 'ytdl',
  36. },
  37. },
  38. {
  39. 'md5': 'd30b5b5f74217410f4689605c35d1fd7',
  40. 'info_dict': {
  41. 'id': '11885679',
  42. 'ext': 'm4a',
  43. 'title': "youtube-dl project as well - youtube-dl test track 3 \"'/\\\u00e4\u21ad",
  44. 'uploader_id': 'ytdl',
  45. },
  46. },
  47. {
  48. 'md5': '4eb0a669317cd725f6bbd336a29f923a',
  49. 'info_dict': {
  50. 'id': '11885680',
  51. 'ext': 'm4a',
  52. 'title': "youtube-dl project as well - youtube-dl test track 4 \"'/\\\u00e4\u21ad",
  53. 'uploader_id': 'ytdl',
  54. },
  55. },
  56. {
  57. 'md5': '1893e872e263a2705558d1d319ad19e8',
  58. 'info_dict': {
  59. 'id': '11885682',
  60. 'ext': 'm4a',
  61. 'title': "PH - youtube-dl test track 5 \"'/\\\u00e4\u21ad",
  62. 'uploader_id': 'ytdl',
  63. },
  64. },
  65. {
  66. 'md5': 'b673c46f47a216ab1741ae8836af5899',
  67. 'info_dict': {
  68. 'id': '11885683',
  69. 'ext': 'm4a',
  70. 'title': "PH - youtube-dl test track 6 \"'/\\\u00e4\u21ad",
  71. 'uploader_id': 'ytdl',
  72. },
  73. },
  74. {
  75. 'md5': '1d74534e95df54986da7f5abf7d842b7',
  76. 'info_dict': {
  77. 'id': '11885684',
  78. 'ext': 'm4a',
  79. 'title': "phihag - youtube-dl test track 7 \"'/\\\u00e4\u21ad",
  80. 'uploader_id': 'ytdl',
  81. },
  82. },
  83. {
  84. 'md5': 'f081f47af8f6ae782ed131d38b9cd1c0',
  85. 'info_dict': {
  86. 'id': '11885685',
  87. 'ext': 'm4a',
  88. 'title': "phihag - youtube-dl test track 8 \"'/\\\u00e4\u21ad",
  89. 'uploader_id': 'ytdl',
  90. },
  91. },
  92. ],
  93. }
  94. def _real_extract(self, url):
  95. playlist_id = self._match_id(url)
  96. webpage = self._download_webpage(url, playlist_id)
  97. data = self._parse_json(
  98. self._search_regex(
  99. r'(?s)PAGE\.mix\s*=\s*({.+?});\n', webpage, 'trax information'),
  100. playlist_id)
  101. session = str(random.randint(0, 1000000000))
  102. mix_id = data['id']
  103. track_count = data['tracks_count']
  104. duration = data['duration']
  105. avg_song_duration = float(duration) / track_count
  106. # duration is sometimes negative, use predefined avg duration
  107. if avg_song_duration <= 0:
  108. avg_song_duration = 300
  109. first_url = f'http://8tracks.com/sets/{session}/play?player=sm&mix_id={mix_id}&format=jsonh'
  110. next_url = first_url
  111. entries = []
  112. for i in range(track_count):
  113. api_json = None
  114. download_tries = 0
  115. while api_json is None:
  116. try:
  117. api_json = self._download_webpage(
  118. next_url, playlist_id,
  119. note='Downloading song information %d/%d' % (i + 1, track_count),
  120. errnote='Failed to download song information')
  121. except ExtractorError:
  122. if download_tries > 3:
  123. raise
  124. else:
  125. download_tries += 1
  126. self._sleep(avg_song_duration, playlist_id)
  127. api_data = json.loads(api_json)
  128. track_data = api_data['set']['track']
  129. info = {
  130. 'id': str(track_data['id']),
  131. 'url': track_data['track_file_stream_url'],
  132. 'title': track_data['performer'] + ' - ' + track_data['name'],
  133. 'raw_title': track_data['name'],
  134. 'uploader_id': data['user']['login'],
  135. 'ext': 'm4a',
  136. }
  137. entries.append(info)
  138. next_url = 'http://8tracks.com/sets/{}/next?player=sm&mix_id={}&format=jsonh&track_id={}'.format(
  139. session, mix_id, track_data['id'])
  140. return {
  141. '_type': 'playlist',
  142. 'entries': entries,
  143. 'id': str(mix_id),
  144. 'display_id': playlist_id,
  145. 'title': data.get('name'),
  146. 'description': data.get('description'),
  147. }