egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

handling regular expression

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
rix
Halfop


Joined: 21 Sep 2005
Posts: 42
Location: Estonia

PostPosted: Sat Jan 07, 2006 11:10 am    Post subject: handling regular expression Reply with quote

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
View user's profile Send private message
avilon
Halfop


Joined: 13 Jul 2004
Posts: 64
Location: Germany

PostPosted: Sat Jan 07, 2006 11:50 am    Post subject: Reply with quote

Use http package
Back to top
View user's profile Send private message
De Kus
Revered One


Joined: 15 Dec 2002
Posts: 1361
Location: Germany

PostPosted: Sun Jan 08, 2006 10:12 am    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
rix
Halfop


Joined: 21 Sep 2005
Posts: 42
Location: Estonia

PostPosted: Sun Jan 08, 2006 5:28 pm    Post subject: Reply with quote

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
View user's profile Send private message
De Kus
Revered One


Joined: 15 Dec 2002
Posts: 1361
Location: Germany

PostPosted: Mon Jan 09, 2006 11:54 am    Post subject: Reply with quote

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 Smile. I hope it doesnt make a too big slowdown, haven't comapred it yet to be honest Very Happy.
_________________
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
View user's profile Send private message MSN Messenger
rix
Halfop


Joined: 21 Sep 2005
Posts: 42
Location: Estonia

PostPosted: Mon Jan 09, 2006 2:04 pm    Post subject: Reply with quote

The speed is not important at the moment. By the way, I merged 2regexp into 1 and included puthelp right after regexp in the loop, but it keeps repeating one result. :s

[20:01:10] <rix> !latestposts
[20:01:13] <StarBot> Q: DVD subtiitrid - http://www.starpump.ee/viewthread.php?tid=29612
[20:01:15] <StarBot> Q: DVD subtiitrid - http://www.starpump.ee/viewthread.php?tid=29612
[20:01:17] <StarBot> Q: DVD subtiitrid - http://www.starpump.ee/viewthread.php?tid=29612
...and so on

How do I avoid that?
Back to top
View user's profile Send private message
Ehlanna
Voice


Joined: 21 Jul 2005
Posts: 15

PostPosted: Wed Jan 11, 2006 6:48 pm    Post subject: Reply with quote

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
View user's profile Send private message
De Kus
Revered One


Joined: 15 Dec 2002
Posts: 1361
Location: Germany

PostPosted: Thu Jan 12, 2006 8:04 am    Post subject: Reply with quote

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 Very Happy.
_________________
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
View user's profile Send private message MSN Messenger
Ehlanna
Voice


Joined: 21 Jul 2005
Posts: 15

PostPosted: Thu Jan 12, 2006 8:51 pm    Post subject: Reply with quote

Quote:
probably too faithfully pasting


Wood/trre syndrome, far too easy to suffer from!
Back to top
View user's profile Send private message
rix
Halfop


Joined: 21 Sep 2005
Posts: 42
Location: Estonia

PostPosted: Mon Jan 16, 2006 2:28 pm    Post subject: Reply with quote

Big thanks, guys!

I hope I'm not too arrogant when asking another thing. Rolling Eyes

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
View user's profile Send private message
Ehlanna
Voice


Joined: 21 Jul 2005
Posts: 15

PostPosted: Mon Jan 16, 2006 10:02 pm    Post subject: Reply with quote

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
View user's profile Send private message
rix
Halfop


Joined: 21 Sep 2005
Posts: 42
Location: Estonia

PostPosted: Mon Jan 23, 2006 5:01 pm    Post subject: Reply with quote

Im still messing around with these files. Sad

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
View user's profile Send private message
De Kus
Revered One


Joined: 15 Dec 2002
Posts: 1361
Location: Germany

PostPosted: Fri Jan 27, 2006 4:32 pm    Post subject: Reply with quote

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 Smile.
_________________
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
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber