Author Topic: tweetWX / forecast issue  (Read 879 times)

0 Members and 1 Guest are viewing this topic.

Offline sydeburnz

  • Senior Member
  • **
  • Posts: 58
tweetWX / forecast issue
« on: April 08, 2016, 05:07:31 PM »
been searching to see if any changes happen to cause issues, but it seems spaces are being removed from the forecast when it downloads, so the replacement strings are not working therefore not posting to twitter for being to long.  any help is appreciated. :)

Topeka Fcst: ThisAfternoonSunny andBreezy: , Hi 64°F; TonightMostly Clear: , Lo 33°F; SaturdayMostly Sunny: , Hi 68°F; #kswxlink kswxlink.com too long (142) -- reducing size
Topeka Fcst: ThisAfternoonSunny andBreezy: , Hi 64°F; TonightMstly Clr: , Lo 33°F; SatMstly Sunny: , Hi 68°F; #kswxlink kswxlink.com
len=133
status={ "errors": [ { "code": 186, "message": "Status is over 140 characters." } ] }

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: tweetWX / forecast issue
« Reply #1 on: April 08, 2016, 05:12:57 PM »
What is your website URL?  What version of TweetWX-forecast.php are you using?
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline sydeburnz

  • Senior Member
  • **
  • Posts: 58
Re: tweetWX / forecast issue
« Reply #2 on: April 08, 2016, 05:19:44 PM »
www.kswxlink.com

looks like im on v1.0, better upgrade before asking :)




Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: tweetWX / forecast issue
« Reply #3 on: April 08, 2016, 05:20:22 PM »
You can try this version of TweetWX-forecast.php
Code: [Select]
<?php
/* TweetWX-forecast1.php

 This package is based on code by Joe Chung at http://nullinfo.wordpress.com/oauth-twitter/
 
 Repackaged for TweetWX by Ken True - Saratoga-weather.org  08-Jun-2010
 Docs at http://saratoga-weather.org/scripts-TweetWX.php
 TweetWX-forecast.php  V1.00 - 11-Jun-2010 - Initial relase
// Version 2.00 - 17-Jul-2015 - major changes for new NWS dual-icon forecast

 This utility function should be used once to get the OAuth request_token and request_token_secret
 for use in the next step of setup.
 
*/
$Version 'TweetWX-forecast.php - V2.00 - 17-Jul-2015';
header("Content-type: text/plain\n");
if (
phpversion() < 5) {
  echo 
'Failure: This Script requires PHP version 5 or greater. You only have PHP version: ' phpversion();
  exit;
}
// --------------- settings ----------------------
$tweetWXdir './';
$wxdir '../';  // path to the advforecast2.php and Settings.php (if used in template)
$tweetHeader 'NWS Fcst: '# leave the : in the text.. may be replaced by (n/3): if multi-tweet is used
$tweetTags 'http://bit.ly/NKksn #CAwx';  // customize for your Twitter tags and/or website url (shortened)
// Fill in the next 2 variables from the values produced in TweetWX-setup2.php.
$access_token '-replace-with-token-from-TweetWX-setup2-'// oauth_token
$access_token_secret '-replace-with-token_secret-from-TweetWX-setup2-'// oauth_token_secret

// ----------- end settings ----------------------

require_once($tweetWXdir 'TweetWX-globals.php');
if (
file_exists($wxdir "Settings.php")) {
include_once($wxdir "Settings.php");
}
print 
"$Version\n";

$doPrintNWS false;
$_REQUEST['force'] = '1';
include_once(
$wxdir'advforecast2.php');

print 
"$Version\n";
// Extract the 'good bits' from the forecasticons array for the text-only forecast
//
/*
<-- part 0 = 'Tonight' -->
<-- part 1 = '' -->
<-- part 2 = '<img src="./forecast/images/nfew.jpg" alt="Tonight: Patchy fog after 2am.  Otherwise, increasing clouds, with a low around 61. Calm wind becoming northeast around 5 mph. " title="Tonight: Patchy fog after 2am.  Otherwise, increasing clouds, with a low around 61. Calm wind becoming northeast around 5 mph. " class="forecast-icon"/>' -->
<-- part 3 = 'Mostly Clear' -->
<-- part 4 = 'then Patchy' -->
<-- part 5 = 'Fog' -->
*/

# process the raw forecast icons for the forecast text to use
for ($i=0;$i<count($forecasticons);$i++) {
$fparts explode('<br/>',$forecasticons[$i]);
// print "<-- icon $i = '".$forecasticons[$i]." -->\n";
// foreach ($fparts as $n => $t) {print "<-- part $n = '" . $t . "' -->\n"; }
    
$t $fparts[0];
$t .= (trim($fparts[1])<>'')?' '.trim($fparts[1]):'';
$t .= ': '$fparts[3];
$t .= (trim($fparts[4])<>'')?' '.trim($fparts[4]):'';
$t .= (isset($fparts[5]) and trim($fparts[5])<>'')?' '.trim($fparts[5]):'';
$t .= (isset($fparts[6]) and trim($fparts[6])<>'')?' '.trim($fparts[6]):'';
$t .= (isset($fparts[7]) and trim($fparts[7])<>'')?' '.trim($fparts[7]):'';
$t .= (isset($fparts[8]) and trim($fparts[8])<>'')?' '.trim($fparts[8]):'';
$t .= (isset($fparts[9]) and trim($fparts[9])<>'')?' '.trim($fparts[9]):'';
$t .= (isset($fparts[10]) and trim($fparts[10])<>'')?' '.trim($fparts[10]):'';
$t .= ', '.$forecasttemp[$i] . '; ';
$t preg_replace('|\s\s+|is',' ',$t);
# $t = preg_replace('| &deg;|is','°',$t);
$t preg_replace('| &deg;|is','',$t);
$fcst[$i] = strip_tags($t);
}
// print_r($fcst);

