Blame


1 3baf40e1 2023-04-13 mischa #!/usr/bin/env perl
2 3baf40e1 2023-04-13 mischa use 5.024;
3 3baf40e1 2023-04-13 mischa use strict;
4 3baf40e1 2023-04-13 mischa use warnings;
5 3baf40e1 2023-04-13 mischa use autodie;
6 3baf40e1 2023-04-13 mischa use Getopt::Long;
7 3baf40e1 2023-04-13 mischa use HTTP::Tiny;
8 3baf40e1 2023-04-13 mischa use JSON::PP;
9 3baf40e1 2023-04-13 mischa use Data::Dumper;
10 3baf40e1 2023-04-13 mischa
11 3baf40e1 2023-04-13 mischa my $TOKEN = "";
12 3baf40e1 2023-04-13 mischa
13 3baf40e1 2023-04-13 mischa my $http = HTTP::Tiny->new;
14 3baf40e1 2023-04-13 mischa my %HEADERS = ("Content-Type" => "application/json", "Authorization" => "Bearer $TOKEN");
15 3baf40e1 2023-04-13 mischa my $uri = "https://api.tibber.com/v1-beta/gql";
16 3baf40e1 2023-04-13 mischa
17 3baf40e1 2023-04-13 mischa my $request = HTTP::Tiny->new('default_headers' => \%HEADERS);
18 3baf40e1 2023-04-13 mischa my $json = JSON::PP->new;
19 3baf40e1 2023-04-13 mischa
20 3baf40e1 2023-04-13 mischa my $body = $json->encode({query => "{viewer{homes{currentSubscription{priceInfo{current{total energy tax startsAt} today{total energy tax startsAt} tomorrow{total energy tax startsAt}}}}}}"});
21 3baf40e1 2023-04-13 mischa #print "$body\n";
22 3baf40e1 2023-04-13 mischa
23 3baf40e1 2023-04-13 mischa my $response = $request->post($uri, {'content' => $body});
24 3baf40e1 2023-04-13 mischa #print "$response->{'status'} $response->{'reason'}\n";
25 3baf40e1 2023-04-13 mischa #print "$response->{'content'}\n";
26 3baf40e1 2023-04-13 mischa my $data = $json->decode($response->{'content'})->{'data'}->{'viewer'}->{'homes'}[0]->{'currentSubscription'};
27 3baf40e1 2023-04-13 mischa
28 3baf40e1 2023-04-13 mischa print "*** priceInfo ***\n";
29 3baf40e1 2023-04-13 mischa print "current: ";
30 3baf40e1 2023-04-13 mischa if (!length $data->{'priceInfo'}->{'current'}) {
31 3baf40e1 2023-04-13 mischa print "price not set\n";
32 3baf40e1 2023-04-13 mischa } else {
33 3baf40e1 2023-04-13 mischa print "$data->{'priceInfo'}->{'current'}->{'total'} (Energy: $data->{'priceInfo'}->{'current'}->{'energy'}; Tax: $data->{'priceInfo'}->{'current'}->{'tax'})\n";
34 3baf40e1 2023-04-13 mischa #print Dumper $data->{'priceInfo'}->{'current'};
35 3baf40e1 2023-04-13 mischa }
36 3baf40e1 2023-04-13 mischa
37 3baf40e1 2023-04-13 mischa print "today: ";
38 3baf40e1 2023-04-13 mischa if (!length $data->{'priceInfo'}->{'today'}[0]) {
39 3baf40e1 2023-04-13 mischa print "price not set\n";
40 3baf40e1 2023-04-13 mischa } else {
41 3baf40e1 2023-04-13 mischa print "$data->{'priceInfo'}->{'today'}[0]->{'total'} (Energy: $data->{'priceInfo'}->{'today'}[0]->{'energy'}; Tax: $data->{'priceInfo'}->{'today'}[0]->{'tax'})\n";
42 3baf40e1 2023-04-13 mischa #print Dumper $data->{'priceInfo'}->{'today'}[0];
43 3baf40e1 2023-04-13 mischa }
44 3baf40e1 2023-04-13 mischa
45 3baf40e1 2023-04-13 mischa print "tomorrow: ";
46 3baf40e1 2023-04-13 mischa if (!length $data->{'priceInfo'}->{'tomorrow'}[0]) {
47 3baf40e1 2023-04-13 mischa print "price set at 13:00\n";
48 3baf40e1 2023-04-13 mischa } else {
49 3baf40e1 2023-04-13 mischa print "$data->{'priceInfo'}->{'tomorrow'}[0]->{'total'} (Energy: $data->{'priceInfo'}->{'tomorrow'}[0]->{'energy'}; Tax: $data->{'priceInfo'}->{'tomorrow'}[0]->{'tax'})\n";
50 3baf40e1 2023-04-13 mischa #print Dumper $data->{'priceInfo'}->{'tomorrow'}[0];
51 3baf40e1 2023-04-13 mischa }