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()