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.

Simple socket script

Old posts that have not been replied to for several years.
L
Longbow
Voice
Posts: 29
Joined: Thu Jan 13, 2005 9:24 am

Simple socket script

Post by Longbow »

Does anyone have a working example of a simple sockets script (that get info from a website).

I've tried using the egghttp.tcl tutorial, but it never works. :?
Last edited by Longbow on Fri Feb 04, 2005 6:41 am, edited 1 time in total.
User avatar
DragnLord
Owner
Posts: 711
Joined: Sat Jan 24, 2004 4:58 pm
Location: C'ville, Virginia, USA

Post by DragnLord »

this is snipped from the weather3.5.tcl:
if {[catch {set wzsock [socket -async www.wunderground.com 80]} sockerr]} {
puthelp "NOTICE $nick :$sockerr"
puthelp "NOTICE $nick :Try again later, look out the window till then!"
close $wzsock
return 0
}
puts $wzsock "GET $webpage"
flush $wzsock
It should give you a basic idea of how to pull the page, parsing the information is up to you. :)
User avatar
DragnLord
Owner
Posts: 711
Joined: Sat Jan 24, 2004 4:58 pm
Location: C'ville, Virginia, USA

Post by DragnLord »

if all else fails, you can check out StrikeLight's egghttp.tcl script and tutorial that can be found on www.tclscript.com

you can get the script from here

tutorial
User avatar
^DooM^
Owner
Posts: 772
Joined: Tue Aug 26, 2003 5:40 pm
Location: IronForge
Contact:

Post by ^DooM^ »

DragnLord wrote:if all else fails, you can check out StrikeLight's egghttp.tcl script and tutorial that can be found on www.tclscript.com

you can get the script from here

tutorial
Longbow wrote:I've tried using the egghttp.tcl tutorial, but it never works.
I think the best way is to do as DragnLord first suggested and use other scripts to help you get what you basically need :)
The lifecycle of a noob is complex. Fledgling noobs gestate inside biometric pods. Once a budding noob has matured thru gestation they climb out of their pod, sit down at a PC, ask a bunch of questions that are clearly in the FAQ, The Noob is born
L
Longbow
Voice
Posts: 29
Joined: Thu Jan 13, 2005 9:24 am

Post by Longbow »

DragnLord wrote:if all else fails, you can check out StrikeLight's egghttp.tcl script and tutorial that can be found on www.tclscript.com

you can get the script from here

tutorial
Thats what I said I used, but always get errors.
Tcl error [pub:on]: invalid command name "egghttp:geturl"

Code: Select all

set sock [egghttp:geturl $url your_callbackproc]
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

look true this forum there is a sample on how you can use a async socket and a regular socket so.. search ;)
XplaiN but think of me as stupid
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Part 1 from the tutorial: Loading and Checking for egghttp.tcl

If it can't find that command, maybe you left that part out!
L
Longbow
Voice
Posts: 29
Joined: Thu Jan 13, 2005 9:24 am

Post by Longbow »

Ok so....

Code: Select all

# egghttp_example.tcl

# Config
set url "http://www.mysite.com/page.php?get=online"

# End of config

if {![info exists egghttp(version)]} {
  putlog "egghttp.tcl was NOT successfully loaded."
  putlog "egghttp_example.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]
  
    regsub -all "\n" $body "" body
    regsub -all -nocase {<br>} $body "<br>\n" body

    regexp {(.*?) Total, (.*?) guests, (.*?) members, (.*?) anonymous members} $body - online

    putserv "NOTICE $nick :$online"

  }

bind pub - !online pub:on
proc pub:on {nick uhost hand chan text} {
    global url 
    set sock [egghttp:geturl $url your_callbackproc]
    return 1
  }  

  putlog "egghttp_example.tcl has been successfully loaded."
}
the .php page contains the following....

Code: Select all

70 Total, 11 guests, 59 members, 0 anonymous members
I'm not getting any error message now, however, when doing the command (!online) I got don't anything either.
User avatar
DragnLord
Owner
Posts: 711
Joined: Sat Jan 24, 2004 4:58 pm
Location: C'ville, Virginia, USA

Post by DragnLord »

this might work better for you:
proc your_callbackproc {sock} {
global url
set headers [egghttp:headers $sock]
set body [egghttp:data $sock]

regsub -all "\n" $body "" body
regsub -all -nocase {<br>} $body "<br>\n" body

regexp {(.*?) Total} $body match ptotal
regexp {(.*?) guests} $body match pguests
regexp {(.*?) members} $body match pmemb
regexp {(.*?) anonymous} $body match panon

puthelp "NOTICE $nick :$ptotal Total, $pguests Guests, $pmemb Members, $panon Anonymous users online"

}
I've never had much luck using more then one (.*?) per regexp.[/quote]
L
Longbow
Voice
Posts: 29
Joined: Thu Jan 13, 2005 9:24 am

Post by Longbow »

Thanks, but your_callbackproc still isn't doing anything :shock:
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Well, one thing that jumps out at me is that in your callback, $nick is undefined.

Also, who knows if your regexp is actually working. There are many ways the html could be formatted to show the text you've given. If you gave the actual url it might help.
User avatar
DragnLord
Owner
Posts: 711
Joined: Sat Jan 24, 2004 4:58 pm
Location: C'ville, Virginia, USA

Post by DragnLord »

Since your_callbackproc is called from pub:on, wouldn't $nick have been defined by pub:on?

The best way to "debug" a tcl script such as this is the use of putlog statements to show if the variables actually contain information.

example:
regexp {(.*?) Total} $body match ptotal
putlog "total members = $ptotal"
The script will then show the variable $ptotal in telnet/dcc chat.

Without having the actual url to check the source coding, it's nearly impossible to give further aid in parsing the information.
L
Longbow
Voice
Posts: 29
Joined: Thu Jan 13, 2005 9:24 am

Post by Longbow »

stdragon wrote:Well, one thing that jumps out at me is that in your callback, $nick is undefined.

Also, who knows if your regexp is actually working. There are many ways the html could be formatted to show the text you've given. If you gave the actual url it might help.
Mm... true. Since it was originally a DCC command, i'll look into that.

As far as the web page, I did some server side coding to make it easyier for me.

It shows the amount of people online only in text. No extra html tags or anything are in the source code.

Edit: Ok I used putlog instead of privhelp, but still doesn't work. I used..

Code: Select all

putlog "test"
And it worked, so something works, but when using

Code: Select all

  putlog "$ptotal Total, $pguests Guests, $pmemb Members, $panon Anonymous users online" 
Nothing happens.
User avatar
DragnLord
Owner
Posts: 711
Joined: Sat Jan 24, 2004 4:58 pm
Location: C'ville, Virginia, USA

Post by DragnLord »

your code isn't parsing the webpage correctly

this will get messy, but at least it'll tell you if there is anything in $body to read:

putlog "body = $body"

that will show you everything in the $body variable at that point

from there it's a matter of tuning the regexps
such as:
putlog "body = $body"
regexp {(.*?) Total} $body match ptotal
L
Longbow
Voice
Posts: 29
Joined: Thu Jan 13, 2005 9:24 am

Post by Longbow »

body =
lol :(
Locked