Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###
- #
- # Название: newtime.tcl
- # Версия: 1.3
- # Автор: tvrsh
- # Оффсайт: http://egghelp.ru/
- #
- ###
- #
- # Описание: Скрипт забирает с сайта http://www.timeserver.ru точное время в городах России.
- #
- ###
- #
- # Установка:
- # 1. Скопируйте скрипт newtime.tcl в папку scripts вашего бота.
- # 2. В файле eggdrop.conf впишите строку source scripts/newtime.tcl
- # 3. Сделайте .rehash боту.
- #
- ###
- #
- # Версион хистори:
- #
- # 1.0(28.08.2010) Первая паблик версия.
- # 1.1(29.08.2010) Добавлена поддрежка Suzi ботов.
- # + сравнение времени в друх городах.
- # 1.2(11.11.2010) + работа с многословными городами.
- # + показ даты.
- # 1.3(13.11.2010) + показ времени каждый час.
- #
- ###
- namespace eval newtime {
- # Сбрасываем значения всех переменных.
- foreach p [array names newtime *] { catch {unset newtime($p) } }
- # Указываем канальный флаг(.chanset #chan +newtime для включения скрипта).
- setudef flag newtime
- ### ###
- # Меню настроек ниже этой линии: #
- # ______________________________ #
- ### ###
- variable newtime
- array set newtime {
- pref {!}
- binds {time время dhtvz ешьу}
- msg 1
- delay 5
- autotimer {}
- color1 \00314
- color2 \00303
- color3 \00305
- version "newtime.tcl version 1.3"
- author "tvrsh"
- }
- ### ###
- # Ниже этой линии начинается код, не изменяйте его если не знаете TCL: #
- # ____________________________________________________________________ #
- ### ###
- # Обработка биндов.
- foreach bind [split $newtime(binds) " "] {
- bind pub -|- "$newtime(pref)$bind" ::newtime::newtime_pub
- if {$newtime(msg) >= 1} {
- bind msg -|- "$newtime(pref)$bind" ::newtime::newtime_msg
- }
- }
- # начальный счетчик редиректов
- variable redirs 0
- # Биндим ежечасный показ времени.
- if {$newtime(autotimer) != ""} {
- bind time - "00 * * * *" ::newtime::newtime_onhour
- }
- proc ::newtime::newtime_onhour {min hour day month year} {
- variable newtime
- foreach elem [split $newtime(autotimer) "\n"] {
- if {[llength $elem]} {
- ::newtime::newtime_proc nick uhost hand [lindex [split [join $elem]] 0] 1 [lrange [split [join $elem]] 1 end]
- }
- }
- }
- # Процедура обработки приватных команд.
- proc ::newtime::newtime_msg {nick uhost hand text} {
- variable newtime
- ::newtime::newtime_proc $nick $uhost $hand $nick 0 $text
- }
- # Процедура обработки паблик команд.
- proc ::newtime::newtime_pub {nick uhost hand chan text} {
- variable newtime
- # Проверяем наличие флага.
- if {![channel get $chan newtime]} {
- return 0
- }
- ::newtime::newtime_proc $nick $uhost $hand $chan 0 $text
- }
- # Процедура обработки запроса.
- proc ::newtime::newtime_proc {nick uhost hand chan auto text} {
- global lastbind
- variable newtime
- if {[info exists newtime(lasttime,$chan)] && [expr $newtime(lasttime,$chan) + $newtime(delay)] > [clock seconds]} {
- putserv "NOTICE $nick :$newtime(color1)Запрос не чаще одного раза в $newtime(color2)$newtime(delay) $newtime(color1)[lindex {. секунду секунды секунд} [::newtime::numgrp $newtime(delay)]]."
- putserv "NOTICE $nick :$newtime(color1)Подождите $newtime(color3)[expr $newtime(delay) - [expr [clock seconds] - $newtime(lasttime,$chan)]] $newtime(color1)[lindex {. секунду секунды секунд} [::newtime::numgrp [expr $newtime(delay) - [expr [clock seconds] - $newtime(lasttime,$chan)]]]] до следующего запроса."
- return 0
- }
- set newtime(start) [clock clicks]
- if {[llength [split $text]] == "0"} {
- putserv "PRIVMSG $chan :$newtime(color1)Используйте $newtime(color3)\002$lastbind город\002 $newtime(color1)для просмотра точного текущего времени в городах России ...\003"
- # putserv "PRIVMSG $chan :$newtime(color1)... или $newtime(color3)\002$lastbind город1+город2\002 $newtime(color1)для просмотра разницы во времени между городами.\003"
- return 0
- }
- set text [stripcodes bc [::newtime::en2ru $text]]
- set tmp [list]
- set text_ $text
- foreach _ [split $text] {lappend tmp [string totitle $_]}
- if {[llength $tmp]} {set text [join $tmp]}
- if {![string match "*+*" $text]} {
- # Поддержка Suzi?
- if {![info exists ::sp_version]} {
- set text [encoding convertfrom cp1251 $text]
- }
- set newtime_tok [::http::config -urlencoding utf-8 -useragent "Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; PPC; 240x320)"]
- set newtime_tok [::http::geturl "http://www.timeserver.ru/city/search.html?query=[string map {%20 +} [::http::formatQuery $text]]" -timeout 25000 -command [list ::newtime::data $nick $chan $text_ 1 $auto]]
- }
- }
- proc ::newtime::data {nick chan text type auto newtime_tok} {
- set status [::http::status $newtime_tok]
- set ncode [::http::ncode $newtime_tok]
- array set meta [::http::meta $newtime_tok]
- variable redirs
- variable newtime
- if {$status eq "ok"} {
- if {[string range $ncode 0 1] == "30" && [info exists meta(Location)] && $redirs < 2} {
- if {[string index $meta(Location) 0] == "/"} {
- set url "http://www.timeserver.ru$meta(Location)"
- } else {
- set url $meta(Location)
- }
- ::http::cleanup $newtime_tok
- ::http::geturl $url -timeout 25000 -command [list ::newtime::data $nick $chan $text $type $auto]
- incr redirs
- return
- }
- set redirs 0
- if {[http::ncode $newtime_tok] != "200"} {
- putserv "PRIVMSG $chan :$newtime(color3)\002$nick\002\00304, ошибка запроса $newtime(color1)... ([http::code $newtime_tok])"
- ::http::cleanup $newtime_tok
- return
- }
- set data [::http::data $newtime_tok]
- ::http::cleanup $newtime_tok
- if {$type == "1"} {
- if {[info exists ::sp_version]} {
- putlog "\[newtime\] $nick/$chan/$text"
- # set data [encoding convertfrom cp1251 $data]
- } else {
- putlog "\[newtime\] $nick/$chan/[encoding convertto utf-8 $text]"
- }
- regsub -nocase -- {^.*?<div id="container">} $data "" data
- regsub -nocase -- {<div class="selected">.*?$} $data "" data
- egglib::writedata newtime.txt [list $data]
- if 0 {
- <div class="mainContent">
- <div class="current-time">
- <div class="time">
- <h3><span>ТОЧНОЕ ВРЕМЯ В Зеленоград</span></h3>
- <div class="date" id="main1_global_date">четверг, 17 ноября 2011</div>
- <div class="clock" id="main_clock1">12:36<span class="seconds">:04</span></div>
- <div class="utc"><span class="main1_utc_offset" data-timeformat="am">UTC+4</span></div>
- <div class="daytime night" id="main_clock1_daytime">Ночь</div>
- <div class="clearfloat"></div>
- <span id="main1_sub_timezone_offset">MSK+0</span>, <span id="main1_sub_timezone">Europe/Moscow</span>
- </div>
- }
- if 0 {
- <h3><span class="homecity_name_text text_tgl">Точное время в Москве</span></h3><div class="city_view_clock city_clock"><div class="city_clock_board">15:27<span class="city_clock_board_secs">15</span></div><div class="city_clock_info"><span class="timeofday with_icon day">День</span>UTC+0 MSK-3<span class="timeofday">Europe/London</span><div class="sunrise_sunset"><span class="timeofday"><strong>Восход:</strong> </span><span class="timeofday"><strong>Заход:</strong> </span></div></div></div><p class="city_view_date">пятница, 20 марта 2015</p>
- }
- set nmsg [list]
- if {![regexp -nocase -- {<div class="city_view_box">(.+?)<noindex>} $data - data]} {
- putserv "PRIVMSG $chan :$newtime(color3)\002$nick\002$newtime(color1), Такого города не существует или нет в нашей базе городов, или ошибка обработки $newtime(color1)..."
- set newtime(lasttime,$chan) [clock seconds]
- return 0
- }
- if {[regexp -nocase -- {<div class="city_clock_board">(.+?)</div>.*?<p class="city_view_date">(.+?)</p>} $data -> time date]} {
- regsub -all -- {<.*?>} $date {} date
- regsub -all -- {<.*?>} $time {:} time
- lappend nmsg "$newtime(color3)[string totitle $date], [string trim $time ":"] $newtime(color1)::"
- }
- if {[regexp -nocase -- {<h\d+.*?">Точное время в(.*?)</span>.*?<span class="timeofday">(.*?)</span><div class="sunrise_sunset">} $data -> city region]} {
- lappend nmsg "$newtime(color1)Точное текущее время в г. $newtime(color2)[string trim $city]$newtime(color1), $newtime(color2)[string trim $region]$newtime(color1),"
- }
- if {[regexp -nocase -- {</span>(UTC[\+\-]\d+).*?(MSK[\+\-]\d+)<span} $data - utc msk]} {
- lappend nmsg "$newtime(color2)$utc (по Гринвичу), $msk (по Московскому времени)"
- }
- if {![llength $nmsg]} {
- putserv "PRIVMSG $chan :$newtime(color3)\002$nick\002$newtime(color1), Такого города не существует или нет в нашей базе городов $newtime(color1)..."
- set newtime(lasttime,$chan) [clock seconds]
- return 0
- }
- set nmsg [regsub -all -- {[\x20\x09]+} [regsub -all -- {<.*?>} [join $nmsg] { }] { }]
- if {[regexp -nocase -- {<div class="info">(.*?)</div>} $data -> ninfo]} {
- regsub -all -- {\s+} $ninfo { } ninfo
- regsub -all -- {<br>} $ninfo {$newtime(color1),$newtime(color2)} ninfo
- set ninfo [string trim $ninfo]
- lappend nmsg "$newtime(color2)[subst -nocommand $ninfo]\003$newtime(color1)."
- }
- if {$auto == "0"} {
- putserv "PRIVMSG $chan :$nmsg \00315([format "%.2f" [expr ([clock clicks] - $newtime(start))/1000.0/1000.0]]c.)\003"
- set newtime(lasttime,$chan) [clock seconds]
- return 0
- } else {
- putserv "PRIVMSG $chan :$nmsg \00315([format "%.2f" [expr ([clock clicks] - $newtime(start))/1000.0/1000.0]]c.)\003"
- }
- } else {
- # Поддержка Suzi?
- if {[info exists ::sp_version]} {
- set ntext $text
- } else {
- set ntext [encoding convertfrom cp1251 $text]
- }
- set newtime_tok [::http::config -useragent "Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; PPC; 240x320)"]
- set newtime_tok [::http::geturl "http://www.timeserver.ru/compare/[lindex [split $ntext "+"] 0]/[lindex [split $ntext "+"] 1]" -timeout 15000]
- set data [::http::data $newtime_tok]
- ::http::cleanup $newtime_tok
- if {[info exists ::sp_version]} {
- putlog "\[newtime\] $nick/$chan/Compare: [lindex [split $ntext] 0]/[lindex [split $ntext] 1]"
- set data [encoding convertfrom cp1251 $data]
- } else {
- putlog "\[newtime\] $nick/$chan/Compare: [encoding convertto utf-8 [lindex [split $ntext] 0]/[lindex [split $ntext] 1]]"
- }
- regsub -all -- "\n" $data "" data
- regsub -all -- {\s+} $data { } data
- if {[regexp -nocase -- {</fieldset> </form> </div> <div>(.*?)\..*?<a } $data -> error]} {
- regexp {<div>\ (.*?)\ -} $error "" city
- putserv "PRIVMSG $chan :$newtime(color3)$nick$newtime(color1), города $newtime(color2)$city $newtime(color1)не существует или нет в нашей базе городов.\003"
- set newtime(lasttime,$chan) [clock seconds]
- return 0
- }
- foreach line [split $data "\n"] {
- if {[regexp -nocase -- {<div>(.*?)<span id="clock_head">.*?</span></div>} $line -> date]} {
- set date "$newtime(color3)$date $newtime(color1)::"
- }
- }
- regexp {<div class="difference">(.*?)</div>} $data "" diff
- regsub -all -- "<strong>| </strong>" $diff "\002" diff
- regexp {<div\ class=\"compared\ \">(.*?)<br } $data "" data
- regsub -all -- "<div class=\"city\">" $data "|" data
- regsub -all -- "<a .*?>" $data "" data
- regsub -all -- "</a>" $data "" data
- regsub -all -- "<br>" $data "," data
- foreach line [split $data "|"] {
- if {[regexp -nocase -- {<h2>(.*?) <span>\((.*?)\)</span>.*?</h2> <div class="time"> <div class="clock" id=".*?">(.*?)</div> <div class="info">\ (.*?),} $line -> ncity nregion ntime ninfo]} {
- lappend compare "$newtime(color2)$ncity$newtime(color1), $newtime(color2)$nregion $newtime(color1)($newtime(color3)$ntime$newtime(color1), $newtime(color2)$ninfo$newtime(color1))"
- }
- }
- putserv "PRIVMSG $chan :$date $newtime(color1)Между [lindex $compare 0] $newtime(color1)и [lindex $compare 1] $newtime(color3)$diff \00315([expr ([clock clicks] - $newtime(start))/1000.0]мc.)\003"
- set newtime(lasttime,$chan) [clock seconds]
- return 0
- }
- } else {
- ::http::cleanup $newtime_tok
- putserv "NOTICE $nick :Не удалось соединиться с сервером. Попробуйте позже."
- return
- }
- }
- # Процедура переводит текст набранный в неправильно раксладке.
- proc ::newtime::en2ru {t} {
- return [string map {f а \, б d в u г l д t е \` ё \; ж p з b и q й r к k л v м y н j о g п h р c с n т e у a ф \[ х w ц x ч i ш m ь o щ \] ъ \' э s ы \. ю z я} [string tolower $t]]
- }
- # Возвращает правильное окончание слов.
- proc ::newtime::numgrp {number} {
- switch -glob -- "$number" {
- *11 {return 3}
- *12 {return 3}
- *13 {return 3}
- *14 {return 3}
- *1 {return 1}
- *2 {return 2}
- *3 {return 2}
- *4 {return 2}
- default {return 3}
- }
- }
- # Выводим сообщение о том, что скрипт удачно загружен.
- putlog "\[newtime\] $newtime(version) by $newtime(author) loaded"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement