You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
5.6 KiB

[8tracks] Use track count instead of looking at at_last_track property This fixes the error: $ youtube-dl http://8tracks.com/vladmc/counting-stars [8tracks] counting-stars: Downloading webpage [8tracks] counting-stars: Downloading song information 1/4 [8tracks] counting-stars: Downloading song information 2/4 [8tracks] counting-stars: Downloading song information 3/4 [8tracks] counting-stars: Downloading song information 4/4 [8tracks] counting-stars: Downloading song information 5/4 Traceback (most recent call last): File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main "__main__", fname, loader, pkg_name) File "/usr/lib/python2.7/runpy.py", line 72, in _run_code exec code in run_globals File "/home/phihag/projects/youtube-dl/youtube_dl/__main__.py", line 18, in <module> youtube_dl.main() File "/home/phihag/projects/youtube-dl/youtube_dl/__init__.py", line 761, in main _real_main(argv) File "/home/phihag/projects/youtube-dl/youtube_dl/__init__.py", line 714, in _real_main retcode = ydl.download(all_urls) File "/home/phihag/projects/youtube-dl/youtube_dl/YoutubeDL.py", line 701, in download videos = self.extract_info(url) File "/home/phihag/projects/youtube-dl/youtube_dl/YoutubeDL.py", line 342, in extract_info ie_result = ie.extract(url) File "/home/phihag/projects/youtube-dl/youtube_dl/extractor/common.py", line 121, in extract return self._real_extract(url) File "/home/phihag/projects/youtube-dl/youtube_dl/extractor/eighttracks.py", line 111, in _real_extract 'id': track_data['id'], KeyError: 'id'
11 years ago
  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. mobj = re.match(self._VALID_URL, url)
  102. playlist_id = mobj.group('id')
  103. webpage = self._download_webpage(url, playlist_id)
  104. json_like = self._search_regex(
  105. r"(?s)PAGE.mix = (.*?);\n", webpage, 'trax information')
  106. data = json.loads(json_like)
  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. 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. }