1

I'm just trying to figure out how to keep checking to see if there is a song playing on Spotify using Spotipy then print it, and when the song changes it is supposed to also print out what it changed to. This should keep going on until the program is closed. This is what I have so far.

import spotipy
import spotipy.util as util

class SongAsAppName:
    def __init__(self):
        self.new_song = ''
        scope = 'user-read-currently-playing'

        self.token = util.prompt_for_user_token(username, scope,
                                           CLIENT_ID, CLIENT_SECRET,
                                           redirect_uri='http://localhost:8888/callback')
        self.spotify = spotipy.Spotify(auth=self.token)
        self.current_track = self.spotify.current_user_playing_track()


    def set_song_info(self, new_song):
        if self.song != new_song:
            self.get_song_info()
            self.current_track = new_song
            self.print_song_info()


    def get_song_info(self):
        song_title = self.current_track['item']['name']
        artist_name = self.current_track['item']['artists']
        for entry in artist_name:
            artist_name = entry['name']
        full_title = (song_title + ' - ' + artist_name)
        return full_title


    def print_song_info(self):
        self.song = self.get_song_info()
        while True:
            if self.new_song != self.song:
                print('Playing song: ' + self.song)
                self.new_song = self.song

test = SongAsAppName()
test.print_song_info()

I figure it has something to do with overwriting new_song with song and it getting stuck there. For example it prints out:

Playing song: Natural Disasters - Enon

And when the next song plays, doesn't print out that song's name. Brain is just fried after working all day and doing this lil side project so any help is appreciated!

--------------------------------------------------------------------------

Trying something different here, but same general idea. Song doesn't seem to get updated when the next song plays.

import spotipy
import spotipy.util as util
import sched
import time

new_song = ''
s = sched.scheduler(time.time, time.sleep)
scope = 'user-read-currently-playing'

token = util.prompt_for_user_token(username, scope,
                                   CLIENT_ID, CLIENT_SECRET,
                                   redirect_uri='http://localhost:8888/callback')
spotify = spotipy.Spotify(auth=token)
current_track = spotify.current_user_playing_track()


def get_song_info():
    while True:
        song_title = current_track['item']['name']
        artist_name = current_track['item']['artists']
        for entry in artist_name:
            artist_name = entry['name']
        full_title = (song_title + ' - ' + artist_name)
        s.enter(10, 1, get_song_info)
        return full_title


def print_song_info(new_song):
    while True:
        new_song = new_song
        song = get_song_info()
        if new_song != song:
            print('Playing song: ' + song)
            print(new_song)
            new_song = song
            print(new_song)
    SongAsAppName.s.enter(10, 1, print_song_info, (new_song,))


s.enter(10, 1, print_song_info(new_song))
print_song_info()
  • How is self.new_song intended to change? I don't see that being set anywhere except in __init__ – possum Jun 15 '20 at 23:45
  • supposed to become the new song title when i try to pull from the api again to see if its the same as the song that was previously playing. – Devon Casey Jun 16 '20 at 01:18

1 Answers1

2

The code you posted with the edit has some problems. I think you're trying to use the new_song in the global scope in print_song_info which isn't how those work. You've redefined print_song_info to take an argument so your last line can't run. I'm not sure why you have while True in get_song_info because it always returns after the first iteration. The last line in print_song_info is never reached.

I stripped down the code using sched as an example. This uses time() to produce a string that changes only once per second, but I think this behaves similar to as you intended without using threads or asyncio.

import sched
import time

s = sched.scheduler(time.time, time.sleep)

current_song = ''


def get_song_info():
    global current_song
    seconds = int(time.time())
    current_song = "This string changes once per second " + str(seconds)
    s.enter(0.5, 1, get_song_info)


def print_song_info(song):
    if current_song != song:
        print(current_song)
    s.enter(0.1, 1, print_song_info, (current_song,))


get_song_info()
print_song_info('')
s.run()
possum
  • 1,837
  • 3
  • 9
  • 18