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.
}
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.
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?
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.
(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.
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.
*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.
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.
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))).
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 }
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.
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.