Author Topic: Curly's Wx Story script  (Read 22613 times)

0 Members and 2 Guests are viewing this topic.

Offline jgillett

  • Forecaster
  • *****
  • Posts: 1187
  • Boltek, Win7 Pro, ToA
    • TiggrWeather Phoenix
Re: Curly's Wx Story script
« Reply #125 on: March 02, 2018, 11:10:21 PM »
However, this brings up a question regarding the map of the US which shows the wfo's who participate in the weather story program. Who maintains this image file and how does it get updated when wfo's come on-line or drop-off the program?
rrrick8 made the recent changes to it.
« Last Edit: March 03, 2018, 05:49:23 PM by jgillett »
John
W7JKG

Offline mkutche

  • Forecaster
  • *****
  • Posts: 1041
    • GosportWx.com
Re: Curly's Wx Story script
« Reply #126 on: March 03, 2018, 07:56:48 AM »
It seems that the NWS used to accept query strings without a "?" such as http://www.weather.gov/images/arx/wxstory/Tab2FileL.png e73c9f3d342158b6aa283cb30e80054 but no longer does.  Perhaps Ken and his regex wizardry will be able to come up with a better fix, but this seems to be doing it for me:
replace
Code: [Select]
function GetDataByCurl($url)    {
  global $noted;                                                          // set global

with
function GetDataByCurl($url)    {
  $url = str_replace("png ", "png?", $url);    // added 3/1/18
  global $noted;                                                          // set global
and
Code: [Select]

function getWSBystream($url) {
  global $noted;

with
function getWSBystream($url) {
  $url = str_replace("png ", "png?", $url);        // added 3/1/18
  global $noted;

- Jim

Thanks Jim works great on my end!
Mike K.
Davis Vantage Vue 6250 - CumulusMX (3.21.1-b3205)
Gosport, Indiana
Gosportwx.com twitter.com/GosportINWX
-----------------------------------------------------------

Offline gwwilk

  • Southeast Lincoln Weather
  • Forecaster
  • *****
  • Posts: 2578
    • SouthEast Lincoln, NE Weather
Re: Curly's Wx Story script
« Reply #127 on: March 03, 2018, 10:24:26 AM »
Thanks, Jim! \:D/ =D> \:D/ Works great!

Jerry  - I notice the map does not change when you select the various green dots. Once I select another location, it always shows the Pocatello, ID section of the map. The wx story and header do change with each different selection. Not sure if it's me or not. I've tried it on Chrome and Firefox and get similar results.
To buttress my idea that file permissions are the issue with the map not changing to a newly selected area, my alternate self-hosted test-site has no such problems.  I have granular control of file permissions there, whereas on my main GoDaddy-hosted site I don't, and the permissions keep getting hosed when the files are re-written.  All modified files in the GoDaddy /wscache revert to '604' from '707'.  Anybody know how to keep this from happening?
« Last Edit: March 03, 2018, 10:28:49 AM by gwwilk »
Regards, Jerry Wilkins
gwwilk@gmail.com

Offline jmcmurry

  • Jim McMurry
  • Forecaster
  • *****
  • Posts: 528
  • Davis Vantage Pro 2 Plus Wireless.
    • Juneau County Weather
Re: Curly's Wx Story script
« Reply #128 on: March 03, 2018, 02:04:37 PM »
Today I found that there are sites using .gif and .jpg files so we need another tweak.  Now we should have

Code: [Select]
function GetDataByCurl($url)    {
  $url = str_replace("png ", "png?", $url);    // added 3/1/18
  $url = str_replace("jpg ", "jpg?", $url);    // added 3/3/18 Shreveport uses .jpg
  $url = str_replace("gif ", "gif?", $url);    // added 3/3/18 Fort Worth use .gif   
  global $noted;                                                          // set global

and

Code: [Select]
function getWSBystream($url) {
  $url = str_replace("png ", "png?", $url);    // added 3/1/18
  $url = str_replace("jpg ", "jpg?", $url);    // added 3/3/18 Shreveport uses .jpg
  $url = str_replace("gif ", "gif?", $url);    // added 3/3/18 Fort Worth use .gif   
  global $noted;

- Jim

Forum Search and Google Can be Your Best Friends

Offline tmabell

  • Forecaster
  • *****
  • Posts: 394
    • Mishawaka Weather
Re: Curly's Wx Story script
« Reply #129 on: March 03, 2018, 05:09:26 PM »
Thanks Jim!

Offline rrrick8

  • Senior Contributor
  • ****
  • Posts: 224
    • Vermilion weather
Re: Curly's Wx Story script
« Reply #130 on: March 03, 2018, 05:16:39 PM »
Sorry I'm late to the party. Been traveling this past week and just got back last night.
Caught up on the developments and applied Jim's fixes and resolved that problem but I see a new problem, at least for me..
The Southern Region has switched their images url subfolders from /wxstory/  to /graphicast/  thus their images are not working.

I see the line that is probably the reason is my line#297
Code: [Select]
$wxsimage = 'https://www.weather.gov/images/'.$wswfo.'/wxstory/'.$imName.'.png'; //  set image path
It probably needs an array to add the
Code: [Select]
$wxsimage = 'https://www.weather.gov/images/'.$wswfo.'/graphicast/'.$imName.'.png'; //  set image path  option but to be honest, arrays are not my forte.
« Last Edit: March 03, 2018, 05:20:12 PM by rrrick8 »
Severe Weather Manager-Vermilion County EMA
CWOP-CW9931 KILDANVI5

Offline tmabell

  • Forecaster
  • *****
  • Posts: 394
    • Mishawaka Weather
Re: Curly's Wx Story script
« Reply #131 on: March 03, 2018, 05:23:53 PM »
rrrick8,

I have not had any problems.  My images are showing for all Southern Region WFO's without your most recent edit.  I should mention that I have disabled caching.  Might this be the reason?

Offline rrrick8

  • Senior Contributor
  • ****
  • Posts: 224
    • Vermilion weather
Re: Curly's Wx Story script
« Reply #132 on: March 04, 2018, 07:56:21 PM »
rrrick8,

I have not had any problems.  My images are showing for all Southern Region WFO's without your most recent edit.  I should mention that I have disabled caching.  Might this be the reason?

Well, everything works for me except the Southern Region WFO's. I get the wording, just no images. I'm using V2.24 which I don't know if that was Curly's last or not. I was a beta tester for him and some of my scripts are from that endeavor and may not be his released version.
I have absolutely nothing in the script that mentions graphicast as a possible folder.
Severe Weather Manager-Vermilion County EMA
CWOP-CW9931 KILDANVI5

Offline tmabell

  • Forecaster
  • *****
  • Posts: 394
    • Mishawaka Weather
Re: Curly's Wx Story script
« Reply #133 on: March 04, 2018, 08:02:15 PM »
This is what I thought was his latest (2.23) but includes the recent fixes posted earlier.

Offline chief-david

  • Educational Weather
  • Forecaster
  • *****
  • Posts: 2845
  • Space Academy for Educators
    • Benilde-St. Margaret's Weather
Re: Curly's Wx Story script
« Reply #134 on: July 07, 2020, 10:52:09 PM »
Sorry to resurrect this-but I wanted to make a change.

92merc sent me the package and I have done what I thought was correct with all the instructions-but cannot get it to work.

To isolate the issue-I created a page with just the weather story. As you can see it just cuts off and stops.  https://rms-weather.rdale.org/Weatherstory.php

I would not be surprised if it is a code issue on my end somehow. We all know my site is put together with duct tape and chicken wire.

Any help is appreciated. I try as best as a can.



You can't phase me-I teach Middle School.
It's not you-It's WU.

Offline chief-david

  • Educational Weather
  • Forecaster
  • *****
  • Posts: 2845
  • Space Academy for Educators
    • Benilde-St. Margaret's Weather
Re: Curly's Wx Story script
« Reply #135 on: July 17, 2020, 12:11:09 PM »
bump.
Need help. Would like it to update itself instead of me doing it via Teamviewer.



You can't phase me-I teach Middle School.
It's not you-It's WU.

Offline tmabell

  • Forecaster
  • *****
  • Posts: 394
    • Mishawaka Weather
Re: Curly's Wx Story script
« Reply #136 on: July 17, 2020, 01:59:25 PM »
Any chance you could post your code?

Offline chief-david

  • Educational Weather
  • Forecaster
  • *****
  • Posts: 2845
  • Space Academy for Educators
    • Benilde-St. Margaret's Weather
Re: Curly's Wx Story script
« Reply #137 on: July 17, 2020, 02:29:03 PM »
I can only do part of the code at a time. It will not let me attach php

Quote
$NWSregion      = 'mpx';               // Enter your local NWS office three letter code
$useallwfos     = true;                // true=allow all WFO locations to be selected    false=display only the default WFO
$wstitle        = 'Weather Story';     // page title
$ourTZ          = 'America/Chicago';  // Time Zone     http://www.php.net/manual/en/timezones.america.php
$ws_cachetime   = '30';                // time in minutes to hold cache files before updating fresh data  ( 0 is not recommended)
$tablewdth      = '700';               // displayed image width in pixels inside the table

//  MAP & DATA SETTINGS
$useWFOmap      = false;               // true=use map to select WFO     false=use drop down menu to select the WFO
$ws_mapfolder   = '.images/';         // main map image file FOLDER
$ws_cachefolder = './wscache/';        // cache file FOLDER
$wfomaps        = './wsimages/';       // folder containing individual WFO coverage maps
$mapname        = 'WS_map';            // main map name - without extension
$ift            = '.png';              // map image file extesion
$autocacheclean = true;                // true=auto delete cache files older than 1 day   false=keep cache files

// Download method
// IF downloading seems slow, change setting
$useCURL        = false;               // true=get data by cURL method  false=stream data

###  END OF SETTINGS  ############################################################################################################



You can't phase me-I teach Middle School.
It's not you-It's WU.

Offline chief-david

  • Educational Weather
  • Forecaster
  • *****
  • Posts: 2845
  • Space Academy for Educators
    • Benilde-St. Margaret's Weather
Re: Curly's Wx Story script
« Reply #138 on: July 17, 2020, 02:30:34 PM »
Quote
// set variables
$updateCache = true;                   // SET update cache file(s)
$updateTextTime = $ws_cachetime * 60;  // change cache time minutes to seconds
$uts       = date("U");                // current unix time stamp
$goodWFO   = true;
$altStory  = array();
$cft       = '';
$hdline    = '';
$hdlnr     = array();
$imgMn     = array();
$imgName   = '';
$imName    = '';
$ifn       = '';
$mainStory = array();
$nfo       = '';
$noted     = '';
$wsdata    = '';
$wxsimage  = '';

// HTTPS sites
$usehttps = array('grr','iwx','lmk');

// old CRH or non working WFO codes
$nonWorkingCodes = array('akq','aly','bgm','boi','box','btv',
                         'buf','byz','cae','car','chs','cle',
                         'ctp','ggw','gsp','gyx','hnx','ilm',
                         'iln','lox','lwx','mhx','mlb','okx',
                         'pbz','phi','pih','pqr','psr','rah',
                         'rlx','rnk','sew','slc','tae','tfx',
                         'vef'
                         );

// home page WS WFO's - not in  /weatherstory
$hpwxs2 = array('abq','ama','bmx','bro','crp','eka','epz','ewx','ffc',
                'fgz','fwd','hgx','hun','jan','jax','key','lch','lix',
                'lkn','lub','lzk','maf','meg','mfl','mfr','mob','mrx',
                'mso','mtr','ohx','otx','oun','pdt','rev','sgx','shv',
                'sjt','sju','sto','tbw','tsa','twc'
             );

$hpwxs = array_merge($nonWorkingCodes, $hpwxs2);

// WFO code and location array as of 4/27/2017
$fo = array(
    'abq' => 'Albuquerque',
    'abr' => 'Aberdeen',
    'afc' => 'Anchorage',
    'afg' => 'Fairbanks',
    'ajk' => 'Juneau',
    'akq' => 'Wakefield',
    'aly' => 'Albany',
    'ama' => 'Amarillo',
    'apx' => 'Gaylord',
    'arx' => 'La Crosse',
    'bgm' => 'Binghamton',
    'bis' => 'Bismarck',
    'bmx' => 'Birmingham',
    'boi' => 'Boise',
    'bou' => 'Denver/Boulder',
    'box' => 'Boston',
    'bro' => 'Brownsville',
    'btv' => 'Burlington',
    'buf' => 'Buffalo',
    'byz' => 'Billings',
    'cae' => 'Columbia',
    'car' => 'Caribou',
    'chs' => 'Charleston',
    'cle' => 'Cleveland',
    'crp' => 'Corpus Christi',
    'ctp' => 'State College',
    'cys' => 'Cheyenne',
    'ddc' => 'Dodge City',
    'dlh' => 'Duluth',
    'dmx' => 'Des Moines',
    'dtx' => 'Detroit/Pontiac',
    'dvn' => 'Quad Cities',
    'eax' => 'Kansas City/Pleasant Hill',
    'eka' => 'Eureka',
    'epz' => 'El Paso Area',
    'ewx' => 'Austin/San Antonio',
    'ffc' => 'Peachtree City',
    'fgf' => 'Grand Forks',
    'fgz' => 'Flagstaff',
    'fsd' => 'Sioux Falls',
    'fwd' => 'Fort Worth',
    'ggw' => 'Glasgow',
    'gid' => 'Hastings',
    'gjt' => 'Grand Junction',
    'gld' => 'Goodland',
    'grb' => 'Green Bay',
    'grr' => 'Grand Rapids',
    'gsp' => 'Greenville-Spartanburg',
    'gyx' => 'Gray/Portland',
    'hfo' => 'Honolulu',
    'hgx' => 'Houston/Galveston',
    'hnx' => 'San Joaquin Valley/Hanford',
    'hun' => 'Huntsville',
    'ict' => 'Wichita',
    'ilm' => 'Wilmington',
    'iln' => 'Wilmington',
    'ilx' => 'Central Illinois',
    'ind' => 'Indianapolis',
    'iwx' => 'Northern Indiana',
    'jan' => 'Jackson',
    'jax' => 'Jacksonville',
    'jkl' => 'Jackson',
    'key' => 'Key West',
    'lbf' => 'North Platte',
    'lch' => 'Lake Charles',
    'lix' => 'New Orleans/Baton Rouge',
    'lkn' => 'Elko',
    'lmk' => 'Louisville',
    'lot' => 'Chicago',
    'lox' => 'Los Angeles/Oxnard',
    'lsx' => 'St. Louis',
    'lub' => 'Lubbock',
    'lwx' => 'Baltimore/Washington',
    'lzk' => 'Little Rock',
    'maf' => 'Midland/Odessa',
    'meg' => 'Memphis',
    'mfl' => 'Miami-South Florida',
    'mfr' => 'Medford',
    'mhx' => 'Newport/Morehead City',
    'mkx' => 'Milwaukee/Sullivan',
    'mlb' => 'Melbourne',
    'mob' => 'Mobile/Pensacola',
    'mpx' => 'Twin Cities',
    'mqt' => 'Marquette',
    'mrx' => 'Morristown',
    'mso' => 'Missoula',
    'mtr' => 'San Francisco/Monterey Bay Area',
    'oax' => 'Omaha/Valley',
    'ohx' => 'Nashville',
    'okx' => 'New York City/Upton',
    'otx' => 'Spokane',
    'oun' => 'Norman',
    'pah' => 'Paducah',
    'pbz' => 'Pittsburgh',
    'pdt' => 'Pendleton',
    'phi' => 'Philadelphia/Mount Holly',
    'pih' => 'Pocatello',
    'pqr' => 'Portland',
    'psr' => 'Phoenix',
    'pub' => 'Pueblo',
    'rah' => 'Raleigh',
    'rev' => 'Reno',
    'riw' => 'Riverton',
    'rlx' => 'Charleston',
    'rnk' => 'Blacksburg',
    'sew' => 'Seattle',
    'sgf' => 'Springfield',
    'sgx' => 'San Diego',
    'shv' => 'Shreveport',
    'sjt' => 'San Angelo',
    'sju' => 'San Juan',
    'slc' => 'Salt Lake City',
    'sto' => 'Sacramento',
    'tae' => 'Tallahassee',
    'tbw' => 'Tampa Bay',
    'tfx' => 'Great Falls',
    'top' => 'Topeka',
    'tsa' => 'Tulsa',
    'twc' => 'Tucson',
    'unr' => 'Rapid City',
    'vef' => 'Las Vegas'
   );

// set download method from main settings
($useCURL == true) ? $dldata = 'GetDataByCurl' : $dldata = 'getWSBystream';



You can't phase me-I teach Middle School.
It's not you-It's WU.

Offline chief-david

  • Educational Weather
  • Forecaster
  • *****
  • Posts: 2845
  • Space Academy for Educators
    • Benilde-St. Margaret's Weather
Re: Curly's Wx Story script
« Reply #139 on: July 17, 2020, 02:32:43 PM »
// set download method from main settings
($useCURL == true) ? $dldata = 'GetDataByCurl' : $dldata = 'getWSBystream';

// set the time zone
date_default_timezone_set("$ourTZ");

// check for map in folder
if($useWFOmap == true and !file_exists($ws_mapfolder.$mapname.$ift)){   // IF use map and the map is not in the folder
  $useWFOmap = false;                                                   //  turn off map and use drop down menu
  $noted .= "<!-- The required map is missing -->\n";                   //  display notice
}

// template map - ./folder/mapname.extension
$mainmap = $ws_mapfolder.$mapname.$ift;

// copy default WFO code
$wswfo = $NWSregion;

//  Set the DEFAULT weather story & image
(in_array($wswfo, $hpwxs)) ? $wxsurl =  'http://www.weather.gov/'.$wswfo.'/' : $wxsurl = 'http://www.weather.gov/'.$wswfo.'/weatherstory';

//  IF the WFO is using https
if(in_array($wswfo, $usehttps)){$wxsurl = preg_replace('/https:/', 'https:', $wxsurl);}  //   change http to https

// auto clean cache file
$defcachedata = $ws_cachefolder.'wsdata_'.$NWSregion.'.php';                   // default WFO data file
if(is_file($defcachedata) and filemtime($defcachedata) < strtotime("today")){  // IF default WFO cache file is found AND the filetime is older than today
  if($autocacheclean == true and $ws_cachefolder != './cache/'){               //  IF auto cache clean is true AND not in a default cache folder
    $cfs = array();                                                            //   set array
    $i = 0;                                                                    //   set counter
    foreach (glob($ws_cachefolder."ws*.*") as $cachedfile) {                   //   FOR EACH file in the cache folder
      if(filemtime($cachedfile) < strtotime("today")){                         //    IF the file time is older than today - before 12:00am
        $cfs[] = filesize($cachedfile);                                        //     set the cache file size
        unlink($cachedfile);                                                   //     delete the file
        $i++;                                                                  //     increment counter
      }
    }
    $dfs = array_sum($cfs);                                                    //   get the deleted cache file size sum
   $fb = getFileSize($dfs);                                                   //   set to mbytes or gbytes
    $noted .= "<!-- Cache files deleted: $i - $fb -->\n";                      //   display notice
  }
}

//   print_r($wxsurl);  echo "<br />\n";

// get submitted location
if(isset($_GET['i']) and strlen($_GET['i']) == "3"){                         // IF a WFO was selected
  $newwswfo = $_GET['i'];                                                    //  get the location
  $wswfo = htmlspecialchars(strip_tags($newwswfo));                          //  clean the location
  $wxsurl =  'http://www.weather.gov/'.$wswfo.'/weatherstory';               //  set the link
  if(in_array($wswfo, $hpwxs)){                                              //  IF the WS is on the WFO home page
    $wxsurl =  'http://www.weather.gov/'.$wswfo.'/';                         //   change the path
  }
  if(in_array($wswfo, $usehttps)){                                           //  IF the WFO is using https
    $wxsurl = preg_replace('/http:/', 'https:', $wxsurl);                    //   change http to https
  }
  // MAP CREATION
  if($useWFOmap){                                                            // IF using the map feature
    $mm = imagecreatefrompng($mainmap);                                      //  creat map from map template
    $orig_size = getimagesize($mainmap);                                    //  get original map size
    $red = imagecolorallocate($mm, 255, 0, 0);                                //  designate red because of county color
    imagecolortransparent($mm, $red);                                        //  make transparent color
    $im = imagecreatetruecolor($orig_size[0], $orig_size[1]);                //  create blank image the size of the map
    $id = imagecreatefrompng($wfomaps.$wswfo.'.png');                        //  create image from county alert png file
    imagecopymerge($mm, $id, 0, 0, 0, 0, $orig_size[0],$orig_size[1] ,100);  //  merge the county image to the map image
    imagedestroy($id);                                                       //  destroy the original county image
    imagepng($mm, $ws_cachefolder.$mapname.$ift,2);                          //  save the image
    imagedestroy($im);                                                       //  destroy the transparent image
    $swfoi = $ws_cachefolder.$mapname.$ift;                                  //  set selected WFO image
  }
}
else{                                                                          // OR ELSE if no selection, create default location map
  if($useWFOmap){
    if(!file_exists($ws_cachefolder.$mapname.$NWSregion.'.png')) {             // IF the WFO boundary image is found
      $mm = imagecreatefrompng($mainmap);                                      //  create image from US map file
      $orig_size = getimagesize($mainmap);                                      //  get original map size
      $red = imagecolorallocate($mm, 255, 0, 0);                              //  designate red because of county color
      imagecolortransparent($mm, $red);                                          //  make transparent color
      $im = imagecreatetruecolor($orig_size[0], $orig_size[1]);                //  create blank image the size of the map
      $id = imagecreatefrompng($wfomaps.$wswfo.'.png');                        //  create image from county alert png file
      imagecopymerge($mm, $id, 0, 0, 0, 0, $orig_size[0],$orig_size[1] ,100);  //  merge the county image to the map image
      imagedestroy($id);                                                       //  destroy the original county image
      imagepng($mm, $ws_cachefolder.$mapname.$NWSregion.'.png',2);             //  save the image
      imagedestroy($im);                                                       //  destroy the transparent image
    }
    $swfoi = $ws_cachefolder.$mapname.$NWSregion.'.png';                       //  set default WFO image
  }
}

// get submitted image
if(isset($_GET['ifn'])){                                                          // IF a headline was selected
  $imName = htmlspecialchars(strip_tags($_GET['ifn']));                           //  clean up the name
  $wxsimage = 'http://www.weather.gov/images/'.$wswfo.'/wxstory/'.$imName.'.png'; //  set image path
  $noted .= "<!-- $imName selected -->\n";                                        //   display notice
}

// create unique cache filenames
$wsimg = $ws_cachefolder.'wsimg_'.$wswfo.'.png';           // Weather Story image
$ws_cachefile = $ws_cachefolder.'wsdata_'.$wswfo.'.php';   // Weather Story text

// update cache file policy
if(file_exists($ws_cachefile)) {                   // IF the data cache file exists
  $cft = filemtime($ws_cachefile);                 //   get cache file last modified time
  $cfAge = $uts - $cft;                            //   get cache file age in seconds
  if($cfAge < $updateTextTime) {                   //   IF cache file has not expired
    $updateCache = false;                          //     don't update
    $noted .= "<!-- Cached data displayed -->\n";  //     display notice
  }
  if(!empty($_GET['mu'])) {                        //   IF you want to manually update the report   webpagefilename.php?mu=1
    $updateCache = true;                           //     set update cache
    $noted .= "<!-- manually updated -->\n";       //     display notice
  }
}

// IF the WFO code is in the above array, set the location
if(array_key_exists($wswfo, $fo)){
  $nfo = $fo[$wswfo];
}

if($updateCache) {                                                                     // IF the cache file needs updating
  if(!$html = $dldata($wxsurl)) {                                                      //  IF the weather story is not found
    $noted .= "<!-- First attempt in getting NWS URL data failed -->\n";               //   create note
    sleep(1);                                                                          //   wait a second !
    if(!$html = $dldata($wxsurl)) {                                                    //   IF the second chance WS fails
      sleep(1);                                                                        //    wait a second...
      $noted .= "<!-- Second attempt in getting NWS URL data failed -->\n";            //    create note
      if(!$html = $dldata($wxsurl)) {                                                  //    IF the last chance to get the WS failed
        $html = '';                                                                    //     set data to null
        $noted .= "<!-- FAILURE:  Could not get the WS data after 3 attempts -->\n";   //     create note
      }
    }
  }
 
  // fix characters
  $html = preg_replace('/°/', '&deg;', $html);                                         //  change it to an entity
  $html = preg_replace('/‘/', '\'', $html);                                            //  change stray character
  $html = preg_replace('/<u>/', '', $html);                                            //  fix error
  $html = preg_replace('/<\/u>/', '', $html);                                          //  fix error
  $html = preg_replace('/<br>/', '<br />', $html);                                     //  fix error

  // MAIN IMAGE URL
  preg_match('|c-tab__content">.*image"><img s.*"(.*)" style|Uis', $html, $image);     //  search for the image
  if(isset($image[1])){                                                                //  IF the image is available
    $iurl = $image[1];                                                                 //   pick out the image URL
    if(!preg_match("|http|",$iurl)) {$iurl = 'http://www.weather.gov'.$iurl;}
    $emi  = explode("/", $iurl);                                                       //   split up the URL
    $imgMn = array_pop($emi);                                                          //   get file name from the last array
    $ifn = substr($imgMn, 0, -4);                                                      //   cut off the extension - image file name w/o extension
    $ift = substr($imgMn, -3, 3);                                                      //   get the image file type/extension
    $di = $dldata($iurl);                                                              //   get the image
    $wxsimage = $ws_cachefolder.'wsimg_'.$wswfo.'_'.$ifn.'.'.$ift;                     //   assemble image cache file name
    $wsif = fopen($wxsimage, 'w');                                                     //   open weather story image file
    fwrite($wsif, $di);                                                                //   write image file to cache folder
    fclose($wsif);                                                                     //   close weather story image file
    $noted .= "<!-- Weather Story image downloaded -->\n";                             //   display remark

    // MAIN DESCRIPTION TEXT
    preg_match('|is-active">.*description">(.*)</div>|Uis', $html, $story);            //  search for the story
    (isset($story[1])) ? $wsdata = $story[1] :  $wsdata = '';                          //  IF the story is found, set variable

    // remvoe added non-story information
   if($wsdata <> '' and preg_match('/<hr>/Uis', $wsdata)){                            //  IF data shows a horizontal rule
      $wsdata = preg_replace('/<hr>.*/', ' +more', $wsdata);                           //   delete the hotizontal line and everythig after it
   }
    $wsdata = preg_replace("/target='_blank'/", 'onclick="window.open(this.href,\'_blank\');return false;"', $wsdata);  //  fix to validate
   
    if(mb_detect_encoding($wsdata) == 'UTF-8'){                                        // IF character encoding is set to URF-8
      $wsdata = html_entity_decode(strip_tags($wsdata));                               //  decode story text html enities
      $wsdata = htmlentities($wsdata);                                                 //  convert all story text html entities
      $noted .= "<!-- UTF-8 was detected in the main story -->\n";                     //  create note
    }

//  print_r($wsdata);  echo "<br />\n";



You can't phase me-I teach Middle School.
It's not you-It's WU.

Offline chief-david

  • Educational Weather
  • Forecaster
  • *****
  • Posts: 2845
  • Space Academy for Educators
    • Benilde-St. Margaret's Weather
Re: Curly's Wx Story script
« Reply #140 on: July 17, 2020, 02:33:41 PM »
Quote
Quote
// MAIN HEADLINE
    preg_match('|c-tabs-nav__link is-active"><span>(.*)</span>|Uis', $html, $hdln);    //  search for the story headline
    (isset($hdln[1])) ? $wshdlne = $hdln[1] : $wshdlne = '';                           //  IF the headline is found, set variable
    $wshdlne = html_entity_decode($wshdlne);                                           //   decode headline html enities
    $wshdlne = preg_replace('/ & /', ' &amp; ', $wshdlne);                             //   convert headline html entities
    $mainStory[$ifn] = array($wshdlne, $wsdata, $iurl, $wxsurl, $wxsimage, $ift, $ifn, $wswfo, $nfo);  //  array for cache file
    if($wshdlne <> ''){                                                                //  IF there is a headline, create table div
      $hdlnr[] = '     <td style="text-align: center; background-color:#F0F7FF; padding: 6px 0px 6px;"><a href="'.
                  $_SERVER['PHP_SELF'].'?i='.$wswfo.'&amp;ifn='.$ifn.'" title="'.$wshdlne.
                  '" style="text-decoration: none; color: #0071BC; font-size: 1.3em;">'.$wshdlne.'</a></td>';
    }
    else{
      $hdlnr[] = '     <td style="text-align: center; background-color:#F0F7FF; padding: 6px 0px 6px;"><a href="'.
                  $_SERVER['PHP_SELF'].'?i='.$wswfo.'&amp;ifn='.$ifn.'" title="'.$wshdlne.
                  '" style="text-decoration: none; color: #0071BC; font-size: 1.3em;">Weather Story</a></td>';
   }

    // ALTERNATE HEADLINE & DESCRIPTION & LINK
    preg_match_all('|c-tabs-nav__link"><span>(.*)</span>|Uis', $html, $hdlnAlt);         //  search for any ALTERNATE story headlines
    if(!empty($hdlnAlt[1])){                                                             //  IF any alternative headlines are found
      preg_match_all('|c-tab">.*description">(.*)</div>|Uis', $html, $altstory);         //   get the story
      preg_match_all('|c-tab">.*image"><img src="(.*)" style|Uis', $html, $altImage);    //   get the image URL
      foreach($hdlnAlt[1] as $ahk => $ahv){                                              //   FOR EACH alternative headline
        $ai = $altImage[1][$ahk];                                                        //    get the alternate image URL
        if(!preg_match("|http|",$ai)) {$ai = 'http://www.weather.gov'.$ai;}              //    IF the alternate image URL is incomplete, add pefix
        $eai = preg_split('/\//', $ai);                                                  //    split up the URL
        $imgalt = array_pop($eai);                                                       //    get the file name and extension
        $imgAlt = substr($imgalt, 0, -4);                                                //    cut off the extension - image file name w/o extension
        $aift = substr($imgalt, -3, 3);                                                  //    get the image file type/extension
        $awxsimage = $ws_cachefolder.'wsimg_'.$wswfo.'_'.$imgAlt.'.'.$aift;              //    assemble image cache file name
        $wshdlne = $ahv;                                                                 //    get alternate headline
        $wshdlne = html_entity_decode($wshdlne);                                         //    decode stray headline html enities
        $wshdlne = preg_replace('/ & /', ' &amp; ', $wshdlne);                           //    convert headline html entities
        $altstry = html_entity_decode($altstory[1][$ahk]);                               //    decode stray story text html enities
        $altstry = preg_replace("/target='_blank'/", 'onclick="window.open(this.href,\'_blank\');return false;"', $altstry); //  fix to validate
 
        $altstry = preg_replace('/&/', '&amp;', $altstry);                               //  convert headline html entities
        if(mb_detect_encoding($altstry) == 'UTF-8'){                                     //  IF character encoding is set to URF-8
          $altstry = html_entity_decode(strip_tags($altstry));                           //   decode story text html enities
          $altstry = htmlentities($altstry);                                             //   convert all story text html entities
          $noted .= "<!-- UTF-8 was detected in an alternate story -->\n";               //   create note
        }
        $altStory[$imgAlt] = array($wshdlne, $altstry, $ai, $wxsurl, $awxsimage, $aift, $imgAlt, $wswfo, $nfo);  // create array
        $hdlnr[] = '     <td style="text-align: center; background-color:#F0F7FF; padding: 6px 0px 6px;"><a href="'.$_SERVER['PHP_SELF'].'?i='.
                  $wswfo.'&amp;ifn='.$imgAlt.'" title="'.$wshdlne.'" style="text-decoration: none; color: #0071BC; font-size: 1.3em;">'.$wshdlne.'</a></td>';
      }
    }
    $focf = fopen($ws_cachefile,'wb');                                          //  open the text cache file
    if($focf) {                                                                 //  IF cache file opens
      fwrite($focf, "<?php\n\n");                                               //   write
      fwrite($focf, '$mainStory = '.var_export($mainStory, 1).";\n\n");         //   write the main story data
      fwrite($focf, '$altStory = '.var_export($altStory, 1).";\n\n");           //   write the alternate story data
      fwrite($focf, '$nfo = '.var_export($nfo, 1).";\n\n");                     //   write the foreccast office name
      fwrite($focf, '$hdlnr = '.var_export($hdlnr, 1).";\n\n");                 //   write the headline data
      fwrite($focf, "\n?>");                                                    //   end of the php code
      fclose($focf);                                                            //   close the file
      $noted .= "<!-- Cache file written -->\n";                                //   display notice
    }
  }
  else {                                                                        //  OR ELSE if a weather story was not posted at the WFO
    $onfo = $nfo;                                                               //   copy WFO location name
    if(is_file($ws_cachefile) and $wswfo != $NWSregion){                        //   IF the data cache file exists AND it not the default WFO
      unlink($ws_cachefile);                                                    //    delete the file
      $noted .= "<!-- Weather Story is not available for $nfo -->\n";           //   display remark
      // create the headliner
      $hdlnr[] = '<td style="text-align: center; background-color:#F0F7FF; color:#FFF; padding: 6px 0px 6px;; font-size: 1.3em">Today\'s Weather Story has not been posted for '.$onfo.'</td>';
    }

     // IF a WFO is selected without a WS, create the headliner
    elseif(in_array($wswfo,$nonWorkingCodes)){
      $noted .= "<!-- $onfo does not have the new weather story -->\n";          //   display notice
      $hdlnr[] = '<td style="text-align: center; background-color:#F0F7FF; color:#FFF; padding: 6px 0px 6px;; font-size: 1.3em">'.$onfo.' does not have the Weather Story</td>';
    }

    // IF a weather story WFO without a weatherstory folder is selected, create the headliner
    elseif(in_array($wswfo,$hpwxs2)){
      $hdlnr[] = '<td style="text-align: center; background-color:#F0F7FF; color:#FFF; padding: 6px 0px 6px;; font-size: 1.3em">Today\'s Weather Story has not been posted for '.$onfo.'</td>';
      $noted .= "<!-- $onfo does not have the weather story in their home page -->\n"; //   display notice
    }
   
    // OR ELSE create the headliner
    else {
      $hdlnr[] = '<td style="text-align: center; background-color:#F0F7FF; color:#FFF; padding: 6px 0px 6px;; font-size: 1.3em">Today\'s Weather Story has not been posted for '.$onfo.'</td>';
      $noted .= "<!-- $onfo does not have the weather story -->\n"; //   display notice
    }
    $goodWFO = false;
  }
}
// end of updating cache file



You can't phase me-I teach Middle School.
It's not you-It's WU.

Offline chief-david

  • Educational Weather
  • Forecaster
  • *****
  • Posts: 2845
  • Space Academy for Educators
    • Benilde-St. Margaret's Weather
Re: Curly's Wx Story script
« Reply #141 on: July 17, 2020, 02:36:13 PM »
Quote
// set page title
$wstitle2 = '<span style="color:#0061AC;">'.strtoupper($wstitle).'</span><br />'.$nfo.'<br /><span style="font-size:60%; vertical-align: text-top;">Weather Forecast Office</span>';

// include the cache file
if(is_file($ws_cachefile)){
  include($ws_cachefile);
}

// get submitted image
if(isset($_GET['ifn']) and $wswfo){                                                       // IF the WFO was submitted
  $imgname = htmlspecialchars(strip_tags($_GET['ifn']));                                  //  get the WFO code
  if(!array_key_exists($imgname, $mainStory) and !array_key_exists($imgname, $altStory)){ //  IF the name is not a key in either array
    $mskey = key($mainStory);                                                             //   get the main story array key
   $imgname = $mskey;                                                                    //   change the image name to the mainstory name
   $altStory = $mainStory;                                                               //   change the alternate story to the main story
    $noted .= "<!-- An invalid file name was selected -->\n";                             //   display notice
  }
  $mskey = key($mainStory);                                               //  get the main story array key
  ($mskey != $imgname) ? $altStory = $altStory : $altStory = $mainStory;  //  IF the selected story is the same as the main story, set variable back to the main data
  $wsimg = $altStory[$imgname][4];                                        //  get the cached image file name
  $wswfo = $altStory[$imgname][7];                                        //  get the WFO code
  $wxsurl = $altStory[$imgname][3];                                       //  get the NWS story text link
  $wsdata = $altStory[$imgname][1];                                       //  get the story headline
  $nfo = $altStory[$imgname][8];                                          //  get the WFO location
  $imName = $altStory[$imgname][6];                                       //  use main story image file name
  $imagepath = $altStory[$imgname][4];                                    //  get the cached image path
  $wshdlne = $altStory[$imgname][0];                                      //  get the headline
  $wxsimage = $imagepath;                                                 //  copy the image cache path
  if(file_exists($imagepath)) {                                           //  IF the iamge cache file exists
    $cft = filemtime($imagepath);                                         //   get cache file last modified time
    $cfAge = $uts - $cft;                                                 //   get cache file age in seconds
    if($cfAge > $updateTextTime) {                                        //   IF cache file has expired
      if($mskey != $imgname) {                                            //    IF the image cache file does not exist
       $di = $dldata($altStory[$imgname][2]);                             //     get the image from the NWS
        $icf = fopen($imagepath, 'w');                                    //     open the image cache file
        fwrite($icf, $di);                                                //     write image file in cache folder
        fclose($icf);                                                     //     open the image cache fil
        $noted .= "<!-- Weather Story image downloaded ! -->\n";          //     display remark
      }
    }
  }
  else{                                                                   //  OR ELSE
    $di = $dldata($altStory[$imgname][2]);                                //   get the image
    $aicf = fopen($imagepath, 'w');                                       //   open the alternate image cache file
    fwrite($aicf, $di);                                                   //   write image file in cache folder
    fclose($aicf);                                                        //   close the file
    $noted .= "<!-- Weather Story image downloaded  -->\n";               //   display remark
  }
}
else {                                                                    // OR ELSE
  if($goodWFO){
    $curkey = key($mainStory);                                            //  ket main story key
    $wsimg = $mainStory[$curkey][4];                                      //  use main story image
    $wswfo = $mainStory[$curkey][7];                                      //  use main story WFO
    $wxsurl = $mainStory[$curkey][3];                                     //  use main story cached file
    $wsdata = $mainStory[$curkey][1];                                     //  use main story cached text
    $nfo = $mainStory[$curkey][8];                                        //  use main story cached location
    $imName = $mainStory[$curkey][6];                                     //  use main story image file name
    $wshdlne = $mainStory[$curkey][0];                                    //  use main story headline
  }
}

// Change headline colors after selection
$hdlrcount = count($hdlnr);                                                          // count the headlines
$hdlnrwidth = round($tablewdth / $hdlrcount);                                        // get width of headline from table width
foreach($hdlnr as $hk => $hv){                                                       // FOR EACH headline
  if(preg_match("|$imName|",$hv)) {                                                  //  IF the selected image is in the headline
    $hv = preg_replace('/-color:#F0F7FF/', '-color:#0071BC', $hv);                   //   change background color to blue
    $hv = preg_replace('/ color: #0071BC/', ' color:#FFF', $hv);                     //   change text color to white
    $hv = preg_replace('/td style="/', 'td style="width:'.$hdlnrwidth.'px; ', $hv);  //   adjust column width
  }
  else{                                                                              //  OR ELSE
    $hv = preg_replace('/ background-color:#F0F7FF;/', '', $hv);                     //   change background color to a light color
    $hv = preg_replace('/td style="/', 'td style="width:'.$hdlnrwidth.'px; ', $hv);  //   change column width
    $hv = preg_replace('/td style="/', 'td class="hdline" style="border-bottom: thin solid #0071BC; border-top: thin solid #0071BC; ', $hv);  //   change column width
  }
  $hdline .= $hv."\n";
}

// print_r($wswfo);  echo "<br />\n";



You can't phase me-I teach Middle School.
It's not you-It's WU.

Offline chief-david

  • Educational Weather
  • Forecaster
  • *****
  • Posts: 2845
  • Space Academy for Educators
    • Benilde-St. Margaret's Weather
Re: Curly's Wx Story script
« Reply #142 on: July 17, 2020, 02:37:08 PM »
Quote
// FUNCTION - get data cURL
function GetDataByCurl($url)    {
  global $noted;                                                          // set global
  $data = '';                                                             // set variable
  $ch = curl_init();                                                      // initialize a cURL session
  curl_setopt($ch, CURLOPT_URL, $url);                                    // connect to provided URL
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);                            // don't verify peer certificate
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:14.0) Gecko/20100101 Firefox/14.0.1  (.NET CLR 4.0.20506)');
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);                            // 3 sec connection timeout
  curl_setopt($ch, CURLOPT_TIMEOUT, 3);                                   // 2 sec data timeout
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                         // return the data transfer
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION , true);                        // follow HTTP 3xx redirects
  curl_setopt($ch, CURLOPT_NOBODY, false);                                // set nobody
  curl_setopt($ch, CURLOPT_HEADER, false);                                // do not pass headers to the data stream
  $data = curl_exec($ch);                                                 // execute session
  if(curl_error($ch) <> '') {                                             // IF there is an error
   $noted .= "<!-- Error: ". curl_error($ch) ." -->\n";                   //  display error notice
   $noted .= "<!-- cURL Error at $url -->\n";                             //  display error source notice
  }
  $header_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);                // set header information
  curl_close($ch);                                                        // close the cURL session
  if(preg_match('/text\/html|image\/png|image\/gif|image\/jpeg|image\/jpg/',$header_type)) {                           // IF text/html is found in the header
   $data = $data;                                                         //  set data
   $noted .= "<!-- cURL Data source: $url -->\n";                         //  display notice
  }
  else {                                                                  // OR ELSE
    $noted .= "<!-- cURL error source: $url -->\n";                       //  display error source notice
    $data = '';                                                           //  set data to null value
  }
  return $data;                                                           // return data
}// end GetDataByCurl


// FUNCTION - Get file contents
function getWSBystream($url) {
  global $noted;
  $hdropts = array('http' => array('method'=>"GET",
                                   "timeout" => 3,
                                   'header'=>"Cache-control: max-age=0\r\n" .
                                   "Connection: close\r\n" .
                                   "User-agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:14.0) Gecko/20100101 Firefox/14.0.1  (.NET CLR 4.0.20506)\r\n" .
                                   "Accept: text/html; image/png; image/gif; image/jpeg; image/jpg\r\n"
                                   )
  );
  $context = stream_context_create($hdropts);
  if(@$data = file_get_contents($url,false,$context)){
    $noted .= "<!-- Stream data source: $url -->\n";                        //  display notice
  }
  else{
    $data = '';
    $noted .= "<!-- Stream failure source: $url -->\n";                     //  display notice
  }
  return $data;
} // end function getWSBystream


// FUNCTION - File size
function getFileSize($bytes) {
  $precision = 2;
  $units = ['Bytes', 'Kilobytes', 'Megabytes', 'Gigabytes', 'Terabytes'];
  $i = 0;
  while($bytes > 1024) {
    $bytes /= 1024;
    $i++;
  }
  return round($bytes, $precision) . ' ' . $units[$i];
}

?>
<table border="0" cellspacing="0" cellpadding="0" style="width: <?php echo $tablewdth ?>px; margin: 20px auto 0px auto;">
 <tr>
  <td style="text-align:center;"><a href="<?php echo $wxsurl; ?>" onclick="window.open(this.href,'_blank');return false;" title="Click to view this forecast office" style="text-decoration: none; color: inherit;"><span style="font-size:20px; letter-spacing:2px;"><b><?php echo $wstitle2 ?></b></span></a></td>
 </tr>
</table>



You can't phase me-I teach Middle School.
It's not you-It's WU.

Offline chief-david

  • Educational Weather
  • Forecaster
  • *****
  • Posts: 2845
  • Space Academy for Educators
    • Benilde-St. Margaret's Weather
Re: Curly's Wx Story script
« Reply #143 on: July 17, 2020, 02:38:03 PM »
Then assuming the rest is ok I have not changed it.



You can't phase me-I teach Middle School.
It's not you-It's WU.

Offline tmabell

  • Forecaster
  • *****
  • Posts: 394
    • Mishawaka Weather
Re: Curly's Wx Story script
« Reply #144 on: July 17, 2020, 03:17:29 PM »
That is old code.  For starters try changing all references from http://www.weather.gov/  to https://www.weather.gov/ The script will likely jump back to life.  If it doesn't I posted the most recent version a few posts up in this thread a while back.  Try that version and make the changes you require.
« Last Edit: July 17, 2020, 03:20:25 PM by tmabell »

Offline chief-david

  • Educational Weather
  • Forecaster
  • *****
  • Posts: 2845
  • Space Academy for Educators
    • Benilde-St. Margaret's Weather
Re: Curly's Wx Story script
« Reply #145 on: July 17, 2020, 03:48:15 PM »
also assuming all folders need to be in the same place. Which they are.

is the recall line still the same?

I also changed the number from 0 to 5.


Give it time but nothing yet.
« Last Edit: July 17, 2020, 03:58:50 PM by chief-david »



You can't phase me-I teach Middle School.
It's not you-It's WU.

Offline tmabell

  • Forecaster
  • *****
  • Posts: 394
    • Mishawaka Weather
Re: Curly's Wx Story script
« Reply #146 on: July 17, 2020, 04:02:31 PM »
Did you try the version I posted?

Offline chief-david

  • Educational Weather
  • Forecaster
  • *****
  • Posts: 2845
  • Space Academy for Educators
    • Benilde-St. Margaret's Weather
Re: Curly's Wx Story script
« Reply #147 on: July 17, 2020, 04:25:04 PM »
Yes the one from Mar 14, 2018.



You can't phase me-I teach Middle School.
It's not you-It's WU.

Offline tmabell

  • Forecaster
  • *****
  • Posts: 394
    • Mishawaka Weather
Re: Curly's Wx Story script
« Reply #148 on: July 17, 2020, 04:28:28 PM »
Check your PM's

Offline chief-david

  • Educational Weather
  • Forecaster
  • *****
  • Posts: 2845
  • Space Academy for Educators
    • Benilde-St. Margaret's Weather
Re: Curly's Wx Story script
« Reply #149 on: July 27, 2020, 12:13:57 PM »
Nothing I have tried has worked.

I was able to Iframe tmabell's info to my page-but that is not fair to his system.

He made/edited a script for me.
I am thinking either an issue with the call script or my template. So I have removed it.

Something else has to be the issue. Please help.

edit: here is my page with everything removed but weather story
https://rms-weather.rdale.org/weatherstory.php

I did use this for the call script and it allowed the page to finish.
<?php include("https://rms-weather.rdale.org/ChiefDavid1.php"); ?>

The frame on there is an iframe of my ChiefDavid1 page. Nothing in it.
« Last Edit: July 27, 2020, 01:04:45 PM by chief-david »



You can't phase me-I teach Middle School.
It's not you-It's WU.

 

anything