diff --git a/LastFM-NowPlaying.py b/LastFM-NowPlaying.py new file mode 100644 index 0000000..3d4d2ff --- /dev/null +++ b/LastFM-NowPlaying.py @@ -0,0 +1,69 @@ +from xml.dom import minidom +from urllib.request import urlopen +import sys +import time +import threading + +userName = input("Enter your LastFM username: ") + +#Public API Key for Accessing Last FM +apiKey = ("460cda35be2fbf4f28e8ea7a38580730") + +#Global Variables +currentTrackURL = ('http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&nowplaying="true"&user=' + str(userName) + '&api_key=' + str(apiKey)) +runCheck = True +waitTime = 0 +noSongPlaying = ("Nothing Currently Playing") + +#Defines the funcions for use in different threads. +def checkForNewSong(): + global runCheck + global waitTime + while runCheck == True: + #Loads Current Song info from Last FM + currentTrackXML = urlopen(currentTrackURL).read() + currentTrack = minidom.parseString(currentTrackXML) + songName = currentTrack.getElementsByTagName('name') + songArtist = currentTrack.getElementsByTagName('artist') + songInfo = songName[0].firstChild.nodeValue + " by " + songArtist[0].firstChild.nodeValue + " " + + currentSongFile = open("currentSong.txt", "r") + #Checks if currentSong is already in currentSong.txt If it isn't, clears the file. + if currentSongFile.readline() != songInfo: + currentSongFile.close() + #Runs twice; Once for clearing and once for adding the curren song. + currentSongFile = open("currentSong.txt", "w") + currentSongFile.write(songInfo) + currentSongFile.close() + + print(songInfo) + #Adds wait before next check. + time.sleep(waitTime) + #End of While Loop + + #Sets the current song to "Nothing Currently Playing" + currentSongFile = open("currentSong.txt", "w") + currentSongFile.write(noSongPlaying) + currentSongFile.close() + #Closes the Application + sys.exit() + +def exitCheck(): + global runCheck + while runCheck == True: + exitInput = input("Type Exit to close: \n") + if exitInput == "exit" or exitInput == "Exit" or exitInput == "EXIT": + print("Closing Application...") + runCheck = False + + +#Checks for currentSong.txt. If doesn't exist, creates file. +currentSongFile = open("currentSong.txt", "w") +#Used to keep currentSong.txt on "Nothing Currently Playing" until first song is pulled from LastFM +currentSongFile.write(noSongPlaying) +currentSongFile.close() +#Creates Threads for the two functions +newSongThread = threading.Thread(target=checkForNewSong) +exitThread = threading.Thread(target=exitCheck) +newSongThread.start() +exitThread.start() diff --git a/README.md b/README.md index 54f9ef6..ec158cf 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # LastFM-NowPlaying -A Python Script that pull's a user's current song from Last FM then writes it to a text file for use with OBS or Xsplit. +A Python Script that pull's a user's current song from LastFM then writes it to a text file for use with an external program. + +### Use +Input your LastFM username. Then let it run. The songs will automatically write to currentSong.txt +To close, type in "exit" then press Enter. diff --git a/__pycache__/apiKey.cpython-36.pyc b/__pycache__/apiKey.cpython-36.pyc new file mode 100644 index 0000000..6aa1f77 Binary files /dev/null and b/__pycache__/apiKey.cpython-36.pyc differ