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.

HTTP choking on ^ character in URL

Help for those learning Tcl or writing their own scripts.
Post Reply
p
pitbull
Voice
Posts: 13
Joined: Sun Aug 12, 2007 12:12 pm

HTTP choking on ^ character in URL

Post by pitbull »

I am modifying a script that retrieves stock quotes from yahoo finance. It works for most symbols, such as DJI. however, some require a ^ infront of the stock, like ^IXIC . Eggdrop spits out an error when that symbol is used:

[11:49] Tcl error [pub:quote]: Illegal characters in URL path

This is the part of the script that retrieves the data:
set stock [string toupper [lindex $arg 0]]
set query "http://finance.yahoo.com/d/quotes.csv?s ... 1a3&e=.csv"
set token [http::geturl $query]
set all [http::data $token]
regsub -all \" $all "" all
set all [split $all ","]
so if $query contains a ^, it chokes. How can I get around this?
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

You have to translate those chars into html before you can use them. Typically we use string map to do that, such as:

Code: Select all

proc myproc {n u h c t} {
        set myurl [string map $::mapvar $inputvar]
        # geturl or whatever after translating the input (inputvar)
}

# String map basically uses pairs, the char to be changed, and then char to change it to.. Some chars are quoted to protect them or indicate they all go together. Also note the $:: above refers to global space (outside of the proc) so $::mapvar means this setting below:

set mapvar {
"\ " +  : %3A  \\ %5C%5C  \[ %5B  \] %5D  \# %23  \" %22    \' %27  ! %21  @ %40 $ %24  \( %28  \) %29  ~  %7E  `  %60  %  %25   ^   %5E  \& %26  = %3D  + %2B| %7C  \{ %7B  \} %7D  /  %2F  \? %3F  ,  %2C   <   %3C   > %3E  ; %3B
}

# ignore the word wrap above, string map doesn't care as long as the var has an even number of chars/words.

Thats a good collection of strings to handle most html input, there's other examples around, much more extensive ones (look at my dictionary script in the archive for example, that has over 100 chars in its map.)
p
pitbull
Voice
Posts: 13
Joined: Sun Aug 12, 2007 12:12 pm

Post by pitbull »

Thanks, however now I get the error
[13:55] Tcl error [pub:quote]: char map list unbalanced

I don't understand the charmap thing to debug it myself :?

Edit: Well after much studying of the charmap var, I realized there was a space missing in front of the | char. Also, I removed the remapping of the : and /, because I got the error Tcl error [pub:quote]: Unsupported URL: http%3A%2F%2Ffinance.yahoo.com%2Fd%2Fquotes.csv%3Fs%3D%5ESPC%26f%3Dst5l9c6p4b1a3%26e%3D.csv
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

Don't feed http:// to the string map and it won't mangle your input var.. Just use the url without http:// until you're giving the data to geturl (you can stuff http://$input into geturl, the http:// is always present so it can be hard-coded)

And yeah my example got word-wrapped by the forum's editor so it might've been a little messed up.
j
jinsonxu
Voice
Posts: 1
Joined: Sun Oct 05, 2008 11:18 am

Post by jinsonxu »

Well, i don't use the manual mapping solution. Simply encoded the query with ::http::formatQuery and it works.

Not sure whether i got that right. Am new to TCL. :P
Post Reply