WEATHER_MODULE: Locks up or takes forever

Have you created a module that you would like to submit to BudaBot? Post it here, and it might make it into the next release.
Lucier
The Team
Posts: 57
Joined: Tue Nov 17, 2009 11:27 pm

WEATHER_MODULE: Locks up or takes forever

Postby Lucier » Mon Nov 11, 2013 8:40 am

Looks like they changed something with the their server. Before, the connection closed after the file was sent. But now it says open. (This means feof($server) stayed true and got somewhat stuck in a loop.) Hmm.. maybe I needed to change 'Connection: Keep-Alive'? Looks like I should read up on the RFC on HTTP some more! heh

After doing some tests, I've noticed that there is now 'Content-Length' which I do not remember seeing before in the header the server sends back. So we'll use that to know when the server is finished.

NOTE: I'm on a completely different version of Budabot then everyone else, so I haven't done any testing with the recent version. Use at your own risk! Make a backup of the file before doing any edits!

./modules/WEATHER_MODULE/WeatherController.class.php

Original code:

Code: Select all

                fputs($server, $request);
                while (!feof($server)) {
                        $stream .= fread($server, 8192);
                }
                fclose($server);

                $stream = substr($stream, strpos( $stream, "\r\n\r\n") +4); //remove packet info         


Updated Code:

Code: Select all

                // Send header to server
                fputs($server, $request); $stream = "";

                // Download from server, as soon as we see \r\n\r\n, stop. (Only want header)
                while(!feof($server) && strpos($stream,"\r\n\r\n") === false) {
                    $stream .= fread($server, 8192);
                }

                // Save header in $header, and remove header from $stream
                $s = explode("\r\n\r\n", $stream, 2);
                $header = $s[0]; $stream = $s[1];

                // Find 'Content-Length' in the header so we know when it's finished
                if (preg_match("/Content-Length: ([0-9]+)/",$header, $arr)) {
                    $length = $arr[1];
                } else { // could not find Content-Length
                    fclose($server); return -3;
                } 
        
                
// finish downloading the page
                while(!feof($server) && (strlen($stream) < $length)) {
                    $stream .= fread($server, 8192);
                }
        
                
// close connection
                fclose($server); 
Captank
Member
Posts: 77
Joined: Sun Sep 09, 2012 6:36 pm
antispam: Rimor
Location: irc.funcom.com #budabot

Re: WEATHER_MODULE: Locks up or takes forever

Postby Captank » Mon Nov 11, 2013 8:35 pm

<alerts>
<termsofservice link="http://www.wunderground.com/weather/api/d/terms.html">This feed will be deprecated. Please switch to http://www.wunderground.com/weather/api/</termsofservice><alert count="0"></alert>
</alerts>


seems a bit more difficult to fix :s
Tyrence
The Team
Posts: 1876
Joined: Sat Jan 09, 2010 1:32 am

Re: WEATHER_MODULE: Locks up or takes forever

Postby Tyrence » Tue Nov 12, 2013 12:12 am

You could also use http://us2.php.net/file_get_contents. I used this in all the new code whenever I needed to make a call to a website and it always seems to do the right thing:

Code: Select all

private function getweatherdata ($host, $port, $url) {
   return file_get_contents("http://" + $host + ":" + $port + $url);
}


In fact, if you do use file_get_contents() there is no need for getweatherdata(). Just replace calls to getweatherdata() with calls to file_get_contents().

Also, if you ever need to parse HTTP headers you can use this: http://php.net/manual/en/function.http- ... eaders.php
"Those who expect to reap the blessings of freedom, must, like men, undergo the fatigues of supporting it." — Thomas Paine
"Nearly all men stand adversity, but if you want to test a man's character, give him power." — Abraham Lincoln
Budabot Releases and Downloads: https://github.com/Budabot/Budabot/releases
Lucier
The Team
Posts: 57
Joined: Tue Nov 17, 2009 11:27 pm

Re: WEATHER_MODULE: Locks up or takes forever

Postby Lucier » Tue Nov 12, 2013 7:54 pm

