Last.fm Now Playing (Utilizing Last.fm's API)

By Jethro on Sep 15, 2011

This is initially a request by AnaBotNowYourGone who says the script made by tv3636 has ceased to work.

I figure I'd give it a rewrite using last.fm's API to fetch users' now playing song info.
The script will output the trigger as an action text, which is inspired by tv3636's.

I've decided to submit this as a public snippet, because I've found out that the API key is nothing private.
A former last.fm employee name jr has posted it everywhere at last.fm's method instructions.

The script supports both PM and Channel.> @lastfm jr

  • bot jr isn't playing music right now! .lastfm fadfjkhadksfj
  • Bot No user with that name was found! !lastfm fireninjaxxx
  • Bot Last.fm_NP : ♩♪♫ Leftöver Crack - Burn Them Prisons ♩♫♬You can use any of the !,@, and . trigger prefix to command the script.

It also acknowledges self-trigger using the command:

 /np <username>

will output as an echo.

on $*:text:/[!.@]lastfm( |$)/iS:*:{
  .ignore -cpu3 $nick 2 
  $np($strip($2))
}
alias -l lastfm { 
  return $replace($1,&gt;,>,&lt;,<,&amp;,&) 
}
on *:sockread:lastfm*:{ 
  tokenize 96 $sock($sockname).mark 
  var %lf = $sockname
  if ($sockerr) { 
    $2 $me is having difficulty reading the data from last.fm! 
    halt 
  } 
  sockread &lastfm 
  if ($regex([ [ $4 ] ],/<error code="6">(.*)</error></lfm>)) { 
    $2 $+($regml(1),!) 
    halt 
  }
  elseif (nowplaying !isin [ [ $4 ] ]) { 
    $2 $1 isn't playing music right now! 
    sockclose %lf 
  }
  else { 
    $2 Last.fm_NP : $+($chr(9833),$chr(9834),$chr(9835)) $+($chr(2),$&
      $lastfm($gettok($gettok([ [ $4 ] ],6,62),1,60)),$chr(2)) - $+($chr(2),$&
      $lastfm($gettok($gettok([ [ $4 ] ],8,62),1,60)),$chr(2)) $&
      $+($chr(9833),$chr(9835),$chr(9836)) 
    sockclose %lf 
  }
}
on *:sockopen:lastfm*:{ 
  tokenize 96 $sock($sockname).mark
  if ($sockerr) { 
    $2 $me is having difficulty connecting to lastfm's website! 
    halt 
  }
  $5 GET $+(/2.0/?method=user.getrecenttracks&user=,$1,&api_key=,$3) HTTP/1.1
  $5 Host: $sock($sockname).addr 
  $5 Connection: close 
  $5
}
alias np { 
  if (!$1) { 
    .notice $iif($isid,$nick,$me) Please enter a user name! e.g. /np <UserName> 
    halt 
  }
  var %lastfm $+(lastfm,$r(1,9999),$ticks,$network,$cid) 
  $+(sock,$iif($sock(%lastfm),close,open)) %lastfm ws.audioscrobbler.com 80
  sockmark %lastfm $+($1,`,$iif($isid,.describe $iif(#,#,$nick),echo -at *),`,b25b959$&
    $+ 554ed76058ac220b7b2e0a026,`,$!bvar(&lastfm,1-).text,`,sockwrite -nt %lastfm)
}

Comments

Sign in to comment.
Hobbyboy   -  Feb 15, 2016

Is there a version of this script that broadcasts the song playing in the current irc channel with /np, but doesn't have the public commands? (Like the irssi script here: https://github.com/irssi/scripts.irssi.org/blob/gh-pages/scripts/lastfm.pl)

 Respond  
DarkGamer120   -  Dec 24, 2012

I get this:
<~DG> !lastfm darkgamer120

  • &Apple darkgamer120 isn't playing music right now!
    Even though i am playing music and Scrobbling it..
illhawkthat  -  Feb 23, 2013

@DarkGamer120 and @Jethro
The API key (public) that was listed here has been deprecated.
You have to sign up for an API key from last.fm to get this script to work again. Just letting you know so you can edit the script a little and maybe put a note in the description.

HerbTarlek  -  Mar 06, 2013

I'd appreciate it if someone showed what needed to be changed on this to use the new API keys.

illhawkthat  -  Mar 07, 2013

@HerbTarlek
I made some small modifications to this script for someone as a request you can find it here http://pastebin.com/hmTu6eKs
For the script by hxck you can find it here http://pastebin.com/HC10yAay

Sign in to comment

ES   -  Nov 20, 2011

chachin, http://www.hawkee.com/snippet/9077/
I tried to base it around the perl version (Play count, loved track, ability to compare users.)

 Respond  
PyThOn   -  Oct 19, 2011

paca paca paca

 Respond  
Jethro   -  Oct 18, 2011

Thank you, chachin. But there are members at Hawkee who hate my guts. :p I'm glad you see the cool side in me. :) ^^

 Respond  
chachin   -  Oct 18, 2011

oh thanks a lot man. you always the coolest person on hawkee :D

 Respond  
Jethro   -  Oct 17, 2011

chachin, locate and change this part:

else { 
    $2 Last.fm_NP : $+($chr(9833),$chr(9834),$chr(9835)) $+($chr(2),$&
      $lastfm($gettok($gettok([ [ $4 ] ],6,62),1,60)),$chr(2)) - $+($chr(2),$&
      $lastfm($gettok($gettok([ [ $4 ] ],8,62),1,60)),$chr(2)) $&
      $+($chr(9833),$chr(9835),$chr(9836)) 
    sockclose %lf 
  }

to this:

else { 
    $2 Last.fm_NP : $+($chr(2),$&
      $lastfm($gettok($gettok([ [ $4 ] ],6,62),1,60)),$chr(2)) - $+($chr(2),$&
      $lastfm($gettok($gettok([ [ $4 ] ],8,62),1,60)),$chr(2))
    sockclose %lf 
  }
 Respond  
chachin   -  Oct 17, 2011

how can I get rid of the "music notes" people keep saying its spammy :s

 Respond  
Mtn64   -  Sep 21, 2011

Good script, Clean and nice, 10/10.

 Respond  
PyThOn   -  Sep 17, 2011

Jethro_ does it again.
Good script i like this. +Like ;D

 Respond  
chachin   -  Sep 17, 2011

guys why not make something like this:

.np [17|7:11:47am] 'FightingNavyman' is now playing: Anthrax - Spreading The Disease - Madhouse [playcount 6x] (thrash metal, metal, heavy metal, speed metal) [04:19] [17|7:11:58am] .compare winston_manswallow [17|7:11:59am] 'FightingNavyman' vs 'heroinhero115': 93.5% - Common artists include: Bassnectar, deadmau5, Foo Fighters, Skrillex, Daft Punk OH and this script works awsome 10/10
 Respond  
Jethro   -  Sep 15, 2011

Guys, I made a tiny oversight. I have edited the code. Please see to it if you get the chance.

 Respond  
AnaBotNowYourGone   -  Sep 15, 2011

Helped me out, sorry that I didn't thank you in PM, but I guess this is kind of a thanks

works well

 Respond  
Frenetic   -  Sep 15, 2011

As have I Jethro, I made a 500 line script into 300 lol.

 Respond  
Jethro   -  Sep 15, 2011

Frenetic, people with know-how of MSL could stack up those bits of code to make the overall script "look" shorter, just as you would play Tetris. :P Though I've begun to quit the habit of writing code in that fashion gradually.

Thanks for liking it, RaZ. I hope you enjoy it.

 Respond  
RaZ   -  Sep 15, 2011

i like it :D

 Respond  
Frenetic   -  Sep 15, 2011

Yeah, I got told the same thing but not by someone on this site; at least I don't think they're on here. Lol. Some people might want the short version of it and not the long version.

 Respond  
Jethro   -  Sep 15, 2011

I recall once upon a time a wise man named jaytea or fordlawnmower, and I'm not certain which one it was, told me it's very ugly to "compact" a code closely to one bit after another. Thank of it when you're in a traffic jam. It's both a mess and bummer or pure annoyance. :p

 Respond  
Frenetic   -  Sep 15, 2011

That's true, but none-the-less why not post both? "Clean & Not so clean" lol

 Respond  
Jethro   -  Sep 15, 2011

Frenetic, trust me. The way I have it now is easier to debug. :P

 Respond  
Frenetic   -  Sep 15, 2011

Aye, it looks much cleaner now. But, the way it was before; was-just as easy to read.

 Respond  
Jethro   -  Sep 15, 2011

I reorganized the script to make it a lot cleaner than it was. Thank you for the rating. Much obliged!

 Respond  
Jordyk19   -  Sep 15, 2011

Very nice.
I really like Last.FM.
Gets a 10/10 from me.

 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.