Last active
September 24, 2023 21:09
-
-
Save jacoby/103e25858ea006ed317eb282df3a88ef to your computer and use it in GitHub Desktop.
store_temp.pl pulls the current temperature from darksky.net (formerly forecast.io) and stores it in Redis. get_temp.pl retrieves it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# retrieves the current temperature from REDIS to be used in the bash prompt | |
# I should also get curr_time and, if the difference is too big, prints nothing | |
# but not implemented yet. | |
use feature qw{ say state unicode_eval unicode_strings } ; | |
use strict ; | |
use warnings ; | |
use utf8 ; | |
binmode STDOUT, ':utf8' ; | |
use Redis ; | |
use Data::Dumper ; | |
my $r = Redis->new() ; | |
print $r->get( 'curr_temp' ) . '°F' || ''; | |
$r->quit ; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# stores current temperature in REDIS so that get_temp.pl can be used | |
# in the bash prompt to display current temperature | |
use feature qw{ say state } ; | |
use strict ; | |
use warnings ; | |
use Data::Dumper ; | |
use DateTime ; | |
use IO::Interactive qw{ interactive } ; | |
use JSON ; | |
use LWP::UserAgent ; | |
use Redis ; | |
use YAML::XS qw{ LoadFile } ; | |
my $config = config() ; | |
my $url = | |
'https://api.darksky.net/forecast/' | |
. $config->{ apikey } . '/' | |
. ( join ',', map { $config->{ $_ } } qw{ latitude longitude } ) ; | |
my $agent = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 } ) ; | |
my $response = $agent->get( $url ) ; | |
if ( $response->is_success ) { | |
my $now = DateTime->now()->set_time_zone( 'America/Indiana/Indianapolis' )->datetime() ; | |
my $content = $response->content ; | |
my $forecast = decode_json $content ; | |
my $current = $forecast->{ currently } ; | |
my $temp_f = int $current->{ temperature } ; | |
my $r = Redis->new() ; | |
$r->set( 'curr_temp', $temp_f ) ; | |
$r->set( 'curr_time', $now ) ; | |
$r->quit ; | |
say { interactive } qq{Current Time: $now} ; | |
say { interactive } qq{Current Temperature: $temp_f} ; | |
} | |
exit ; | |
# ====================================================================== | |
# Reads configuration data from YAML file. Dies if no valid config file | |
# if no other value is given, it will choose current | |
# | |
# Shows I need to put this into a module | |
sub config { | |
my $config_file = $ENV{ HOME } . '/.forecast.yaml' ; | |
my $output = {} ; | |
if ( defined $config_file && -f $config_file ) { | |
my $output = LoadFile( $config_file ) ; | |
$output->{ current } = 1 ; | |
return $output ; | |
} | |
croak( 'No Config File' ) ; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment