cloudy

Author Topic: Fetching Data  (Read 1026 times)

0 Members and 1 Guest are viewing this topic.

Offline Milfordwx

  • Milford, Ma Weather
  • Posts: 78
  • There's a fine line between hobby and obsession!
  • Milford, Ma USA
    • Milford, Ma Weather
Fetching Data
« on: January 01, 2007, 03:49:34 PM »
The neat thing about web pages is that they are text for the most part...

You just need to extract the info you want and present it the way you want.  If you have a php supported webserver, it wouldn't take much to extract the text out of that...

You fetch the page and parse the Bulletin which starts with an <h3> tag and end with a $$ bulletin ender.

Code: [Select]
<h3>Hazardous Weather Outlook</h3><pre>
HAZARDOUS WEATHER OUTLOOK
NATIONAL WEATHER SERVICE GRAY ME
222 AM EDT SUN AUG 20 2006

ANZ150-170-MEZ007>009-012>014-018>028-NHZ001>010-013-014-210630-
STONINGTON ME TO MERRIMACK RIVER MA OUT TO 25 NM-
WATERS FROM STONINGTON (DEER ISLE) ME TO MERRIMACK RIVER MA FROM
25 TO 40 NM-NORTHERN OXFORD-NORTHERN FRANKLIN-CENTRAL SOMERSET-
SOUTHERN OXFORD-SOUTHERN FRANKLIN-SOUTHERN SOMERSET-INTERIOR YORK-
INTERIOR CUMBERLAND-ANDROSCOGGIN-KENNEBEC-INTERIOR WALDO-
COASTAL YORK-COASTAL CUMBERLAND-SAGADAHOC-LINCOLN-KNOX-
COASTAL WALDO-NORTHERN COOS-SOUTHERN COOS-NORTHERN GRAFTON-
NORTHERN CARROLL-SOUTHERN GRAFTON-SOUTHERN CARROLL-SULLIVAN-
MERRIMACK-BELKNAP-STRAFFORD-INTERIOR ROCKINGHAM-
COASTAL ROCKINGHAM-
222 AM EDT SUN AUG 20 2006

THIS HAZARDOUS WEATHER OUTLOOK IS FOR PORTIONS OF AN...WESTERN
MAINE...CENTRAL NEW HAMPSHIRE...NORTHERN NEW HAMPSHIRE...
SOUTHEASTERN NEW HAMPSHIRE AND THE COASTAL WATERS FROM STONINGTON
ME TO THE MERRIMACK RIVER MA.

.DAY ONE...TODAY/TONIGHT

RAINFALL...HEAVY AT TIMES WILL BRING AS MUCH AS IN EXCESS OF 2
INCHES OF RAINFALL TO PORTIONS OF THE REGION TODAY. PER THE RIVER
FORECAST CENTER...IT WOULD TAKE 4 TO 6 INCHES OF RAINFALL OVER
A 12 TO 24 HOUR TIME FRAME. THUS WE MAY SEE SOME NUISANCE TYPE
PROBLEMS OF TRADITIONAL POORLY DRAINED AREAS... THOUGH NO SIGNIFICANT
FLOODING IS EXPECTED AT THIS TIME

.DAYS TWO THROUGH SEVEN...MONDAY THROUGH SATURDAY

NO HAZARDOUS WEATHER IS EXPECTED AT THIS TIME.

.SPOTTER INFORMATION STATEMENT...

SPOTTER ACTIVATION IS NOT ANTICIPATED.

$$

This is exactly what I'm looking to do and I have php support.  Could you please explain how I would fetch and parse the info I'm looking for?  I know the page I want the info from, just not sure how to go about it.

Online TNETWeather

  • Kevin Reed (KrelvinAZ)
  • Global Moderator
  • Posts: 5,865
  • Gremlins are at work...
  • Mesa, AZ
    • TNET Weather Station - Mesa AZ
Re: Fetching Data
« Reply #1 on: January 01, 2007, 05:55:58 PM »
A lot depends on what the data looks like, and if you have the ability to modify existing php code to get what you want.

If you have never used PHP before, this might be a learning curve.

First start out with what  it is you are attempting to parse, the URL etc...

All you need is Time, Aptitude and Desire ... and you can build just about anything...

Offline carterlake

  • Tom Chaplin
  • Posts: 2,273
  • Carter Lake, Iowa USA
    • Carter Lake, Iowa Weather
Re: Fetching Data
« Reply #2 on: January 01, 2007, 09:07:41 PM »
Something like?

http://www.carterlake.org/test.php

Code: [Select]
<?php

$hazardhtml 
fetchUrlWithoutHanging('http://www.crh.noaa.gov/showsigwx.php?warnzone=MAZ012&warncounty=MAC027&local_place1=Milford&product1=Hazardous+Weather+Outlook');

$NOAAHazard explode("$$",$hazardhtml);
$NOAAHazard explode("THIS HAZARDOUS WEATHER OUTLOOK",$NOAAHazard[0]);
if (strlen($NOAAHazard[1])>1) {
$NOAAHazard[1] = 'THIS HAZARDOUS WEATHER OUTLOOK' $NOAAHazard[1];
} else {
$NOAAHazard[1] = 'No Active Hazardous Weather In Our Area';
}

