scripts/gammastep.pl

41 рядки
827 B
Perl
Executable File

#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use JSON::XS;
my $ua = LWP::UserAgent->new();
my $json = JSON::XS->new();
my $location = "https://papillon.john.me.tz/data/location.json";
my $lat_lon = fetch_lat_lon($ua, $json, $location);
my $pid = fork;
unless ($pid) {
open STDIN, '/dev/null';
open STDOUT, '>>/dev/null';
open STDERR, '>>/dev/null';
`gammastep -l $lat_lon`;
}
sub fetch_lat_lon {
my ($ua, $json, $location) = @_;
my $raw = $ua->get($location)->content();
if (defined $raw) {
my $decoded = $json->decode($raw);
if (defined $decoded->{lat} && defined $decoded->{lon}) {
return "$decoded->{lat}:$decoded->{lon}";
}
print $decoded->{lat};
}
sleep 5;
return fetch_lat_lon($ua, $json, $location);
}
exit;