eighttracks.py 5.6 KB

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