function 
fetchUrlWithoutHanging($url)
   {
   
// Set maximum number of seconds (can have floating-point) to wait for feed before displaying page without feed
   
$numberOfSeconds=4;    

   
// Suppress error reporting so Web site visitors are unaware if the feed fails
   
error_reporting(0);

   
// Extract resource path and domain from URL ready for fsockopen

   
$url str_replace("http://","",$url);
   
$urlComponents explode("/",$url);
   
$domain $urlComponents[0];
   
$resourcePath str_replace($domain,"",$url);

   
// Establish a connection
   
$socketConnection fsockopen($domain80$errno$errstr$numberOfSeconds);

   if (!
$socketConnection)
       {
       
// You may wish to remove the following debugging line on a live Web site
       // print("<!-- Network error: $errstr ($errno) -->");
       
}    // end if
   
else    {
       
$xml '';
       
fputs($socketConnection"GET $resourcePath HTTP/1.0\r\nHost: $domain\r\n\r\n");
   
       
// Loop until end of file
       
while (!feof($socketConnection))
           {
           
$xml .= fgets($socketConnection128);
           }    
// end while

       
fclose ($socketConnection);

       }    
// end else

   
return($xml);

   }    
// end function

?>


<pre>
<? echo $NOAAHazard[1]; ?>
</pre>


Should probably add caching but I was in a hurry... Rose Bowl is on.  :wink:

WD; Davis VP2 6153; Quickcam for Notebooks Pro; Boltek w/ Nexstorm; GRLevel3; Live NOAA radio

Offline weatheroz

  • Brendan
  • Posts: 1,133
  • Logan Village, Queensland, Australia
    • Logan Village Weather
Re: Fetching Data
« Reply #3 on: January 01, 2007, 10:12:50 PM »

Should probably add caching but I was in a hurry... Rose Bowl is on.  :wink:

What is so important about a bowl of flowers ????  :o :o :o   :lol: :lol: :lol:

Offline Weather Display

  • Posts: 65,566
Re: Fetching Data
« Reply #4 on: January 02, 2007, 02:51:52 AM »
how are you getting on with WD there milfordwx?
still overwhelming, LOL?

Offline Milfordwx

  • Milford, Ma Weather
  • Posts: 78
  • There's a fine line between hobby and obsession!
  • Milford, Ma USA
    • Milford, Ma Weather
Re: Fetching Data
« Reply #5 on: January 02, 2007, 03:22:28 AM »
Something like?

http://www.carterlake.org/test.php

Code: [Select]
<?php

$hazardhtml 
fetchUrlWithoutHanging('http://www.crh.noaa.gov/showsigwx.php?warnzone=MAZ012&warncounty=MAC027&local_place1=Milford&product1=Hazardous+Weather+Outlook');

$NOAAHazard explode("$$",$hazardhtml);
$NOAAHazard explode("THIS HAZARDOUS WEATHER OUTLOOK",$NOAAHazard[0]);
if (strlen($NOAAHazard[1])>1) {
$NOAAHazard[1] = 'THIS HAZARDOUS WEATHER OUTLOOK' $NOAAHazard[1];
} else {
$NOAAHazard[1] = 'No Active Hazardous Weather In Our Area';
}

function 
fetchUrlWithoutHanging($url)
   {
   
// Set maximum number of seconds (can have floating-point) to wait for feed before displaying page without feed
   
$numberOfSeconds=4;    

   
// Suppress error reporting so Web site visitors are unaware if the feed fails
   
error_reporting(0);

   
// Extract resource path and domain from URL ready for fsockopen

   
$url str_replace("http://","",$url);
   
$urlComponents explode("/",$url);
   
$domain $urlComponents[0];
   
$resourcePath str_replace($domain,"",$url);

   
// Establish a connection
   
$socketConnection fsockopen($domain80$errno$errstr$numberOfSeconds);

   if (!
$socketConnection)
       {
       
// You may wish to remove the following debugging line on a live Web site
       // print("<!-- Network error: $errstr ($errno) -->");
       
}    // end if
   
else    {
       
$xml '';
       
fputs($socketConnection"GET $resourcePath HTTP/1.0\r\nHost: $domain\r\n\r\n");
   
       
// Loop until end of file
       
while (!feof($socketConnection))
           {
           
$xml .= fgets($socketConnection128);
           }    
// end while

       
fclose ($socketConnection);

       }    
// end else

   
return($xml);

   }    
// end function

?>


<pre>
<? echo $NOAAHazard[1]; ?>
</pre>


Should probably add caching but I was in a hurry... Rose Bowl is on.  :wink:

Thats exactly what I'm looking for!  I will play with it a bit tomorrow, thanks!

Brian, I'm doing well with WD, but I'm working my way through things.  I am still having some issues with the import of the old data that I posted a few days ago, but the holidays have slowed things a bit for now.

Paul

Offline Milfordwx

  • Milford, Ma Weather
  • Posts: 78
  • There's a fine line between hobby and obsession!
  • Milford, Ma USA
    • Milford, Ma Weather
Re: Fetching Data
« Reply #6 on: January 02, 2007, 02:41:22 PM »
I have the php running fine as a page itself (http://www.pdfamily.com/weather/hazard.php) and I'm wondering if there is a way to get it to run on an html page, preferably within a table?  What I'm looking to do is have the hazard outlook appear in the table I have the watches and warnings on my main page (http://www.pdfamily.com/weather/index.html), just in a different cell.
I'm new to php and appreciate any help.

Thanks!

Offline Milfordwx

  • Milford, Ma Weather
  • Posts: 78
  • There's a fine line between hobby and obsession!
  • Milford, Ma USA
    • Milford, Ma Weather
Re: Fetching Data
« Reply #7 on: January 21, 2007, 03:11:44 AM »
Well, after some battles with my host I am able to run php within an html document and now I would like to run 2 or 3 php scripts on the same page, is this possible?  I am looking to run a watch, short tern forecast and hazard outlook script all on the same page, but, when I try to, only the first one runs properly and the other 2 come back as false...?

 

cumulus