| View previous topic :: View next topic |
| Author |
Message |
pitbull Voice
Joined: 12 Aug 2007 Posts: 13
|
Posted: Fri Aug 17, 2007 1:12 pm Post subject: HTTP choking on ^ character in URL |
|
|
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:
| Quote: | set stock [string toupper [lindex $arg 0]]
set query "http://finance.yahoo.com/d/quotes.csv?s=$stock&f=st5l9c6p4b1a3&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? _________________ irc://irc.efnet.net/peakoil |
|
| Back to top |
|
 |
rosc2112 Revered One

Joined: 19 Feb 2006 Posts: 1454 Location: Northeast Pennsylvania
|
Posted: Fri Aug 17, 2007 1:54 pm Post subject: |
|
|
You have to translate those chars into html before you can use them. Typically we use string map to do that, such as:
| Code: |
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.) |
|
| Back to top |
|
 |
pitbull Voice
Joined: 12 Aug 2007 Posts: 13
|
Posted: Fri Aug 17, 2007 3:00 pm Post subject: |
|
|
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 _________________ irc://irc.efnet.net/peakoil |
|
| Back to top |
|
 |
rosc2112 Revered One

Joined: 19 Feb 2006 Posts: 1454 Location: Northeast Pennsylvania
|
Posted: Fri Aug 17, 2007 5:02 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
jinsonxu Voice
Joined: 05 Oct 2008 Posts: 1
|
Posted: Sun Oct 05, 2008 11:20 am Post subject: |
|
|
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.  |
|
| Back to top |
|
 |
|