Code: Select all
# Lastfm Artist Lookup Script by Rosc2112 at yahoo com http://members.dandy.net/~fbn/lastfm-artist.tcl.txt
# Copyright C.Leonhardt March 12, 2008
set lalver "0.1a"
#
# Retrieves artist info from last.fm, top album and tracks (last week & 6 months)
# http://www.last.fm/music/
#
# Usage: .lfm <artist> or /msg botnick lfm <artist>
#
###############################################################################################################
# Configs:
##########
# channels we allow public use in:
set lalchans "#mychan #chan2 #etc"
# out of the above channels, these will only respond by PRIVMSG:
set lalquiet "#chan2 #etc"
# timeout for geturl (1/1000ths sec.)
set laltout "1200000"
###############################################################################################################
# Code Below
############
package require http
bind pub - .lfm lalproc
bind msg - .lfm lalmsg
bind msg - lfm lalmsg
proc lalmsg {nick uhost hand text} {
if {![onchan $nick]} {return}
lalproc $nick $uhost $hand privmsg $text
return
}
proc lalproc {nick uhost hand chan text} {
if {([lsearch -exact $::lalchans $chan] == -1) && ($chan != "privmsg")} {return}
if {([lsearch -exact $::lalquiet $chan] != -1) || ($chan == "privmsg")} {set chan $nick}
set text [split [string trim $text]]
if {[llength $text] < 1} {
puthelp "PRIVMSG $nick :Lastfm Artist Lookup Script: Artist/Band Name must be at least one word."
return
}
set originaltext $text
# Need to convert space chars to html code. We'll assume the rest of the input is somewhat rational.
regsub -all { } $text {%20} text
regsub -all {\+} $text {%2B} text
set lalurl "http://www.last.fm/music/"
putquick "PRIVMSG $chan :Last.fm: looking up artist, holon.."
catch {set page [::http::geturl $lalurl$text -timeout $::laltout]} error
if {[string match -nocase "*couldn't open socket*" $error]} {
puthelp "PRIVMSG $nick :Error: couldn't connect to last.fm..Try again later"
return
}
if { [::http::status $page] == "timeout" } {
puthelp "PRIVMSG $nick :Error: Connection to last.fm timed out."
return
}
set html [::http::data $page]
::http::cleanup $page
if {[regexp {<title>(.*?) –.*?</title>} $html match laltitle]} {
set laltitle [split [string trim $laltitle]]
if {([regexp -nocase {<title>Last.fm – The Social Music Revolution</title>} $html])\
||([regexp -nocase {>We don?t know much about .*? yet.<} $html])\
|| ([regexp -nocase {<span>We don.*? have a description for this artist yet, <} $html])\
|| (![regexp -nocase $originaltext $laltitle])} {
puthelp "PRIVMSG $chan :Last.fm: no artist info available for [join $originaltext]"
return
} elseif {[regexp -nocase $originaltext $laltitle]} {
set lalbum "";set laltoplw "";set laltop6m ""
regexp {>Top Albums <span>.*?<strong class="title"><a href="/music/.*?/.*?" title=".*?">(.*?)</a></strong>} $html match lalbum
regexp {<h3>Top Tracks, Last Week</h3>.*?<a href="/music/.*?/_/.*?">(.*?)</a>} $html match laltoplw
regexp {<h3>Top Tracks, Last Six Months</h3>.*?<a href="/music/.*?/_/.*?">(.*?)</a>} $html match laltop6m
puthelp "PRIVMSG $chan :Last.fm \002Artist\002: $laltitle \002Top Album\002: $lalbum \002Top Track (Last week)\002: $laltoplw \002Top Track (6 months)\002: $laltop6m"
return
} else {
puthelp "PRIVMSG $chan :Last.fm: no artist info available for [join $originaltext] "
}
} else {
puthelp "PRIVMSG $chan :Sorry, artist \002[join $originaltext]\002 was not found."
}
}
putlog "Last.fm Artist Lookup Script $lalver by Rosc loaded."