Last.FM User Info (+ Now Playing)

By dsabecky on 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

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 }
}

Comments

Sign in to comment.
Abcdefmonkey   -  Dec 20, 2011

I tried that one just now and it keeps saying I'm not listening to anything... Confused. Lol. It should work..

 Respond  
Jethro   -  Dec 20, 2011

I made a snippet using Last.fm's API here at: http://www.hawkee.com/snippet/8984/

 Respond  
Abcdefmonkey   -  Dec 20, 2011

I like the script, even though it doesn't update the information like it should. I ran it through a test and this is the result:

(Tue/01:59:44 PM) <%umadbro> [Last.FM] Chris (BluntKthx) has scrobbled 522 tracks and is currently listening to "Equinox" by Sonny.
(Tue/01:59:56 PM) » (%umadbro) Stupid thing..
(Tue/02:00:00 PM) » (%umadbro) I'm not listening to that..
(Tue/02:00:05 PM) <%umadbro> [Last.FM] Chris (BluntKthx) has scrobbled 523 tracks and is currently listening to "Empire" by Roksonix.
(Tue/02:01:53 PM) <%umadbro> [Last.FM] Chris (BluntKthx) has scrobbled 522 tracks and is currently listening to "Equinox" by Sonny.
(Tue/02:04:40 PM) » (%umadbro) .lastfm
(Tue/02:04:41 PM) <%umadbro> [Last.FM] Chris (BluntKthx) has scrobbled 524 tracks and is currently listening to "No Introduction" by Ransom.
(Tue/02:04:45 PM) » (%umadbro) .lastfm
(Tue/02:04:45 PM) <%umadbro> [Last.FM] BluntKthx has scrobbled 522 tracks and is currently listening to "No Introduction" by Ransom.

As you can see, it kept changing the number of songs I've scrobbled from 522 to 523 then 524 then back to 522. It also kept losing my "real name" as seen in the last line. Also, it wasn't changing the song name like it should. >.<

Overall, good work. :)

 Respond  
carmellia   -  Dec 19, 2011

Thank you for all your help Sunny. I like that I can come here and ask for help when I need it. I have recommended the site to others and hope that if they need help, they will come and ask.

carm

 Respond  
Sorasyn   -  Dec 19, 2011

Go ahead and amend that to your post on the forums I think they will find it vastly more helpful.. :) You may be able to accomplish your script through $com() but seeing as I have never used $com() before I wouldn't know where to start.

 Respond  
carmellia   -  Dec 19, 2011

It is a program on the computer. I was just informed tonight that one of our dj's and a friend of his built the site, so asked him to imbed the webplayer code into the website. I did post what I found on the site that I am using to run auto dj and am hoping that someone can help me with it so that I can plug it into my bumIRC bot and it will then show dj$nick Artist-Song.

Sorry to be so vague Sunny and hope what little bit I explained helps.

carm

 Respond  
Sorasyn   -  Dec 18, 2011

That doesn't help me at all... What is a SAM Broadcaster? A program running on your computer? Is it a website of some sort? You can't get something for nothing after all.

 Respond  
carmellia   -  Dec 18, 2011

I am trying to get my bot on mIRC show the name of the song and the artist currently playing in the rooms it is in. We use SAM Broadcaster t dj and so far I've not luch with it whatseover.

 Respond  
Sorasyn   -  Dec 15, 2011

Yeah I've read your thread. It seems like you're providing a vague idea for them to go off of. At least I can't exactly catch your drift. Is it a website that you're wanting this information off of, a program on your computer? The more details the better it makes our job that much easier.

 Respond  
carmellia   -  Dec 15, 2011

Thank you, I have and so far no help but I'm hoping that someone with more experience will poke their head in and give some input.

 Respond  
Sorasyn   -  Dec 13, 2011

@Carmellia, Post your request in the mIRC snippet request forums. It will attract more attention and will likely render a response faster than notifying just Jethro_ and myself.

Found here: http://www.hawkee.com/phpBB2/viewforum.php?f=9

 Respond  
carmellia   -  Dec 13, 2011

I am looking for something similar to this but am using SAM Spacial Broadcasting to dj and am looking for a script that will show what each dj is playing and update as each new song comes on the station.

Any help would be grateful.

Thank you

carm

 Respond  
Jethro   -  Sep 21, 2011

If you use a hash table, that sort of defeats the purpose of sockmark's availability. The only good thing about hash table is that you can sockread it into a binary variable and refer to the info with the -b switch. It's useful when you have a massive amount of data to be fetched.

P.S. jaytea may have a say on this if he figures what I figure is incorrect.

 Respond  
Sorasyn   -  Sep 21, 2011

Yeah any method of data storage that isn't global variables is a step up. :) Personally I like using hash since it's pretty easy to organize and call back information.

 Respond  
Jethro   -  Sep 21, 2011

Nice job, but you should learn to make friends with the sockmark and $sock($sockname).mark so you don't have to set global variables all over the place and then have them unset at the end.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.