| View previous topic :: View next topic |
| Author |
Message |
rix Halfop
Joined: 21 Sep 2005 Posts: 42 Location: Estonia
|
Posted: Sat Jan 07, 2006 11:10 am Post subject: handling regular expression |
|
|
Hey!
Im new to this, so I try to make myself clear as possible. I need to take url and text from this link and show them in the channel.
I tryed something like this:
| Code: |
set url "http://www.starpump.ee/linkme.php"
if {![info exists egghttp(version)]} {
putlog "egghttp.tcl was NOT successfully loaded."
putlog "lasttopics.tcl has not been loaded as a result."
} else {
proc your_callbackproc {sock} {
global url
set headers [egghttp:headers $sock]
set body [egghttp:data $sock]
foreach line [split $body \n] {
regexp -all {<a href=".*?">(.*?)</a>} $line text
}
putserv "PRIVMSG $chan : $text
}
bind pub - !lasttopics our:dcctrigger
proc our:dcctrigger {hand idx text} {
global url
set sock [egghttp:geturl $url your_callbackproc]
set chan #star
return 1
}
putlog "egghttp_example.tcl has been successfully loaded."
}
|
It gives error: Tcl error [our:dcctrigger]: called "our:dcctrigger" with too many arguments. I thought I can even use 5 arguments in the brackets.
Anyway, there's enough tutorials saying what to do. Yes, that's good. The one major problem is that i can't find any instructions how to turn it to actually working script. |
|
| Back to top |
|
 |
avilon Halfop

Joined: 13 Jul 2004 Posts: 64 Location: Germany
|
Posted: Sat Jan 07, 2006 11:50 am Post subject: |
|
|
| Use http package |
|
| Back to top |
|
 |
De Kus Revered One

Joined: 15 Dec 2002 Posts: 1361 Location: Germany
|
Posted: Sun Jan 08, 2006 10:12 am Post subject: |
|
|
Read TCLCommands.doc, section "BIND" especially for DCC and PUB. Its not up to, it must match exactly (well, actually there are workarounds, but they should be only used if dynamic is requried).
Tip: you will probably NOT want to output unfiltered regexp -all output. _________________ De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens... |
|
| Back to top |
|
 |
rix Halfop
Joined: 21 Sep 2005 Posts: 42 Location: Estonia
|
Posted: Sun Jan 08, 2006 5:28 pm Post subject: |
|
|
Okay, I did a little research again and got it working. Just one thing more. It shows only one (first) result. How do i loop this thingie?
| Code: | set url "http://www.starpump.ee/linkme.php"
set trigger "!latestposts"
set channel "#star"
if {![info exists egghttp(version)]} {
putlog "egghttp.tcl was NOT successfully loaded."
putlog "latestposts.tcl has not been loaded as a result."
} else {
proc setup {sock} {
global url channel
set headers [egghttp:headers $sock]
set body [egghttp:data $sock]
foreach line [split $body \n] {
regexp {<a href=".*?"target="_blank">(.*?)</a>} $body - title
regexp {<a href="(.*?)"target="_blank">} $body - link
}
puthelp "PRIVMSG $channel :$title - $link "
}
bind pub -|* $trigger action
proc action {nick host hand chan text} {
global rssfeed
set sock [egghttp:geturl $url setup]
return 1
}
putlog "latestposts.tcl has been successfully loaded."
}
|
Thanks! |
|
| Back to top |
|
 |
De Kus Revered One

Joined: 15 Dec 2002 Posts: 1361 Location: Germany
|
Posted: Mon Jan 09, 2006 11:54 am Post subject: |
|
|
include 'puthelp "PRIVMSG $channel :$title - $link "' in the foreach loop.
btw you can merge the 2 regexp into 1:
regexp {<a href="([^"]+)" target="_blank">([^<]+)</a>} $body {} link title
Note: I assumed the missing space in the expression was an error. And I usually prefer something like [^<] to . because regexp tries to find the widest match, but you dont want the match to pass the closing </ tags or " braces, so matching every character but that ones are usually more predictable . I hope it doesnt make a too big slowdown, haven't comapred it yet to be honest . _________________ De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens... |
|
| Back to top |
|
 |
rix Halfop
Joined: 21 Sep 2005 Posts: 42 Location: Estonia
|
|
| Back to top |
|
 |
Ehlanna Voice
Joined: 21 Jul 2005 Posts: 15
|
Posted: Wed Jan 11, 2006 6:48 pm Post subject: |
|
|
| At a rough guess, and speaking as a self-taught , beginner tcl coder, I would say that you want to swap $body to $line in the regexp command as $line is the variable that changes with each iteration of the loop. |
|
| Back to top |
|
 |
