Last.fm Now Playing

By tv3636 on Mar 05, 2009

I know there are tons of now playing scripts, but because last.fm now shows the currently playing song I figured I'd make this one for last.fm. Also I really wanted to make one for Pandora but couldn't figure out how to (if anyone can/does please let me know) and this works while I listen to Pandora because of a Firefox add-on I have.

Anyway, please give me suggestions/feedback :)

By the way: Last.fm will randomly not update the currently playing song every once in a while. If the script gets the song wrong it's because of last.fm, not the script. It grabs whatever is on your last.fm profile at the moment you use the /np alias.

To use:
-Set your username when you load the script or use the alias /lastfm nickname to set it
-Use the alias /np to describe your current song according to last.fm

;Last.fm Now Playing
;by tv3636
;Grabs the currently playing song from last.fm (if applicable) and describes it to the active window
;Takes a few seconds to grab the data

on *:LOAD:/lastfm $?="Enter your last.fm username."

alias np {
  if ($sock(lastfm)) .sockclose lastfm
  if (%last.fm.username) {
    sockopen lastfm www.last.fm 80
    set -u10 %last.fm.active $active
  }
  else echo $active 08,01Error, no username found.  To set your username use /lastfm <username>
}

alias lastfm { set %last.fm.username $1 }

on *:SOCKOPEN:lastfm: {
  sockwrite -nt $sockname GET /user/ $+ %last.fm.username HTTP/1.1
  sockwrite -nt $sockname Host: www.last.fm
  sockwrite -nt $sockname $crlf
}
on *:SOCKREAD:lastfm: {
  var %last.fm
  sockread %last.fm
  if (imageMedium isin %last.fm) {
    sockread %last.fm | sockread %last.fm | sockread %last.fm 
    describe %last.fm.active np: ♪ $regsubex(%last.fm,/(?:<(?:.*?)>)([^<]+)(?:<(?:.*?)>)(?:[^<]+)(?:<(?:.*?)>)([^<]+).+/,\2 by \1) ♪
    sockclose lastfm
  }
  elseif (</body> isin %last.fm) echo %last.fm.active 08,01Last.fm error, no currently playing song detected.
}

Comments

Sign in to comment.
AnaBotNowYourGone   -  Sep 11, 2011

everytime i try it says IS UNKNOWN COMMAND

any help? :/

 Respond  
Jordyk19   -  Jun 11, 2011

Its pretty awesome and useful.
And sine i use Last.FM daily i will +1 this smippet.

 Respond  
cooldude13233   -  Jun 10, 2011

the script is awesome but it sometimes spits out HTML code into the chat :/

 Respond  
KilllerX   -  Feb 04, 2011

I would need to read up on it. and I read the TCP socket indtroduction on the forums... and It explained a few things to me. I understood what they do. but it didn't give me the information to understand how to script it effectively. It gave examples on how to join a server. well I thought that is what the /server was for. Granted doing it as an alias you could do /name and have it automatically join that server. or you could do a menu to do that also. Personally I don't think it explained what is needed to know with sockets as effective as it could of.

 Respond  
Jethro   -  Feb 04, 2011

KillerX, why don't you set out to write one on your own? It's not hard to make a socket script using GET. Give it a try yourself and if you have an issue with it, you can post your work at the forum, I'm sure napa, dean or whoever and I would be glad to help you out.

tv3636, I'm not sure why you put this line of code:> sockread %last.fm | sockread %last.fm | sockread %last.fmwhen it's not necessary and can be omitted?

 Respond  
KilllerX   -  Feb 04, 2011

Well not saying you personally have to. I tried changing around some Alias. using a bnp. and using a write /buy /1 /2 Which I want it to write to the $scriptdirc a new folder /buy the file name would be the artist name (or /1) and then it would list the songs inside that file. But It didn't work. so I deleated it. even if this could be rewritten to do that.

 Respond  
tv3636   -  Feb 04, 2011

Well, regardless, I have a macbook at college with me and I actually don't even have my own PC at home anymore, so I probably won't be doing any mIRC scripting for a very long time...if ever. I would be open to doing bug fixes but I don't think I'll be adding that, sorry.

 Respond  
KilllerX   -  Feb 03, 2011

(personally has 157 pages) I doubt I want to go though those looking for how long ago I played them. And sometimes I am to lazy to write it out myself. So I miss a song then. Would be nice to be able to just /bfm and have it write down the song title and artist. So I can get it from Itunes or last.fm or where-ever I decide to grab it from.

 Respond  
tv3636   -  Feb 02, 2011

I could add that, but Last.fm already does that. If you're using this script, you already have that information. Go to your last.fm profile, click on the list of artists, and you can then click on an artist to see a chart of which songs you've listened to and how many times you've listened to each one.

 Respond  
KilllerX   -  Feb 02, 2011

*wonders how he would go about writing an addition to this so he could do a /npw (now playing write) So it writes to a file so I can get the song later on.

Personally I would use something like that. And if it would write it in a fashion as such

Artist - Song1, Song2, Song3 If I grab a song from the same artist.

 Respond  
Spanky   -  Aug 06, 2009

its ok im trying to fix it by im not having any luck. i will let you know if i fix it or something.

 Respond  
tv3636   -  Aug 06, 2009

Hmm..I have no idea why it would do that..if no currently playing song is detected it should give you an error message. I've honestly never seen that though, sorry.

 Respond  
Spanky   -  Aug 06, 2009

i know im late here But i have only just started using last.fm i noticed most of the times /np does [00:25] * ~Spencer np: ♪ ♪

instead of [00:23] * ~Spencer np: ♪ song by artist ♪

 Respond  
tv3636   -  May 29, 2009

Yeah, I noticed that too after seeing %last.fm in my variables all the time.
Code updated in a few ways, I shortened it a lot with regex, which I now know how to use. (kind of) If you have a better regex match for me let me know.
Also added an error message if no currently playing song is detected, meaning last.fm doesn't have one on the page.

 Respond  
^Neptune   -  May 25, 2009
var %t
  sockread %t

Actually this does very well serve a good purpose, as it declares a local variable for the sockread data to be put into, rather than having to unset it on a sockclose event.

 Respond  
DJTwittr   -  May 25, 2009

bummer... i was going to try something, but it doesnt work the same way.. I was going to try to change last.fm to blip.fm...

/me wonders if it is even possible

 Respond  
miniCruzer   -  May 23, 2009

Yay!

 Respond  
tv3636   -  May 22, 2009

add an on TEXT event somewhere in the script:

on *:TEXT:!np:#:{ np }

Might want to change # to #yourchannel and add an if statement if you don't want anyone else to be able to trigger it but yourself so the event would then be:

on *:TEXT:!np:#YOURCHANNEL:{ if ($nick == YOURNICK) np }
 Respond  
miniCruzer   -  May 22, 2009

Suggestion: I want to put this in my bot... So I would need a trigger such as !np or !lastfm - how can I do this???

 Respond  
Kirby   -  Mar 14, 2009

Very nice. :D

 Respond  
tv3636   -  Mar 13, 2009

minor update to deal with ampersands (&) ..I'm not sure what other weird characters would show up as code but if anyone has any instances (like & showed up as &) let me know and I can add a $replace to deal with them

 Respond  
Kirby   -  Mar 05, 2009

Cheiron, you don't need to set a variable for the sockread as you did here:

var %t
  sockread %t

; it can just be

sockread %t

.

tv3636: /tokenize is basically using bits of things (such as $sock($sockname).mark or variables), which you can turn into identifiers, such as $1, $2, $3, depending on the number of contents you have in a set. Like the name, you get the $1, $2, $3 through the token number.
So if I have

/sockmark obama $active $nick($chan,$r(1,$nick($chan,0)))

, since those two are separated by $chr(32), I can use /tokenize 32 $sock($sockname).mark to make $1 the $active, and $2 the $nick($chan,$r(1,$nick($chan,0))).

 Respond  
Cheiron   -  Mar 05, 2009

you can always try this ...

on *:TEXT:!tune:#radio:{ /songp }
alias songp {
  sockopen songp "url for the website showing what is playing"
  /set %kanaal4 #radio
}
on *:sockopen:songp: {
  sockwrite -n $sockname GET /7 HTTP/1.0
  sockwrite -n $sockname User-Agent: Mozilla
  sockwrite -n $sockname
}
on *:sockread:songp:{
  var %t
  sockread %t
  while ($sockbr) {
    sockread -f %t
    if ($regex(%t,/<body>(.*?)</body>/)) {
      tokenize 44 $regml(1)
      if (%last != $7-) {
      /msg %kanaal4 13,1 Current Song playing on "station" Radio: 15 $7-
      set %last $7-
      }
    }
  }
}
alias stime { .timercheck 0 10 songp }
 Respond  
tv3636   -  Mar 05, 2009

Oops, %sockreader was what it was originally called and then I switched it to %last.fm.

I guess I could switch it to sockmark..does it really increase efficiency or anything or just cuts down on variable usage?

And I don't really understand/want to get into tokens yet but thank you very much for the suggestions.

Edit: Also, if anybody has an idea for an automatic version of this please let me know. I don't have a use for it personally so I don't care that much, but I could try to add that in if possible. Because there isn't readily available song length data I don't think it would be very easy to do if it could be done at all, but if somebody wanted to find out I'd be interested to hear.

 Respond  
Kirby   -  Mar 05, 2009

Unfortunately I don't have a last.fm account, and I don't want to bother making one, so I can't use this script for my personal use. :<

But as for the script, you can certainly change some things (they're very minor).
Instead of

  set %last.fm.active $active

, you can use

  sockmark lastfm $active

, so

      describe %last.fm.active np: ( )( %last.fm.song by %last.fm.artist )( )

can be

      describe $sock(lastfm).mark np: ( )( %last.fm.song by %last.fm.artist )( )

What's very useful about sockmark is that you can limit your usage on variables. /tokenize works very well with it too, if you have multiple items you want to sockmark. With sockmarking, you can jam bits of information for useful purposes.
The

    halt

in the $sockerr is unecessary because you have an else { after it, and the

    var %sockreader

is unecessary as well because I do not see other place in which it is used.
Good to see some regex too.

Other than that, very nice work tv3636.

 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.