mirror of
https://github.com/c0de-archive/spotipy-experiments.git
synced 2024-12-22 01:32:40 +00:00
Initial experiments complete
Here I learned how to authenticate my app over env, as well as trigger an oauth login for the username specified I created a small script that will write the now playing track info to a file This is useful for keeping track of the song that is playing at all times
This commit is contained in:
parent
bcd6fa5b80
commit
7757235dbb
3
.gitignore
vendored
3
.gitignore
vendored
@ -1 +1,4 @@
|
|||||||
venv
|
venv
|
||||||
|
.cache*
|
||||||
|
*.swp
|
||||||
|
.DS_Store
|
||||||
|
30
README.md
30
README.md
@ -1 +1,29 @@
|
|||||||
Spotify_streamUpdater
|
#Spotify API Experiments
|
||||||
|
|
||||||
|
This is a collection of experimentations with the Spotipy python library.
|
||||||
|
All of these scripts will request an auth token for a particular user (username arg) upon startup
|
||||||
|
From my experimentation so far, this will remmeber that the app was previously authorized for whatever "authorization scope" was approved by the user at the oauth screen
|
||||||
|
|
||||||
|
save_now_playing.py - This script will poll for the user's now playing track every minute and update a text file.
|
||||||
|
get_user_auth.py - "Hello World" from the docs, this shows how to authorize using env
|
||||||
|
show_all_playlist.py - Unlike the name suggests, I did not see all my playlists. This could be an auth scope issue however
|
||||||
|
|
||||||
|
To get started:
|
||||||
|
* Create an app on Spotify's Developer console - https://beta.developer.spotify.com
|
||||||
|
* Clone this repository
|
||||||
|
* Ensure that you have "python-pip", and "python-virtualenv" installed
|
||||||
|
* Run `virtualenv venv` to create a new python environment
|
||||||
|
* Edit "set_env.sh" to configure your API ID, Secret, and redirect using the information from your previously created app
|
||||||
|
* Configure your environment by running `set_env.sh`
|
||||||
|
* Run `pip install -r requirements.txt` to install the required dependencies
|
||||||
|
* Run any of the scripts to use them
|
||||||
|
|
||||||
|
License: MIT
|
||||||
|
Copyright (c) 2014 Paul Lamere - https://github.com/plamere/spotipy
|
||||||
|
Copyright (c) 2018 c0de <c0defox.es, gama.io>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
get_user_auth.py
Normal file
22
get_user_auth.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import sys
|
||||||
|
import spotipy
|
||||||
|
import spotipy.util as util
|
||||||
|
|
||||||
|
scope = 'user-library-read'
|
||||||
|
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
username = sys.argv[1]
|
||||||
|
else:
|
||||||
|
print "Usage: %s username" % (sys.argv[0],)
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
token = util.prompt_for_user_token(username, scope)
|
||||||
|
|
||||||
|
if token:
|
||||||
|
sp = spotipy.Spotify(auth=token)
|
||||||
|
results = sp.current_user_saved_tracks()
|
||||||
|
for item in results['items']:
|
||||||
|
track = item['track']
|
||||||
|
print track['name'] + ' - ' + track['artists'][0]['name']
|
||||||
|
else:
|
||||||
|
print "Can't get token for", username
|
0
requirements.txt
Normal file
0
requirements.txt
Normal file
54
save_now_playing.py
Normal file
54
save_now_playing.py
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# Writes the now playing track to a text file
|
||||||
|
# License: MIT
|
||||||
|
# Copyright (c) 2018 c0de <c0defox.es>
|
||||||
|
|
||||||
|
import spotipy
|
||||||
|
import spotipy.util as util
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
|
||||||
|
if len(sys.argv) > 2:
|
||||||
|
username = sys.argv[1]
|
||||||
|
outfile = sys.argv[2]
|
||||||
|
else:
|
||||||
|
print "Usage: %s username outfile" % sys.argv[0]
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
def get_now_playing(sp):
|
||||||
|
# This should be sp.currently_playing(), but somehow does not work...
|
||||||
|
# So I its return: https://github.com/plamere/spotipy/blob/master/spotipy/client.py#L899
|
||||||
|
# Get the currently playing track and artist
|
||||||
|
np = sp._get("me/player/currently-playing", market=None)
|
||||||
|
return "%s by: %s" % (np['item']['name'], np['item']['artists'][0]['name'])
|
||||||
|
|
||||||
|
# Authorization Scope, can possibly be a dict?
|
||||||
|
scope = 'user-read-currently-playing'
|
||||||
|
|
||||||
|
# User Auth-Token - Achieved by running a webserver on localhost (might not be needed) as we just paste the URL here
|
||||||
|
token = util.prompt_for_user_token(username, scope)
|
||||||
|
|
||||||
|
if token:
|
||||||
|
# Authorize with the API
|
||||||
|
sp = spotipy.Spotify(auth=token)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
np = get_now_playing(sp)
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(outfile, 'w') as output:
|
||||||
|
output.write(np)
|
||||||
|
print "[%.2fs] %s" % (time.time()-start_time, np)
|
||||||
|
output.close()
|
||||||
|
except:
|
||||||
|
print "[%.2fs] Unable to open %s" % (time.time()-start_time, outfile)
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
# Wait 1 minute before checking again
|
||||||
|
time.sleep(60.0 - ((time.time() - start_time) % 60.0))
|
||||||
|
else:
|
||||||
|
print "Can't get a token for %s" % username
|
||||||
|
sys.exit()
|
17
set_env.sh
Normal file
17
set_env.sh
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Client ID and Secret can be found at:
|
||||||
|
# https://beta.developer.spotify.com/dashboard/applications/
|
||||||
|
|
||||||
|
# Activate Python Virtualenv
|
||||||
|
# Ensure that you have previously set this up with `virtualenv venv`
|
||||||
|
# Select only one of the below
|
||||||
|
source venv/bin/activate # For Linux/macOS/*nix Users
|
||||||
|
# source venv/Scripts/activate # For Windows Users
|
||||||
|
|
||||||
|
# API Auth Stuffs
|
||||||
|
export SPOTIPY_CLIENT_ID='YOUR_CLIENT_ID_HERE'
|
||||||
|
export SPOTIPY_CLIENT_SECRET='YOUR_CLIENT_SECRET_HERE'
|
||||||
|
# Response from Spotify API after authorization
|
||||||
|
# Not sure if this actually requires a local web server
|
||||||
|
export SPOTIPY_REDIRECT_URI='http://localhost/'
|
41
show_all_playlist.py
Normal file
41
show_all_playlist.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
# shows a user's playlists (need to be authenticated via oauth)
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import spotipy
|
||||||
|
import spotipy.util as util
|
||||||
|
|
||||||
|
def show_tracks(tracks):
|
||||||
|
for i, item in enumerate(tracks['items']):
|
||||||
|
track = item['track']
|
||||||
|
print " %d %32.32s %s" % (i, track['artists'][0]['name'],
|
||||||
|
track['name'])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
username = sys.argv[1]
|
||||||
|
else:
|
||||||
|
print "Whoops, need your username!"
|
||||||
|
print "usage: python user_playlists.py [username]"
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
scope = 'user-library-read'
|
||||||
|
token = util.prompt_for_user_token(username, scope)
|
||||||
|
|
||||||
|
if token:
|
||||||
|
sp = spotipy.Spotify(auth=token)
|
||||||
|
playlists = sp.user_playlists(username)
|
||||||
|
for playlist in playlists['items']:
|
||||||
|
if playlist['owner']['id'] == username:
|
||||||
|
print
|
||||||
|
print playlist['name']
|
||||||
|
print ' total tracks', playlist['tracks']['total']
|
||||||
|
results = sp.user_playlist(username, playlist['id'],
|
||||||
|
fields="tracks,next")
|
||||||
|
tracks = results['tracks']
|
||||||
|
show_tracks(tracks)
|
||||||
|
while tracks['next']:
|
||||||
|
tracks = sp.next(tracks)
|
||||||
|
show_tracks(tracks)
|
||||||
|
else:
|
||||||
|
print "Can't get token for", username
|
Loading…
Reference in New Issue
Block a user