Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import spotipy
- import spotipy.util as util
- import requests
- import matplotlib.pyplot as plt
- scope = 'user-library-read'
- username = ("kittisama")
- token = util.prompt_for_user_token(username,scope,client_id='05d08001f4b041249642bbd39c258de7',client_secret='2232811290004b4287ec4401692337df',redirect_uri='https://www.youtube.com')
- #sp.audio_analysis knows: duration (secs), loudness (db), tempo (bpm), tempo_confidence (%), time_signature, time_signature confidence (%), key, key_confidence (%), mode, mode_confidence (%)
- #sp.audio_features knows: danceability, energy, key, loudness, mode, speechiness, acousticness, instrumnetalness, liveness, valence, tempo, time_signature, duration
- loudness = {}
- tempo = {}
- albums = []
- if token:
- sp = spotipy.Spotify(auth=token)
- results = sp.current_user_saved_albums(2) #max limit 51
- for item in results['items']:
- print(item['album']['name'])
- print(item['album']['artists'][0]['name'])
- print("\n")
- loudness[item['album']['name']] = []
- tempo[item['album']['name']] = []
- albums.append(item['album']['name'])
- trackList = sp.album_tracks(item['album']['id'])
- for track in trackList['items']:
- print("\n\n" + track['name'])
- print()
- data = (sp.audio_analysis(track['id'])['track'])
- #print(sp.audio_features(track['id']))
- loudness[item['album']['name']].append(data['loudness'])
- tempo[item['album']['name']].append(data['tempo'])
- print(sp.audio_features(track['id'])[0]['tempo'])
- print("duration: " + str(data['duration']))
- print("loudness: " + str(data['loudness']))
- print("tempo: " + str(data['tempo']))
- print("time_signature: " + str(data['time_signature']))
- print("key: " + str(data['key']))
- print("mode: " + str(data['mode']))
- existingColors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
- toShow = []
- for integer in range(0, len(albums)):
- line = plt.plot(tempo[albums[integer]], loudness[albums[integer]], "or", label=albums[integer])
- plt.legend()
- plt.show()
- if False:
- if not url == None:
- try:
- try:
- print("now saving: " + track['name'])
- print(url)
- r = requests.get(url, allow_redirects=True)
- open(track['name'] + '.mp3', 'wb').write(r.content)
- except OSError:
- pass
- except FileNotFoundError:
- pass
- else:
- print("Can't get token for", username)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement