This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Getting values from a url (and count lines) - XML!

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
J
JKM
Voice
Posts: 30
Joined: Sat Dec 06, 2008 11:14 pm

Post by JKM »

It's like this:

Code: Select all

<?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
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

Code: Select all

# 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:
15:15:31 <@tomekk> !check blah
15:15:31 < botty> color: blue, date: 19.12.08, posts: 46, birthday: N/A
try :>
J
JKM
Voice
Posts: 30
Joined: Sat Dec 06, 2008 11:14 pm

Post by JKM »

Would this be correct?

Code: Select all

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!"
            }
         }
      }
   }
} 
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

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

u can always use string match *smth* or regexp etc :)
J
JKM
Voice
Posts: 30
Joined: Sat Dec 06, 2008 11:14 pm

Post by JKM »

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.
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

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

watch out for BIG and small letters :>
Post Reply