Captank wrote:
<alerts>
<termsofservice link="http://www.wunderground.com/weather/api/d/terms.html">This feed will be deprecated. Please switch to http://www.wunderground.com/weather/api/</termsofservice><alert count="0"></alert>
</alerts>


seems a bit more difficult to fix :s
Yes, I saw those messages. But it still works for me at this time. Hope they leave it running for a while! I don't want to deal with signing up for the api. :twisted:

Tyrence wrote:You could also use http://us2.php.net/file_get_contents. I used this in all the new code whenever I needed to make a call to a website and it always seems to do the right thing:
Sure, file_get_contents would probably work here, but it doesn't work all the time. In some cases, when there is no http header, or the header is corrupt, get_file_contents would error. (For example, it can't read shoutCAST data.) My personal preference is to only use file_get_contents for reading files locally. I don't remember what kind of User-Agent is sent. (It's either empty or something non web browser like.) While this server probably doesn't mind a script is contacting them, I'd like web servers to think I'm just another web browser!

Tyrence wrote:Also, if you ever need to parse HTTP headers you can use this: http://php.net/manual/en/function.http- ... eaders.php
Interesting, forgot about this function. Not sure if I have it installed. lol (PECL pecl_http >= 0.12.0) In this instance, preg_match does it all for me anyways. (Any chance you have to practice with preg_match, take it!) :geek:

-Lucier
Tyrence
The Team
Posts: 1876
Joined: Sat Jan 09, 2010 1:32 am

Re: WEATHER_MODULE: Locks up or takes forever

Postby Tyrence » Wed Nov 13, 2013 8:41 pm

Lucier wrote:Sure, file_get_contents would probably work here, but it doesn't work all the time. In some cases, when there is no http header, or the header is corrupt, get_file_contents would error. (For example, it can't read shoutCAST data.) My personal preference is to only use file_get_contents for reading files locally. I don't remember what kind of User-Agent is sent. (It's either empty or something non web browser like.) While this server probably doesn't mind a script is contacting them, I'd like web servers to think I'm just another web browser!r

What I meant was that file_get_contents() works for all HTTP requests. If you have a different protocol then of course you will have to use something that understands that.

If you want to send a custom value for Host, you can do that as well (taken from the file_get_contents() example):

Code: Select all

$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n",
    'host'=>"User-Agent: Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)"
  )
);

$context = stream_context_create($opts);

$page = file_get_contents('http://www.example.com/', false, $context);
"Those who expect to reap the blessings of freedom, must, like men, undergo the fatigues of supporting it." — Thomas Paine
"Nearly all men stand adversity, but if you want to test a man's character, give him power." — Abraham Lincoln
Budabot Releases and Downloads: https://github.com/Budabot/Budabot/releases
User avatar
Shelly
Member
Posts: 80
Joined: Wed Oct 05, 2011 1:51 am
antispam: Rimor
Location: Lost in Time
Contact:

Re: WEATHER_MODULE: Locks up or takes forever

Postby Shelly » Thu Nov 14, 2013 1:33 pm

I tried an update from git for this module on Budanet. It is working now after a bot restart.

Budanet is at
Version: 3.1_RC1

GIT UPDATE:
git pull origin master 2>&1

From git://github.com/Budabot/Budabot
* branch master -> FETCH_HEAD
Updating ac3fe8f..8b26029
Fast-forward
core/HELP/about.txt | 4 +-
core/PROFILE/ProfileController.class.php | 2 +-
core/SYSTEM/SystemController.class.php | 1 +
modules/ALIEN_MODULE/leprocs.sql | 10 +-
modules/GUIDE_MODULE/guides/doja.txt | 7 +-
modules/WEATHER_MODULE/WeatherController.class.php | 95 +++-----------------
6 files changed, 24 insertions(+), 95 deletions(-)


Shelly
Your type personality is: SAEK
Breakdown: Socializer 73.33%, Achiever 60.00%, Explorer 60.00%, Killer 6.67%

http://en.wikipedia.org/wiki/Bartle_Test

Return to “User Modules”

Who is online

Users browsing this forum: No registered users and 1 guest