# try full length message first
$message $tweetHeader ' '$fcst[0] . $fcst[1] . $fcst[2] . ' ' $tweetTags;
if (
strlen($message) > 135) {
print $message "\nMessage is too long (".strlen($message). " bytes) -- reducing size.\n";
$message TW_shorten_tweet($message);
}
echo 
$message."\nlen=".strlen($message)." bytes.\n";

if (
strlen($message) > 135) {
# use 3 messages instead
print "\nMessage still too long (".strlen($message)." bytes).  Formatting 3 tweets instead\n";
for ($i=0;$i<3;$i++) {
$n $i+1;
$t str_replace(":","($n/3):",$tweetHeader);
$message $t $fcst[$i] . ' ' $tweetTags;
$message preg_replace('|\s\s+|is',' ',$message); # every char counts :)
        
if(strlen($message)> 135) {
print $message "\nMessage is too long (".strlen($message). " bytes) -- reducing size.\n";
$message TW_shorten_tweet($message);
}
$utfmessage iconv('ISO-8859-1','UTF-8//TRANSLIT',$message);
echo "\nUTF='$utfmessage' len=".strlen($utfmessage)." bytes.\nstatus=";

// POST a tweet using OAuth authentication
$retarr post_tweet(OAUTH_CONSUMER_KEYOAUTH_CONSUMER_SECRET,
                           $utfmessage$access_token$access_token_secret,
                           truetrue);
    sleep(2); # Pause between tweets
print "\n";
}

} else {

$utfmessage iconv('ISO-8859-1','UTF-8//TRANSLIT',$message);
echo 
"UTF='$utfmessage' len=".strlen($utfmessage)." bytes.\nstatus=";

// POST a tweet using OAuth authentication
$retarr post_tweet(OAUTH_CONSUMER_KEYOAUTH_CONSUMER_SECRET,
                           
$utfmessage$access_token$access_token_secret,
                           
truetrue);
}

function 
TW_shorten_tweet($M) {
# -- shorten the tweet to fit the 140 character limit (if possible)

$msg $M;
# list of abbreviations to apply to shorten the tweet
# Note.. these can be regular expressions in the first field for the preg_replace()
$ALIST '
Sunday|Sun|
Monday|Mon|
Tuesday|Tue|
Wednesday|Wed|
Thursday|Thu|
Friday|Fri|
Saturday|Sat|
Afternoon|Aft.noon|
chance|Chc|
Partly|Ptly|
Heavy|Hvy|
Rain/Snow|RaSn|
Breezy|Wnd|
Snow|Sn|
Frost|Frst|
Blustery|Wnd|
Drizzle|Driz|
Areas Dense Fog|Area Dns Fog|
likely|lkly|
slight|Slt|
Windy|Wndy|
T-storms|Tstrm|
Tropical Storm|TropStrm|
Conditions|Cond|
Expected|Expctd|
Isolated|Isol|
Possible|Poss|
Severe|Svr|
Patchy|Ptchy|
showers|Shwrs|
Mostly|Mstly|
Clear|Clr|
Cloudy|Cldy|
Clouds|Clds|
Scattered|Sctd|
increasing|Incr|
decreasing|Decr|
\sand\s|+|
\s*then\s*|->|
\.\s+|+|
'
;

$A explode("\n",$ALIST);
# Apply each shortner to the text
foreach ($A as $rec) {
list($k,$v) = explode('|',$rec.'||');
if (strlen($v) < 1) {continue;}
$msg preg_replace("|$k|is",$v,$msg);
// print "<!-- repl '$k' with '$v' -->\n";
}

$msg preg_replace('|\s\s+|is',' ',$msg); # remove extra spaces if any
return ($msg);

// end TW_shorten_tweet function


?>
It's what I'm currently using on my site.  It will split big forecasts into three separate tweets, one for each period, if need be.
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline sydeburnz

  • Senior Member
  • **
  • Posts: 58
Re: tweetWX / forecast issue
« Reply #4 on: April 08, 2016, 05:33:43 PM »
worked like a charm, ken.  thanks for your support as always.

im really digging the meteo templates, working on overhauling my site.


Offline sydeburnz

  • Senior Member
  • **
  • Posts: 58
Re: tweetWX / forecast issue
« Reply #5 on: April 08, 2016, 05:43:33 PM »
any chance someone has added warnings, advisories, etc to tweetwx?


Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: tweetWX / forecast issue
« Reply #6 on: April 08, 2016, 06:02:51 PM »
I haven't, but Mike Challis' messenger script did .. he's not updated it in a while, so I don't know if it still functions.
http://www.642weather.com/weather/scripts-noaa-top-weather-alerter.php
It uses the TweetWX common functions (AFAIK).
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

 

anything