package require http 2.5.2
# SM-TV - Copyright (C) 2007 Meliorator@irc.freenode
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
namespace eval SMTV {
variable Conf
variable Version
namespace export *
array set Conf {
TVRage_Base_Url http://www.tvrage.com
TVRage_Search_Url /search.php?search=%%SERIESNAME%%
TVRage_Search_Exp {(?:</td></tr>.*href='(http://[^']+)' ?>([^<>]+)</a>){1,1}?}
Verbose TRUE
}
}
proc SMTV::Title {urlname search} { variable Conf; variable CallBack
if [catch {set url $Conf(${urlname}_Base_Url)$Conf(${urlname}_Search_Url); set exp $Conf(${urlname}_Search_Exp)}] {
Put "$urlname is not a vaild url name, specify one of: [array names Conf *_Base_Url]"
};
set url [string map "%%SERIESNAME%% [::http::formatQuery [string map {_ " " - " " ( " " ) " " . " "} $search]]" $url]
set token [::http::geturl $url -command "SMTV::TitleParse [incr CallBack(0)] {$search} {$exp}"]
trace add variable CallBack($CallBack(0)) write Out
puts 1.5
}
proc SMTV::Out {array element operation} {
variable CallBack
Put $CallBack($element)
trace del variable CallBack($element) write Out
unset CallBack($element)
}
proc SMTV::TitleParse {callback search exp token} {
variable CallBack
set data [::http::data $token]; ::http::cleanup $token
for {set i [expr [llength [set getname [split $search]]] -1]} {$i >= 0} {incr i -1} { set getname [lrange $getname 0 $i]
foreach {. url title} [regexp -all -inline $exp $data] {
set chkname [string map {? * . * " " * - * _ * ( * ) * + * , * ' * : * ! * + * * * ^ * $ * [ * ] * \{ * \} * \" *} [join $getname]]
set chktitle [string map {? * . * " " * - * _ * ( * ) * + * , * ' * : * ! * + * * * ^ * $ * [ * ] * \{ * \} * \" *} $title]
if {[string match -noc *$chkname* $title] || [string match -noc $chktitle $search]} {
# return [list url $url title $title]
set CallBack($callback) [list url $url title $title]
return
}
}
}; Put "$search ::: SERIES NOT FOUND AT [UrlName $url]"
}
proc SMTV::Put {text} { if {$::tcl_interactive == 0 && [info commands putlog] == "putlog"} { putlog $text } { puts $text } }
Title TVRage [join $argv] }
vwait CallBack(1)