mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Oct 2004
Posts: 8,330
Riamus2 Offline OP
Hoopy frood
OP Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
Strange to be having this problem, but I haven't scripted much of anything in years. It's probably a simple answer, but I'm out of ideas. smile

I'm trying to update an old weather script to use Open Weather Map for it's data. I connect fine to the server using sockets. However, when I try looking at the raw data, I run into a problem. If I get the data in HTML format, I receive all of the data as expected, but if I choose XML format, it gives me only the header line and nothing else. If I choose JSON format, I don't get anything. Here is the raw XML output:
Code
HTTP/1.1 200 OK
Server: openresty
Date: Fri, 14 Feb 2020 02:36:44 GMT
Content-Type: application/xml; charset=utf-8
Content-Length: 871
Connection: close
X-Cache-Key: /data/2.5/weather?mode=xml&q=01613,us&units=imperial
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST

<?xml version="1.0" encoding="UTF-8"?>


That last line is the header line for the XML. But I get nothing else. Here's the correct output if I view this same GET through the browser:
Code
<?xml version="1.0" encoding="UTF-8"?>
<current><city id="4956184" name="Worcester"><coord lon="-71.83" lat="42.28"></coord><country>US</country><timezone>-18000</timezone><sun rise="2020-02-13T11:47:14" set="2020-02-13T22:16:03"></sun></city><temperature value="34.97" min="33.01" max="37.4" unit="fahrenheit"></temperature><feels_like value="23.47" unit="fahrenheit"></feels_like><humidity value="86" unit="%"></humidity><pressure value="1006" unit="hPa"></pressure><wind><speed value="13.87" unit="mph" name="Moderate breeze"></speed><gusts></gusts><direction value="280" code="W" name="West"></direction></wind><clouds value="90" name="overcast clouds"></clouds><visibility value="16093"></visibility><precipitation value="0.25" mode="rain" unit="1h"></precipitation><weather number="500" value="light rain" icon="10n"></weather><lastupdate value="2020-02-14T02:27:11"></lastupdate></current>


As I mentioned, if I request the weather with HTML mode instead of XML, I get all of the data, so the socket is working properly. But for some reason, the XML isn't reading past the header. Any ideas? I know I could use HTML, but I want to know why it isn't working for XML or JSON modes.

Sockread code is simply this for testing:
Code
on *:sockread:weather: {
  if ($sockerr) {
    echo -a Error.
    halt
  }
  else {
    var %temptext
    sockread %temptext
    echo -a %temptext
  }
}


Invision Support
#Invision on irc.irchighway.net
Joined: Jan 2004
Posts: 1,361
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,361
It might be that the data does not end with a newline, you must use /sockread -f %temptext in that case.

Joined: Oct 2004
Posts: 8,330
Riamus2 Offline OP
Hoopy frood
OP Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
I did actually try -f and -fn, but with no luck. frown

Here's the rest of the socket info if you want to give it a test:

Code
alias weathersock {
  set %query $1-
  if ($sock(weather)) { sockclose weather }
  sockopen weather api.openweathermap.org 80
}

on *:sockopen:weather: {
  sockwrite -n $sockname GET /data/2.5/weather?q= $+ %query $+ &appid=fac434f677376d8589d3db1a1831a201&units= $+ %forecast.type $+ &mode=html HTTP/1.0
  sockwrite -n $sockname Host: api.openweathermap.org
  sockwrite -n $sockname $crlf $+ $crlf
}


I'll replace the api key in a day or so, but for now that key can be used to test it. %forecast.type would be metric or imperial. The mode at the end is either XML, HTML, or JSON. %query would be, for example: Paris,FR

Last edited by Riamus2; 14/02/20 03:48 AM.

Invision Support
#Invision on irc.irchighway.net
Joined: Jan 2004
Posts: 1,361
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,361
When reading into a %variable the sockread event is only retriggered if the receive queue contains a $crlf

You should use a while loop to fully read the buffer:

Code
on *:sockread:weather: {
  if ($sockerr) {
    echo -a Error.
    halt
  }
  else {
    var %temptext
    sockread -f %temptext
    while ($sockbr) {
      if (%temptext) { echo -ag %temptext }
      sockread -f %temptext
    }
  }
}

Joined: Oct 2004
Posts: 8,330
Riamus2 Offline OP
Hoopy frood
OP Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
You know... I tried doing the loop, but didn't try it with -f at the same time. Duh! Definitely rusty. smile

Thanks!


Invision Support
#Invision on irc.irchighway.net

Link Copied to Clipboard