Advertisement
Guest User

some code

a guest
Nov 14th, 2019
1,798
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. import sys
  2. import spotipy
  3. import spotipy.util as util
  4. import requests
  5. import matplotlib.pyplot as plt
  6.  
  7.  
  8. scope = 'user-library-read'
  9.  
  10. username = ("kittisama")
  11.  
  12. token = util.prompt_for_user_token(username,scope,client_id='05d08001f4b041249642bbd39c258de7',client_secret='2232811290004b4287ec4401692337df',redirect_uri='https://www.youtube.com')
  13. #sp.audio_analysis knows: duration (secs), loudness (db), tempo (bpm), tempo_confidence (%), time_signature, time_signature confidence (%), key, key_confidence (%), mode, mode_confidence (%)
  14. #sp.audio_features knows: danceability, energy, key, loudness, mode, speechiness, acousticness, instrumnetalness, liveness, valence, tempo, time_signature, duration
  15.  
  16.  
  17. loudness = {}
  18. tempo = {}
  19. albums = []
  20.  
  21.  
  22. if token:
  23. sp = spotipy.Spotify(auth=token)
  24.  
  25. results = sp.current_user_saved_albums(2) #max limit 51
  26. for item in results['items']:
  27. print(item['album']['name'])
  28. print(item['album']['artists'][0]['name'])
  29. print("\n")
  30.  
  31. loudness[item['album']['name']] = []
  32. tempo[item['album']['name']] = []
  33. albums.append(item['album']['name'])
  34.  
  35. trackList = sp.album_tracks(item['album']['id'])
  36.  
  37. for track in trackList['items']:
  38. print("\n\n" + track['name'])
  39. print()
  40.  
  41. data = (sp.audio_analysis(track['id'])['track'])
  42.  
  43. #print(sp.audio_features(track['id']))
  44.  
  45. loudness[item['album']['name']].append(data['loudness'])
  46. tempo[item['album']['name']].append(data['tempo'])
  47.  
  48. print(sp.audio_features(track['id'])[0]['tempo'])
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. print("duration: " + str(data['duration']))
  56. print("loudness: " + str(data['loudness']))
  57. print("tempo: " + str(data['tempo']))
  58. print("time_signature: " + str(data['time_signature']))
  59. print("key: " + str(data['key']))
  60. print("mode: " + str(data['mode']))
  61.  
  62. existingColors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
  63.  
  64. toShow = []
  65.  
  66. for integer in range(0, len(albums)):
  67. line = plt.plot(tempo[albums[integer]], loudness[albums[integer]], "or", label=albums[integer])
  68. plt.legend()
  69.  
  70. plt.show()
  71.  
  72. if False:
  73. if not url == None:
  74. try:
  75. try:
  76. print("now saving: " + track['name'])
  77. print(url)
  78. r = requests.get(url, allow_redirects=True)
  79. open(track['name'] + '.mp3', 'wb').write(r.content)
  80. except OSError:
  81. pass
  82. except FileNotFoundError:
  83. pass
  84.  
  85. else:
  86. print("Can't get token for", username)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement