From 81869c6b75fa0cd93a378082c065ef9d69f8d3fc Mon Sep 17 00:00:00 2001 From: GamePlayerCole Date: Sun, 22 Jul 2018 18:49:02 -0400 Subject: [PATCH] Created ver 1.00 of LastFM-NowPlaying. -Stable --- LastFM-NowPlaying.py | 69 ++++++++++++++++++++++++++++++ README.md | 6 ++- __pycache__/apiKey.cpython-36.pyc | Bin 0 -> 218 bytes 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 LastFM-NowPlaying.py create mode 100644 __pycache__/apiKey.cpython-36.pyc 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 0000000000000000000000000000000000000000..6aa1f7737a7ca001f831aee8259d6a7cb1011151 GIT binary patch literal 218 zcmXr!<>h*FBqUmofq~&M5W@i@kmUfx#T-B)g&~R|g)x{xlPOBU#LOT$CDGV4Db*+~ zDa|C!$RgDuHPJlL*uvDpz}(otPm}Q$TVg?`cWUKIh9V}QZZPpHMn5+-B{NY!Jux@6 zASbahwJ141Csp6SC_OPRvnsVnKe!~nC^0=%KcFZ-y(lp^H#09?KPa^zzc>>pk(pYo p?~_