10/04/2013, 10:25
On m'avait demandé il y a quelques temps, sur IRC, un code pour annoncer sur un canal le titre d'une page web citée, pour faire comme l'excellent robot de MenzAgitat.
J'avais pondu un petit code qui malheureusement ne suivait pas les redirections de pages, donc lorsqu'on mettait une URL raccourcie, on ne voyait que "Moved permanently".
Voici mon petit code, modifié:
J'avais pondu un petit code qui malheureusement ne suivait pas les redirections de pages, donc lorsqu'on mettait une URL raccourcie, on ne voyait que "Moved permanently".
Voici mon petit code, modifié:
tcl
package require http
package require uri
bind pubm - "* *http*" check_url
proc check_url {nick uhost handle chan text} {
if {[regexp -- {(https?://[a-z0-9\-]+\.[a-z0-9\-\.]+(?:/|(?:/[a-zA-Z0-9!#\$%&'\*\+,\-\.:;=\?@\[\]_~]+)*))} $text match url]} {
set url [string map -nocase { "&" "&" } $url]
set token [geturl $url]
set html_data [::http::data $token]
::http::cleanup $token
regexp -all -nocase -- "<title>(.*)</title>" $html_data match title
putserv "PRIVMSG $chan :Titre de l'url de $nick : $title"
}
return 0
}
proc geturl {url args} {
array set URI [::uri::split $url];
while {1} {
set token [eval [list http::geturl $url] $args]
if {![string match {30[1237]} [::http::ncode $token]]} {return $token}
array set meta [set ${token}(meta)]
if {![info exist meta(Location)]} {
return $token
}
array set uri [::uri::split $meta(Location)]
unset meta
if {$uri(host) == ""} { set uri(host) $URI(host) }
set url [eval ::uri::join [array get uri]]
}
}