Advertisement
tikimyster

Song Request Via Flask

Feb 24th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import json
  3. from flask import Flask, request, redirect
  4. import requests, base64, os.path
  5.  
  6. username = "YOUR_SPOTIFY_USERNAME"
  7. playlist_id = "YOUR_PUBLICK_PLAYLIST_ID"
  8.  
  9. app = Flask(__name__)
  10.  
  11. # Authorization code flow
  12. url = "https://accounts.spotify.com/authorize"
  13. CLIENT_ID = "YOUR_APP_CLIENT_ID"
  14. REDIRECT_URI = "http://127.0.0.1:8080/callback"
  15. SCOPE = "playlist-modify-public"
  16. CLIENT_SECRET = "YOUR_APP_SECRET_ID"
  17.  
  18. @app.route('/')
  19. def index():
  20.     return redirect(
  21.         "https://accounts.spotify.com/authorize/?client_id=" + CLIENT_ID + "&response_type=code&redirect_uri=http%3A%2F%2F127.0.0.1%3A8080%2Fcallback&scope=" + SCOPE)
  22.  
  23.  
  24. @app.route("/callback")
  25. def callback():
  26.     access_token = request.args
  27.     code_payload = {"grant_type": "authorization_code", "code": str(access_token['code']), "redirect_uri": REDIRECT_URI}
  28.     base64encoded = base64.b64encode(bytes(CLIENT_ID + ":" + CLIENT_SECRET, "UTF-8"))
  29.     headers = {"Authorization": "Basic %s" % base64encoded.decode('utf-8')}
  30.     post_request = requests.post("https://accounts.spotify.com/api/token", data=code_payload, headers=headers)
  31.     json_response = json.loads(post_request.text)
  32.  
  33.  
  34.  
  35.     #song = json.loads(open(os.path.dirname(__file__) + '/add_song.json',"r", encoding="utf-8-sig", errors="ignore").read())
  36.     #track_ids = song[u"track"][u"id"]
  37.  
  38.      track_ids = "TRACK_ID_OF_SONG_YOU_WANT_TO_ADD"
  39.  
  40.  
  41.     add_req = requests.post(
  42.         "https://api.spotify.com/v1/users/" + username + "/playlists/" + playlist_id + "/tracks?uris=spotify:track:" + track_ids,
  43.         data=code_payload, headers={'Authorization': 'Bearer ' + json_response[u'access_token']})
  44.     return "Song Added"
  45.  
  46. if __name__ == "__main__":
  47.     app.run(debug=True, port=8080)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement