Skip to content

Instantly share code, notes, and snippets.

@jonas747
Last active December 26, 2015 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonas747/7076477 to your computer and use it in GitHub Desktop.
Save jonas747/7076477 to your computer and use it in GitHub Desktop.
Last.fm srobbler in e2
@name Last.fm Nowplaying
@inputs Screen:wirelink
@outputs Song:string Artist:string
@persist Uri:string User:string
@trigger
if(first() || duped() || inputClk()){
#Your api key from last.fm
API_KEY = ""
#your last.fm username
User = "jonasr747"
#Poor but quick implementation of xml, has many large issues but works for this use
function string xmlGetNode(Str:string, Start:string, End:string){
Index = Str:find(Start)
if(Index){
New = Str:sub(Index+Start:length())
EndIndex = New:find(End)
if(EndIndex){
FinalSub = New:sub(1, EndIndex-1)
return FinalSub
}
}
return ""
}
#gets a node and ignores attributes
function string xmlGetNodeIgnoreAttributes(Str:string, Start:string, End:string){
Node = xmlGetNode(Str, Start, End)
Index = Node:find(">")
if(Index){
return Node:sub(Index+1)
}
return ""
}
Screen:egpClear()
Screen:egpRoundedBox(1, vec2(256, 256), vec2(310, 210))
Screen:egpColor(1, 0, 0, 0, 200)
Screen:egpRoundedBox(2, vec2(256, 256), vec2(300, 200))
Screen:egpColor(2, 50, 50, 200, 200)
Screen:egpText(3, "Now Playing - "+User, vec2(256, 160))
Screen:egpAlign(3, 1)
Screen:egpSize(3, 25)
Screen:egpText(4, "", vec2(256, 220))
Screen:egpText(5, "", vec2(256, 280))
Screen:egpAlign(4, 1)
Screen:egpAlign(5, 1)
Screen:egpSize(4, 25)
Screen:egpSize(5, 25)
Uri = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user="+User+"&api_key="+API_KEY
runOnHTTP(1)
timer("check", 100)
}elseif(clk("check")){
timer("check", 5000)
if(httpCanRequest()){
httpRequest(Uri)
}else{
print("Can't make http request")
}
}elseif(httpClk()){
Data = httpData()
Node = xmlGetNodeIgnoreAttributes(Data, "<track", "</track>")
if(Node != ""){
Song = xmlGetNode(Node, "<name>", "</name")
Artist = xmlGetNodeIgnoreAttributes(Node, "<artist ", "</artist>")
Screen:egpSetText(5, "By: "+Artist)
Screen:egpSetText(4, Song)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment