Skip to content

Instantly share code, notes, and snippets.

@gilhooleyd
Created August 8, 2017 00:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilhooleyd/a543e6fc9539f9153200eca32970bae5 to your computer and use it in GitHub Desktop.
Save gilhooleyd/a543e6fc9539f9153200eca32970bae5 to your computer and use it in GitHub Desktop.
Siri Control + Spotify
import sys
import os
import subprocess
import spotipy
import spotipy.util as util
import json
#This is name of the module - it can be anything you want
moduleName = "spotify"
#These are the words you must say for this module to be executed
commandWords = ["spotify"]
#This is the main function which will be execute when the above command words are said
def execute(command):
# Connect to Spotify
scope = "streaming user-read-playback-state"
username = "[USERNAME]"
token = util.prompt_for_user_token(username, scope)
sp = spotipy.Spotify(auth=token)
pp = pprint.PrettyPrinter(indent=4)
# get the proper query
words = command.split(" ")
query = " ".join(words[2:])
prefix = ""
# see if the user is asking for album or artist
if words[1] == "album" or words[1] == "artist":
prefix = words[1]
else:
print ("Please say correct query")
return
# search and re-direct playback
results = sp.search(q=prefix + ':' + query, type=prefix)
items = results[prefix + "s"]['items']
if len(items) > 0:
play = items[0]
print (play['name'])
uri = play["uri"]
sp.start_playback(context_uri=uri)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment