<reponame>matthewdargan/Spotify-Siri-Integration<gh_stars>0
import configparser
import os
import pprint

from spotipy import spotipy
from spotipy.spotipy import util as util

# name of the module
moduleName = "spotify"

# words that must be said for this module to be executed
commandWords = ["spotify"]


def execute(command):
    """
    Executes the proper voice command when the above command words are said.

    :param command: voice command
    """
    # connect to Spotify
    scope = "user-top-read"
    username = os.environ['SPOTIFY_USERNAME']

    # use config token settings to setup connection to the Spotify API
    config = configparser.ConfigParser()
    config.read('config.cfg')
    client_id = config.get('SPOTIFY', 'CLIENT_ID')
    client_secret = config.get('SPOTIFY', 'CLIENT_SECRET')
    redirect_uri = config.get('SPOTIFY', 'REDIRECT_URI')

    token = util.prompt_for_user_token(username, scope, client_id, client_secret, redirect_uri)
    sp = spotipy.Spotify(auth=token)

    # get the proper query
    words = command.split(" ")
    query = " ".join(words[2:])
    prefix = ""

    # add a new track to the user's library
    if 'add' in words:
        # name of the track comes after the add keyword
        track = words[words.index('add') + 1]

        results = sp.current_user_saved_tracks_add(tracks=track)
        pprint.pprint(results)
        return

    # print the user's top artists
    elif 'top' and 'artists' in words:
        terms = ['short_term', 'medium_term', 'long_term']

        for term in terms:
            print("range: ", term)
            results = sp.current_user_top_artists(time_range=term, limit=50)

            for i, item in enumerate(results['items']):
                pprint.pprint(i, item['name'])

        return

    # create a new playlist
    elif 'create' and 'playlist' in words:
        playlist_name = words[words.index('playlist') + 1]
        playlists = sp.user_playlist_create(username, playlist_name)
        pprint.pprint(playlists)
        return

    # see if the user is asking for album or artist
    elif 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 there are any words in items, playback the corresponding track
    if len(items) > 0 and 'play' in items:
        play = items[0]
        pprint.pprint(play['name'])
        uri = play["uri"]
        sp.start_playback(context_uri=uri)
    else:
        print('Invalid voice command')

import configparser
import os
import pprint

from spotipy import spotipy
from spotipy.spotipy import util as util

# name of the module
moduleName = "spotify"

# words that must be said for this module to be executed
commandWords = ["spotify"]


def execute(command):
    """
    Executes the proper voice command when the above command words are said.

    :param command: voice command
    """
    # connect to Spotify
    scope = "user-top-read"
    username = os.environ['SPOTIFY_USERNAME']

    # use config token settings to setup connection to the Spotify API
    config = configparser.ConfigParser()
    config.read('config.cfg')
    client_id = config.get('SPOTIFY', 'CLIENT_ID')
    client_secret = config.get('SPOTIFY', 'CLIENT_SECRET')
    redirect_uri = config.get('SPOTIFY', 'REDIRECT_URI')

    token = util.prompt_for_user_token(username, scope, client_id, client_secret, redirect_uri)
    sp = spotipy.Spotify(auth=token)

    # get the proper query
    words = command.split(" ")
    query = " ".join(words[2:])
    prefix = ""

    # add a new track to the user's library
    if 'add' in words:
        # name of the track comes after the add keyword
        track = words[words.index('add') + 1]

        results = sp.current_user_saved_tracks_add(tracks=track)
        pprint.pprint(results)
        return

    # print the user's top artists
    elif 'top' and 'artists' in words:
        terms = ['short_term', 'medium_term', 'long_term']

        for term in terms:
            print("range: ", term)
            results = sp.current_user_top_artists(time_range=term, limit=50)

            for i, item in enumerate(results['items']):
                pprint.pprint(i, item['name'])

        return

    # create a new playlist
    elif 'create' and 'playlist' in words:
        playlist_name = words[words.index('playlist') + 1]
        playlists = sp.user_playlist_create(username, playlist_name)
        pprint.pprint(playlists)
        return

    # see if the user is asking for album or artist
    elif 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 there are any words in items, playback the corresponding track
    if len(items) > 0 and 'play' in items:
        play = items[0]
        pprint.pprint(play['name'])
        uri = play["uri"]
        sp.start_playback(context_uri=uri)
    else:
        print('Invalid voice command')
# name of the module # words that must be said for this module to be executed Executes the proper voice command when the above command words are said.      :param command: voice command # connect to Spotify # use config token settings to setup connection to the Spotify API # get the proper query # add a new track to the user's library # name of the track comes after the add keyword # print the user's top artists # create a new playlist # see if the user is asking for album or artist # search and re-direct playback # if there are any words in items, playback the corresponding track