From 3baf40e13bf91a7ee8b1257ec20ebec4333a1a6e Mon Sep 17 00:00:00 2001 From: mischa Date: Thu, 13 Apr 2023 16:50:12 +0200 Subject: [PATCH] Initial commit --- tibber.pl | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 tibber.pl diff --git a/tibber.pl b/tibber.pl new file mode 100755 index 0000000..c5a7e89 --- /dev/null +++ b/tibber.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl +use 5.024; +use strict; +use warnings; +use autodie; +use Getopt::Long; +use HTTP::Tiny; +use JSON::PP; +use Data::Dumper; + +my $TOKEN = ""; + +my $http = HTTP::Tiny->new; +my %HEADERS = ("Content-Type" => "application/json", "Authorization" => "Bearer $TOKEN"); +my $uri = "https://api.tibber.com/v1-beta/gql"; + +my $request = HTTP::Tiny->new('default_headers' => \%HEADERS); +my $json = JSON::PP->new; + +my $body = $json->encode({query => "{viewer{homes{currentSubscription{priceInfo{current{total energy tax startsAt} today{total energy tax startsAt} tomorrow{total energy tax startsAt}}}}}}"}); +#print "$body\n"; + +my $response = $request->post($uri, {'content' => $body}); +#print "$response->{'status'} $response->{'reason'}\n"; +#print "$response->{'content'}\n"; +my $data = $json->decode($response->{'content'})->{'data'}->{'viewer'}->{'homes'}[0]->{'currentSubscription'}; + +print "*** priceInfo ***\n"; +print "current: "; +if (!length $data->{'priceInfo'}->{'current'}) { + print "price not set\n"; +} else { + print "$data->{'priceInfo'}->{'current'}->{'total'} (Energy: $data->{'priceInfo'}->{'current'}->{'energy'}; Tax: $data->{'priceInfo'}->{'current'}->{'tax'})\n"; + #print Dumper $data->{'priceInfo'}->{'current'}; +} + +print "today: "; +if (!length $data->{'priceInfo'}->{'today'}[0]) { + print "price not set\n"; +} else { + print "$data->{'priceInfo'}->{'today'}[0]->{'total'} (Energy: $data->{'priceInfo'}->{'today'}[0]->{'energy'}; Tax: $data->{'priceInfo'}->{'today'}[0]->{'tax'})\n"; + #print Dumper $data->{'priceInfo'}->{'today'}[0]; +} + +print "tomorrow: "; +if (!length $data->{'priceInfo'}->{'tomorrow'}[0]) { + print "price set at 13:00\n"; +} else { + print "$data->{'priceInfo'}->{'tomorrow'}[0]->{'total'} (Energy: $data->{'priceInfo'}->{'tomorrow'}[0]->{'energy'}; Tax: $data->{'priceInfo'}->{'tomorrow'}[0]->{'tax'})\n"; + #print Dumper $data->{'priceInfo'}->{'tomorrow'}[0]; +}