eighttracks.py 5.7 KB

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