With my new job, I haven’t had much time to add much to this website.
So here’s this small perl script I wrote that sends a summary of the weather in SMS format from my iPhone daily.
There was a need, so I figured why not help out those who need the weather forecast =].
use LWP::Simple;
use JSON qw( decode_json );
my $lastRun = 0;
open (MYFILE, 'weatherSMS_LastRun');
while () {
chomp;
$lastRun = $_;
}
close (MYFILE);
my ($S,$M,$H,$d,$m,$Y) = localtime($lastRun);
my ($S2,$M2,$H2,$d2,$m2,$Y2) = localtime(time);
if ( $Y2 == $Y && $m2 == $m && $d2 == $d ){
exit 0;
}
$json = get("https://api.forecast.io/forecast/APIKEY/LATITUDE,LONGITUDE?units=ca&exclude=currently,minutely,hourly,flags");
my $decoded = decode_json($json);
#ensure we can write the file
open(F,'>weatherSMS_LastRun') || die "Could not open file to mark execution";
print F time;
close(F);
my @timeslot = @{ $decoded->{'daily'}{'data'} };
my $forcast = "";
foreach my $f ( @timeslot ) {
my ($S,$M,$H,$d,$m,$Y) = localtime($f->{"time"});
$m = $m + 1;
$forcast .= "$m/$d: ".sprintf("%.3f",$f->{"temperatureMin"})."-".sprintf("%.3f",$f->{"temperatureMax"})."C ".$f->{"summary"}."\n";
}
#printf($forcast);
`/Applications/biteSMS.app/biteSMS -send -carrier PHONENUMBER "$forcast"`;
In order to execute this daily, the following plist was used:
Label
com.joshho.weatherSMS
LowPriorityIO
Nice
1
AbandonProcessGroup
OnDemand
ProgramArguments
/usr/local/bin/perl
/location to perlscript/weatherSMS.pl
StartCalendarInterval
Minute
0
Submit it to be executed hourly by running `launchctl load [path to plist]`
Edit
Edits were made to cover the fact that your phone may not always have internet.
(It may be locked/sleeping – which may turn off the wifi?)