logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

eighttracks.py (5868B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import random
  5. from .common import InfoExtractor
  6. from ..compat import (
  7. compat_str,
  8. )
  9. from ..utils import (
  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. playlist_id = self._match_id(url)
  101. webpage = self._download_webpage(url, playlist_id)
  102. data = self._parse_json(
  103. self._search_regex(
  104. r"(?s)PAGE\.mix\s*=\s*({.+?});\n", webpage, 'trax information'),
  105. playlist_id)
  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 = float(duration) / track_count
  111. # duration is sometimes negative, use predefined avg duration
  112. if avg_song_duration <= 0:
  113. avg_song_duration = 300
  114. first_url = 'http://8tracks.com/sets/%s/play?player=sm&mix_id=%s&format=jsonh' % (session, mix_id)
  115. next_url = first_url
  116. entries = []
  117. for i in range(track_count):
  118. api_json = None
  119. download_tries = 0
  120. while api_json is None:
  121. try:
  122. api_json = self._download_webpage(
  123. next_url, playlist_id,
  124. note='Downloading song information %d/%d' % (i + 1, track_count),
  125. errnote='Failed to download song information')
  126. except ExtractorError:
  127. if download_tries > 3:
  128. raise
  129. else:
  130. download_tries += 1
  131. self._sleep(avg_song_duration, playlist_id)
  132. api_data = json.loads(api_json)
  133. track_data = api_data['set']['track']
  134. info = {
  135. 'id': compat_str(track_data['id']),
  136. 'url': track_data['track_file_stream_url'],
  137. 'title': track_data['performer'] + ' - ' + track_data['name'],
  138. 'raw_title': track_data['name'],
  139. 'uploader_id': data['user']['login'],
  140. 'ext': 'm4a',
  141. }
  142. entries.append(info)
  143. next_url = 'http://8tracks.com/sets/%s/next?player=sm&mix_id=%s&format=jsonh&track_id=%s' % (
  144. session, mix_id, track_data['id'])
  145. return {
  146. '_type': 'playlist',
  147. 'entries': entries,
  148. 'id': compat_str(mix_id),
  149. 'display_id': playlist_id,
  150. 'title': data.get('name'),
  151. 'description': data.get('description'),
  152. }