xkcd eggdrop script

Небольшой скриптик для eggdrop’а, который выдаёт на канал случайный комикс с xkcd.

Пример работы:
<z0rc> !xkcd
<Kuro-sama> XKCD №64: Solar Plexus
<Kuro-sama> http://imgs.xkcd.com/comics/solar_plexus.jpg
<Kuro-sama> It hurts to be hit there, you know
<z0rc> !xkcd 448
<Kuro-sama> XKCD №448: Good Morning
<Kuro-sama> http://imgs.xkcd.com/comics/good_morning.png
<Kuro-sama> As my standard, I use going to sleep at midnight and waking up at 8 AM.
<Legio> !xkcd 404
<Kuro-sama> Такого выпуска нет.
<iSage> !xkcd latest
<MahouShoujoAi> XKCD №488: Steal This Comic
<MahouShoujoAi> http://imgs.xkcd.com/comics/steal_this_comic.png
<MahouShoujoAi> I spent more time trying to get an audible.com audio book playing than it took to listen to the book. I have lost every other piece of DRM-locked music I have paid for.

Changelog:
v1.1 Добавлена возможность показа выпуска по его номеру.
v1.2 Добавлена возможность показа последнего вышедшого выпуска.

Прошу:

CODE: tcl
foreach p [array names xkcd *] { catch {unset xkcd($p) } }
namespace eval xkcd {}

set xkcd(ver) 1.2
set xkcd(authors) &quot;z0rc &amp; iSage&quot;
set xkcd(timeout) 20
set xkcd(time) 210

bind pub - !xkcd ::xkcd::pub_xkcd

proc ::xkcd::pub_xkcd {nick uhost hand chan text} {
	global xkcd

	set xkcdline [utimers]
	foreach line $xkcdline {
		if {&quot;xkcd:reset $uhost&quot; == [lindex $line 1]} { set xkcdtime [lindex $line 0] }
	}
	if { [info exists xkcd(host,$uhost)] } { 
		set temp [duration $xkcdtime] 
		regsub -all -- {hours} $temp {час(ов)} temp 
		regsub -all -- {hour} $temp {час} temp 
		regsub -all -- {minutes} $temp {минут(ы)} temp
		regsub -all -- {minute} $temp {минуту} temp
		regsub -all -- {seconds} $temp {секунд(ы)} temp
		regsub -all -- {second} $temp {секунду} temp
		if {$xkcd(time) &gt; 0} { putserv &quot;NOTICE $nick :Вы сможете использовать команду \002!xkcd\002 через \002$temp\002&quot; }
		return
	}

	set xkcd(host,$uhost) 1
	set xkcd(timer,$uhost) [utimer $xkcd(time) [list xkcd:reset $uhost ] ]

  	putlog &quot;\[xkcd\] Downloading...&quot;
	
	
	if { $text == &quot;latest&quot; } {
	    set query &quot;http://xkcd.com/&quot;
	} elseif { $text==&quot;&quot; } {
	
	    set query &quot;http://dynamic.xkcd.com/comic/random/&quot;
    	    set ua &quot;Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.7e&quot;
	    set http [::http::config -useragent $ua -urlencoding &quot;utf-8&quot;]
	    set http [::http::geturl &quot;$query&quot; -query &quot;&quot; -timeout [expr 1000 * 10]]
	
	    upvar #0 $http state
	    foreach {name value} $state(meta) {
		if { $name eq &quot;Location&quot; } {
			set query $value
		}
	    }
	} elseif {[string match {[0-9]*} $text]} {
	    set query &quot;http://xkcd.com/$text/&quot;
	}
	
	set http [::http::geturl &quot;$query&quot; -query &quot;&quot; -timeout [expr 1000 * 10]]
	set html [::http::data $http]
	::http::cleanup $http
	
	putlog &quot;\[xkcd\] Downloaded...&quot;
		
	regexp -all {http://xkcd.com/(.*?)/} $html -&gt; num
	regexp -all {&lt;/div&gt;\n&lt;br/&gt;\n&lt;br/&gt;\n&lt;img src=\&quot;(.*?)\&quot;} $html -&gt; link
	regexp -all {&lt;h1&gt;(.*?)&lt;/h1&gt;&lt;br/&gt;} $html -&gt; title
	regexp -all {title=\&quot;(.*?)\&quot;} $html -&gt; fin

	putlog &quot;\[xkcd\] Parsed...&quot;
	
	putserv &quot;PRIVMSG $chan :XKCD №$num: \002$title\002&quot;
	putserv &quot;PRIVMSG $chan :$link&quot;
	putserv &quot;PRIVMSG $chan :$fin&quot;
	
	putlog &quot;\[xkcd\] Done...&quot;
	
	return
}

proc xkcd:reset { uhost } {
	global xkcd
	
	catch {killutimer $xkcd(timer,$uhost)}
	catch {unset xkcd(timer,$uhost)}
	catch {unset xkcd(host,$uhost)}
}

putlog &quot;xkcd.tcl v$xkcd(ver) by $xkcd(authors) loaded&quot;

или вот.

Спасибо iSage за помощь в доводке скрипта.

Комментировать

Страницы