De Kus Revered One

Joined: 15 Dec 2002 Posts: 1361 Location: Germany
|
Posted: Thu Jan 12, 2006 8:04 am Post subject: |
|
|
| Ehlanna wrote: | | I would say that you want to swap $body to $line in the regexp command as $line is the variable that changes with each iteration of the loop. |
lol, beat why I didnt notice that myself, was probably too faithfully pasting together . _________________ De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens... |
|
| Back to top |
|
 |
Ehlanna Voice
Joined: 21 Jul 2005 Posts: 15
|
Posted: Thu Jan 12, 2006 8:51 pm Post subject: |
|
|
| Quote: | | probably too faithfully pasting |
Wood/trre syndrome, far too easy to suffer from! |
|
| Back to top |
|
 |
rix Halfop
Joined: 21 Sep 2005 Posts: 42 Location: Estonia
|
Posted: Mon Jan 16, 2006 2:28 pm Post subject: |
|
|
Big thanks, guys!
I hope I'm not too arrogant when asking another thing.
I have another script based on same structure, but it doesn't recognize the "?" in the url. It's important, as channel= will give appropriate feed. It's weird, cause other scripts are doing well. :S
| Code: | set feed "http://www.w3.ee/export/tv.php?channel=2"
set trigger "!kanal2"
set channel "#star"
if {![info exists egghttp(version)]} {
putlog "egghttp.tcl was NOT successfully loaded."
putlog "tv2.tcl has not been loaded as a result."
} else {
proc callback{sock} {
global feed channel
set headers [egghttp:headers $sock]
set body [egghttp:data $sock]
foreach line [split $body \n] {
regexp {(.*?)<b>} $body - time
regexp {<b>(.*?)</b>} $body - title
regexp {</b>.*?<br>(.*?)<br>} $body - desc
}
puthelp "PRIVMSG $channel :(Algus: $time) \002$title\002"
puthelp "PRIVMSG $channel :$desc"
}
bind pub -|* $trigger top:trigger
proc top:trigger {nick host hand chan text} {
global feed
set sock [egghttp:geturl $rssfeed callback]
return 1
}
putlog "tv2.tcl has been successfully loaded."
}
|
It takes information from w3.ee/export/tv.php though it should take it from w3.ee/export/tv.php?channel=2. |
|
| Back to top |
|
 |
Ehlanna Voice
Joined: 21 Jul 2005 Posts: 15
|
Posted: Mon Jan 16, 2006 10:02 pm Post subject: |
|
|
| My best guess would be that it was being treated as a special character, and you would thus need to quote it ... \? |
|
| Back to top |
|
 |
rix Halfop
Joined: 21 Sep 2005 Posts: 42 Location: Estonia
|
Posted: Mon Jan 23, 2006 5:01 pm Post subject: |
|
|
Im still messing around with these files.
First script still gives only one result. I only changed the regexp command. Somehow the 2in1 regexp doesn't work anymore. Just no result at all. If I add puthelp command in loop, it doesn't show any result either.
| Code: | set url "http://www.starpump.ee/linkme.php"
set trigger "!latestposts"
set channel "#star"
if {![info exists egghttp(version)]} {
putlog "egghttp.tcl was NOT successfully loaded."
putlog "latestposts.tcl has not been loaded as a result."
} else {
proc setup {sock} {
global url channel
set headers [egghttp:headers $sock]
set body [egghttp:data $sock]
foreach line [split $body \n] {
regexp {<a href=".*?"target="_blank">(.*?)</a>} $line - title
regexp {<a href="(.*?)"target="_blank">} $line - link
}
puthelp "PRIVMSG $channel :$title - $link "
}
bind pub -|* $trigger action
proc action {nick host hand chan text} {
global url
set sock [egghttp:geturl $url setup]
return 1
}
putlog "latestposts.tcl has been successfully loaded."
}
|
If puthelp command is in the loop also, it doesn't reply anything. |
|
| Back to top |
|
 |
De Kus Revered One

Joined: 15 Dec 2002 Posts: 1361 Location: Germany
|
Posted: Fri Jan 27, 2006 4:32 pm Post subject: |
|
|
all characters under REGULAR EXPRESSION SYNTAX on http://www.tcl.tk/man/tcl8.4/TclCmd/re_syntax.htm must be escaped. Reading the manual usually enligthens . _________________ De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens... |
|
| Back to top |
|
 |
|