diff --git a/.gitignore b/.gitignore index 5ceb386..6d76606 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ venv +.cache* +*.swp +.DS_Store diff --git a/README.md b/README.md index 8ee6139..7f34d25 100644 --- a/README.md +++ b/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 + +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. \ No newline at end of file diff --git a/get_user_auth.py b/get_user_auth.py new file mode 100644 index 0000000..8c9ec17 --- /dev/null +++ b/get_user_auth.py @@ -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 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/save_now_playing.py b/save_now_playing.py new file mode 100644 index 0000000..a2d8861 --- /dev/null +++ b/save_now_playing.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# Writes the now playing track to a text file +# License: MIT +# Copyright (c) 2018 c0de + +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() diff --git a/set_env.sh b/set_env.sh new file mode 100644 index 0000000..c6f2e62 --- /dev/null +++ b/set_env.sh @@ -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/' diff --git a/show_all_playlist.py b/show_all_playlist.py new file mode 100644 index 0000000..50a666a --- /dev/null +++ b/show_all_playlist.py @@ -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 \ No newline at end of file