Shell script to getMalaysia weather (API) from met.gov.my and use it with tts_audio.sh (hamvoip allstarlink)

#! /usr/bin/perl
use warnings;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
use JSON;
use JSON::Parse ':all';
use Data::Dumper;
use Time::Piece ();
use feature qw( :all );
my $now = Time::Piece::localtime->strftime('%Y-%m-%d');
my $time = Time::Piece::localtime->strftime('%I:%M %P');
my $ua = LWP::UserAgent->new;
my $json = JSON->new->utf8->allow_nonref;
my $req = HTTP::Request->new(GET => 'https://api.met.gov.my/v2/data?datasetid=FORECAST&datacategoryid=GENERAL&locationid=LOCATION:300&start_date='. $now. '&end_date=' . $now);

my $filename = '/tmp/hello.txt';

$req->content_type('application/json');
$ua->ssl_opts(SSL_verify_mode => 0); #Used to ignore certificate
$ua->default_header('Authorization' => "METToken xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
my $response = $ua->request($req);
#my $json = decode_json($response);
if ($response->is_success) {
    #print Dumper($response->content());
    my $perlData = $json->decode($response->content());
    #my $result = Dumper($perlData);
    my $locationname=$perlData->{results}->[5]->{locationname};
    my $tempmaxunit=$perlData->{results}->[4]->{attributes}->{unit};
    my $tempmax=$perlData->{results}->[4]->{value};
    my $tempminunit=$perlData->{results}->[3]->{attributes}->{unit};
    my $tempmin=$perlData->{results}->[3]->{value};
    my $value=$perlData->{results}->[5]->{value};
    my $when=$perlData->{results}->[5]->{attributes}->{when};
    my $code=$perlData->{results}->[5]->{attributes}->{code};
    #my $str = "The time is, " . $time . " , and the location is at " . $locationname . ", today minimum temperature is at " . $tempmin . " degree " . $tempminunit . " and today maximum temperature is at " . $tempmax . " degree " . $tempmaxunit . " and as a result, today will be, " . $value . " , and , " . $when . " , " . $code . "\n";
    my $str = "The time is, " . $time . " , and the location is at " . "Sue-bung- Jar-ya-" . ", today minimum temperature is at " . $tempmin . " degree " . $tempminunit . " and today maximum temperature is at " . $tempmax . " degree " . $tempmaxunit . " and as a result, today will be, " . $value . " , and , " . $when . " , " . $code . " ..........\n";
    open(FH, '>', $filename) or die $!;
    print FH $str;
    close(FH);
    print "Writing to file successfully!\n";
    #print "Location is at " . $locationname . ", " . "today will be " . $value . " and " . $when . " " . $code . "\n";
}
else{
    print " inside failed block\n";
}

crontab your weather script in hello.txt and generate your audio via tts_audio.sh

/root/scripts/getapimetgovmy.pl && cd /tmp && tts_audio.sh /tmp/hello.txt && /usr/bin/asterisk -rx “rpt localplay 47259 /tmp/hello”

Leave a Reply

Your email address will not be published. Required fields are marked *

*