Last.FM User Info (+ Now Playing)
Platform: mIRC
Published Sep 21, 2011
Updated Sep 21, 2011
A last.fm profile poller I wrote after seeing "Last.fm Now Playing (Utilizing Last.fm's API)" by none other than Jethro (thanks for the inspiration, dude!).
The bot displays basic user info and if their currently playing anything.
Configuration: None.
Syntax: !lastfm <username>
Example result: Dustin Sabecky (dsabecky) has scrobbled 11033 tracks and is currently listening to "Turbulence" by Catherine.
; !lastfm trigger
on *:text:!lastfm*:#: {
if (!$2) { msg $nick Syntax: !lastfm <username> }
else { set %fm.user $2 | set %fm.chan $chan | fm_con }
}
; user info connection
alias fm_con { set %fm.count | sockopen lfm ws.audioscrobbler.com 80 | sockopen lfm_np ws.audioscrobbler.com 80 }
; regex XML code
alias fm_s { var %q,%w $regsub($1-,/(<[^>]+>)/g,$null,%q) | return %q }
; announce results
alias fm_ann {
if (%fm.rname) { msg %fm.chan %fm.rname ( $+ %fm.name $+ ) has scrobbled %fm.pc tracks $iif(%fm.np == 1, and is currently listening to $qt(%fm.track) by %fm.art, and is not currently listening to anything) $+ . }
else { msg %fm.chan %fm.name has scrobbled %fm.pc tracks $iif(%fm.np == 1, and is currently listening to $qt(%fm.track) by %fm.art, and is not currently listening to anything) $+ . }
unset %fm.*
}
; sending API requests
on *:sockopen:lfm: {
sockwrite -nt lfm GET /2.0/?method=user.getinfo&user= $+ %fm.user $+ &api_key=b25b959554ed76058ac220b7b2e0a026 HTTP/1.1
sockwrite -nt lfm User-Agent: Mozilla/5.0
sockwrite -nt lfm Host: ws.audioscrobbler.com
sockwrite -nt lfm Accept: text/xml,application/xml,application/xhtml+xml,text/html
sockwrite -nt lfm $crlf
}
on *:sockopen:lfm_np: {
sockwrite -nt lfm_np GET /2.0/?method=user.getrecenttracks&user= $+ %fm.user $+ &api_key=b25b959554ed76058ac220b7b2e0a026 HTTP/1.1
sockwrite -nt lfm_np User-Agent: Mozilla/5.0
sockwrite -nt lfm_np Host: ws.audioscrobbler.com
sockwrite -nt lfm_np Accept: text/xml,application/xml,application/xhtml+xml,text/html
sockwrite -nt lfm_np $crlf
}
; read API results
on *:sockread:lfm: {
if ($sockerr) { msg %fm.chan Sorry, I encountered an error. | unset %fm.* | sockclose lfm }
sockread %fmxml
if (<name> isin %fmxml) { set %fm.name $fm_s(%fmxml) }
if (<realname> isin %fmxml) { set %fm.rname $fm_s(%fmxml) }
if (<playcount> isin %fmxml) { set %fm.pc $fm_s(%fmxml) }
if (<error isin %fmxml) { msg %fm.chan I can't find that username. | unset %fmxml | unset %fm.* | sockclose lfm }
}
on *:sockread:lfm_np: {
if ($sockerr) { msg %fm.chan Sorry, I encountered an error. | unset %fm.* | sockclose lfm_np }
sockread %npxml
if (<track now isin %npxml) { set %fm.np 1 }
if (<artist isin %npxml) && (%fm.artl != 1) { set %fm.artl 1 | set %fm.art $fm_s(%npxml) }
if (<name> isin %npxml) && (%fm.trackl != 1) { set %fm.trackl 1 | set %fm.track $fm_s(%npxml) }
}
; when socket gets closed, if both are closed, read output
on *:sockclose:lfm: {
inc %fm.count
unset %fmxml
if (%fm.count == 2) { fm_ann }
}
on *:sockclose:lfm_np: {
inc %fm.count
unset %npxml
if (%fm.count == 2) { fm_ann }
}