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 

Getting values from a url (and count lines) - XML!
Goto page Previous  1, 2
 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests
View previous topic :: View next topic  
Author Message
JKM
Voice


Joined: 06 Dec 2008
Posts: 30

PostPosted: Tue Dec 16, 2008 8:35 am    Post subject: Reply with quote

It's like this:
Code:
<?xml version='1.0' encoding='us-ascii' ?>
<SEARCH>

   <field>
      <color>blue</color>
      <date>19.12.08</date>
      <nick>tomekk</nick>
      <homepage>egghelp.org</homepage>

      <posts>46</posts>
      <birthday>N/A</birthday>
      <mail>egg@tomekk</mail>
      <userid>1</userid>
   </field>

</SEARCH>
And I just want to get the values from:
<color></color>
<date></date>
<posts></posts>
<birthday></birthday>

Edit: I'm sorry, it's the !check script. And btw, I want the same with N/A. if one (ore more) of the mentioned xml tags contains "N/A" I would like to respond "N/A". :-p
Back to top
View user's profile Send private message
tomekk
Master


Joined: 28 Nov 2008
Posts: 255
Location: Oswiecim / Poland

PostPosted: Tue Dec 16, 2008 10:16 am    Post subject: Reply with quote

Code:
# Author: tomekk
# e-mail:  tomekk/@/oswiecim/./eu/./org
# home page: http://tomekk.oswiecim.eu.org/
#
# Version 0.2
#
# This file is Copyrighted under the GNU Public License.
# http://www.gnu.org/copyleft/gpl.html

# lines chans
set lines_chans {#chan1 #chan2}

# check chans
set check_chans {#chan1 #chan2}

# search chans
set search_chans {#chan1 #chan2}

# url to list file, row by row
set list_url "http://localhost/list.txt"

# url to check script, one row
set check_url "http://localhost/check.php"

# url to search script, row by row
set search_url "http://localhost/search.php"

# check_url script argument,
# will be: $check_url?$check_url_arg=$query, http://localhost/check.php?value=$query
set check_url_arg "value"

# search_url script argument,
# will be: $search_url?$search_url_arg=$query, http://localhost/search.php?value=$query
set search_url_arg "value"

#######################################################################
bind pub - !lines lines_proc
bind pub - !check check_proc
bind pub - !search search_proc

package require http

proc lines_proc { nick uhost hand chan arg } {
   global list_url lines_chans

   if {[lsearch $lines_chans $chan] != -1} {
      set data_lines [grab_http_data 1 $list_url "" ""]

      if {$data_lines != "error"} {
         set lines_counter 0
   
         # we can count list with [llength] but we dont want to count empty lines { }
         foreach data_line [split $data_lines "\n"] {
            if {$data_line != ""} {
               incr lines_counter 1
            }
         }
         putquick "PRIVMSG $chan :[expr $lines_counter - 1]"
      }
   }
}

proc check_proc { nick uhost hand chan arg } {
   global check_url check_url_arg check_chans

   if {[lsearch $check_chans $chan] != -1} {
      set check_arg [lindex [split $arg] 0]

      if {$check_arg != ""} {
         set xml_data [grab_http_data 2 $check_url $check_url_arg $check_arg]

         if {$xml_data != "error"} {
            regsub -all "\t" $xml_data "" xml_data
            regsub -all "\n" $xml_data "" xml_data
            regsub -all "\r" $xml_data "" xml_data

            regsub "<field>" $xml_data "<field>\n" xml_data
            regsub "<\/field>" $xml_data "\n<\/field>" xml_data

            set xml_data_field [lindex [split $xml_data "\n"] 1]

            set xml_data_tags {color date nick homepage posts birthday mail userid}

            foreach xml_tag $xml_data_tags {
               regsub -nocase "<$xml_tag>| <$xml_tag>" [string trim $xml_data_field] "" xml_data_field
               regsub -nocase "<\/$xml_tag>" [string trim $xml_data_field] "\n" xml_data_field
            }

            set xml_data_lines [split $xml_data_field "\n"]

            set x_color [lindex $xml_data_lines 0]
            set x_date [lindex $xml_data_lines 1]
            set x_posts [lindex $xml_data_lines 4]
            set x_birthday [lindex $xml_data_lines 5]

            putquick "PRIVMSG $chan :color: $x_color, date: $x_date, posts: $x_posts, birthday: $x_birthday"
         }
      }
   }
}

proc search_proc { nick uhost hand chan arg } {
   global search_url search_url_arg search_chans

   if {[lsearch $search_chans $chan] != -1} {
      set search_arg [lindex [split $arg] 0]

      if {$search_arg != ""} {
         set data_lines [grab_http_data 2 $search_url $search_url_arg $search_arg]

         if {$data_lines != "error"} {
            set lines_counter 0

            set data_lines [split $data_lines "\n"]

            foreach data_line $data_lines {
               if {$data_line != ""} {
                  incr lines_counter 1
               }
            }

            if {$lines_counter > 3} {
               set three_lines [lrange $data_lines 0 2]

               foreach one_line $three_lines {
                  putquick "PRIVMSG $chan :$one_line"
               }

               set the_rest [expr $lines_counter - 3]

               putquick "PRIVMSG $chan :... And further $the_rest lines - $search_url?$search_url_arg=$search_arg"
            } else {
               foreach one_line $data_lines {
                  putquick "PRIVMSG $chan :$one_line"
               }
            }
         }
      }
   }
}

proc grab_http_data { type url query arg } {
   set user_agent "http-query.tcl <eggdrop script>"
         
   set tiny_token [http::config -useragent $user_agent]

   if {$type == 1} {   
      set tiny_token [http::geturl $url -timeout 10000]
   } else {
      # for longer queries should be ::http::formatQuery
      set proper_url "$url?$query=$arg"
      set tiny_token [http::geturl $proper_url -timeout 10000]
   }
                  
   set html_data [http::data $tiny_token]
                     
   if {$html_data != ""} {
      return $html_data
   } {
      return "error"
   }
}

putlog "http-query.tcl ver 0.1 by tomekk loaded"


tested with your xml, output:
Quote:
15:15:31 <@tomekk> !check blah
15:15:31 < botty> color: blue, date: 19.12.08, posts: 46, birthday: N/A


try :>
Back to top
View user's profile Send private message Visit poster's website
JKM
Voice


Joined: 06 Dec 2008
Posts: 30

PostPosted: Tue Dec 16, 2008 4:32 pm    Post subject: Reply with quote

Would this be correct?
Code:
proc check_proc { nick uhost hand chan arg } {
   global check_url check_url_arg check_chans

   if {[lsearch $check_chans $chan] != -1} {
      set check_arg [lindex [split $arg] 0]

      if {$check_arg != ""} {
         set xml_data [grab_http_data 2 $check_url $check_url_arg $check_arg]

         if {$xml_data != "error"} {
            regsub -all "\t" $xml_data "" xml_data
            regsub -all "\n" $xml_data "" xml_data
            regsub -all "\r" $xml_data "" xml_data

            regsub "<field>" $xml_data "<field>\n" xml_data
            regsub "<\/field>" $xml_data "\n<\/field>" xml_data

            set xml_data_field [lindex [split $xml_data "\n"] 1]

            set xml_data_tags {color date nick homepage posts birthday mail userid}

            foreach xml_tag $xml_data_tags {
               regsub -nocase "<$xml_tag>| <$xml_tag>" [string trim $xml_data_field] "" xml_data_field
               regsub -nocase "<\/$xml_tag>" [string trim $xml_data_field] "\n" xml_data_field
            }

            set xml_data_lines [split $xml_data_field "\n"]

            set x_color [lindex $xml_data_lines 0]
            set x_date [lindex $xml_data_lines 1]
            set x_posts [lindex $xml_data_lines 4]
            set x_birthday [lindex $xml_data_lines 5]

            if { $x_date != "N/A" } {
              putquick "PRIVMSG $chan :color: $x_color, date: $x_date, posts: $x_posts, birthday: $x_birthday"
            } else {
              putquick "PRIVMSG $chan :Nothing found!"
            }
         }
      }
   }
}
Back to top
View user's profile Send private message
tomekk
Master


Joined: 28 Nov 2008
Posts: 255
Location: Oswiecim / Poland

PostPosted: Tue Dec 16, 2008 6:43 pm    Post subject: Reply with quote

yap, if date will be exactly "N/A"

u can always use string match *smth* or regexp etc Smile
Back to top
View user's profile Send private message Visit poster's website
JKM
Voice


Joined: 06 Dec 2008
Posts: 30

PostPosted: Wed Dec 17, 2008 12:23 pm    Post subject: Reply with quote

Thanks - it's working perfectly... almost. If I'm setting three chans for "set *_chans {#chan1 #chan2}", the script won't work for the first two chans.
Back to top
View user's profile Send private message
tomekk
Master


Joined: 28 Nov 2008
Posts: 255
Location: Oswiecim / Poland

PostPosted: Wed Dec 17, 2008 12:39 pm    Post subject: Reply with quote

damn, I just tested it on 4 channels (defined 10) and all works,
hmm

watch out for BIG and small letters :>
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests All times are GMT - 4 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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