eighttracks.py 5.7 KB

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