Rune Lain Knudsen wrote:
I'm trying to get netclient to download an xml file from my subscription at weather.com but a strange thing happened. I try to send a fairly long GET message (containing a search string, a few parameters and ID+password), and it seems to work on a regular browser but not in PD. When I connect the message box containing the GET message to a print object it doesn't print the whole string in the console, it seems like there's a limit on either the message box(how many characters it can send) or the print object(in which case I'm probably chasing the wrong problem altogether). I've attached the test patch but the user ID and the password is false so if anyone tries it out they will probably get an error page sent back(but then at least the query works). I hope I'm explaining this clearly enough... Is there a workaround to this problem, or have I just done something wrong?
It seemed to me that using pd to parse xml is like using a screwdriver to hammer nails so I did it using awk, since awk is very simple and efficient at parsing and can also communicate via tcp/ip. Here I access a weather site and extract the relevant data in an awk script which it sends to a [netreceive] in pd whenever the pd patch requests it. I'm running awk in a Cygwin terminal in WinXP and pd 0.37.1. Martin
#N canvas 0 1 619 491 10; #X obj 45 213 netsend; #X msg 26 4 connect localhost 8888; #X msg 14 187 disconnect; #X msg 91 187 send hello $1; #X obj 74 86 counter; #X floatatom 91 113 5 0 0 0 - - -; #X obj 40 24 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1 ; #X floatatom 352 166 5 0 0 0 - - -; #X obj 234 134 netreceive 8887; #X text 233 113 run ./get_weather.awk in a cygwin terminal for this to work; #X obj 232 244 vsl 15 128 -40 40 0 0 empty empty empty 0 -8 0 8 -258699 -241291 -44926 10160 1; #X text 385 165 connections; #X obj 232 221 nbx 5 14 -1e+037 1e+037 0 0 empty empty empty 0 -6 0 10 -262144 -1 -1 24 256; #X obj 289 221 nbx 5 14 -1e+037 1e+037 0 0 empty empty empty 0 -6 0 10 -262144 -1 -1 102 256; #X obj 234 201 unpack 1 1; #X obj 289 244 vsl 15 128 0 127 0 0 empty empty empty 0 -8 0 8 -62784 -241291 -258699 10200 1; #X obj 245 166 print >; #X text 234 100 Montreal current temperature & pressure; #X text 194 373 degrees C; #X text 291 373 kPa; #X obj 41 48 metro 60000; #X text 116 40 once a minute , even though the site updates about once an hour....; #X text 176 4 once get_weather.awk is running , hit connect; #X text 61 24 trigger a message; #X connect 1 0 0 0; #X connect 2 0 0 0; #X connect 3 0 0 0; #X connect 4 0 5 0; #X connect 5 0 3 0; #X connect 6 0 20 0; #X connect 8 0 14 0; #X connect 8 0 16 0; #X connect 8 1 7 0; #X connect 12 0 10 0; #X connect 13 0 15 0; #X connect 14 0 12 0; #X connect 14 1 13 0; #X connect 20 0 4 0;
#!/bin/awk -f
# based on http://www.gnu.org/software/gawk/manual/gawkinet/gawkinet.html#GETURL
# Gets an html text weather report for Montreal, extracts the current temperature in degrees Celsius
# and pressure in kiloPascals.
# Waits for some message from pd on tcp port 8888.
# When it gets a message, it GETs the URL and parses it for the current temperature and pressure.
# Then it sends the numbers to pd on tcp port 8887
# See pd_server_awk_tester.pd
BEGIN { Host = "text.weatheroffice.ec.gc.ca" URL = "http://text.weatheroffice.ec.gc.ca/forecast/city_e.html?qc-147&unit=m" Port = 80 Method = "GET" HttpService = "/inet/tcp/0/" Host "/" Port do { RS = "\n" "/inet/tcp/8888/0/0" |& getline ORS = RS = "\r\n\r\n" print Method " " URL " HTTP/1.0" |& HttpService HttpService |& getline Header # print Header > "/dev/stderr" RS = "<dt>" while ((HttpService |& getline) > 0) { if ($0 ~ "Temperature" ) { temp = "Temperature" $2 $3 } if ($0 ~ "Pressure" ) { press = "Pressure" $2 $3 } } close(HttpService) split(temp, a, "<dd>") split (a[2], b, "&") split(press, c, "<dd>") split (c[2], d, "&") output = b[1]" "d[1] ";" print output # Use [netreceive 8899] in PD. Note that the semicolon must be present # or the message will not come out of netreceive. print output |& "/inet/tcp/0/localhost/8887/" } while (1) # well we never get here but: close("/inet/tcp/0/localhost/8887") close("/inet/tcp/8888/0/0") }