1
1
package main
2
2
3
3
import (
4
+ "compress/gzip"
4
5
"context"
5
6
"encoding/json"
6
7
"fmt"
7
8
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
9
+ "io"
8
10
"io/ioutil"
9
11
"net/http"
10
12
"os"
@@ -15,7 +17,7 @@ import (
15
17
)
16
18
17
19
var (
18
- version = "0.0.11 "
20
+ version = "0.0.12 "
19
21
debug bool // Add this line for the debug flag
20
22
)
21
23
@@ -36,17 +38,35 @@ func doEvery(d time.Duration, f func(time.Time)) {
36
38
37
39
func pushToInflux (t time.Time ) {
38
40
httpClient := & http.Client {}
39
- // Set current time to the end of the day before adding a day
40
- endOfDay := time .Now ().Truncate (24 * time .Hour ).Add (24 * time .Hour - 1 * time .Second )
41
- url := "https://www.vattenfall.se/api/price/spot/pricearea/" + time .Now ().Format ("2006-01-02" ) + "/" + endOfDay .AddDate (0 , 0 , 1 ).Format ("2006-01-02" ) + "/SN3"
41
+ endOfDay := time .Now ().Truncate (24 * time .Hour ).Add (24 * time .Hour - 1 * time .Second )
42
+ url := "https://www.vattenfall.se/api/price/spot/pricearea/" + time .Now ().Format ("2006-01-02" ) + "/" + endOfDay .AddDate (0 , 0 , 1 ).Format ("2006-01-02" ) + "/SN3"
42
43
req , _ := http .NewRequest ("GET" , url , nil )
43
44
req .Header .Set ("User-Agent" , fmt .Sprintf ("vattenfall-to-influxdb/%s (+https://github.com/rvoitenko/vattenfall-to-influxdb)" , version ))
44
45
resp , err := httpClient .Do (req )
46
+ if err != nil {
47
+ fmt .Println ("Error on request:" , err )
48
+ return
49
+ }
45
50
defer resp .Body .Close ()
46
- body , err := ioutil .ReadAll (resp .Body )
51
+
52
+ var reader io.Reader
53
+ switch resp .Header .Get ("Content-Encoding" ) {
54
+ case "gzip" :
55
+ reader , err = gzip .NewReader (resp .Body )
56
+ if err != nil {
57
+ fmt .Println ("Error on gzip decompression:" , err )
58
+ return
59
+ }
60
+ default :
61
+ reader = resp .Body
62
+ }
63
+
64
+ body , err := ioutil .ReadAll (reader )
47
65
if err != nil {
48
- fmt .Println ("No response from request" )
66
+ fmt .Println ("Error reading response body:" , err )
67
+ return
49
68
}
69
+
50
70
var result Response
51
71
if err := json .Unmarshal (body , & result ); err != nil {
52
72
fmt .Println ("Can not unmarshal JSON" )
@@ -65,7 +85,6 @@ func pushToInflux(t time.Time) {
65
85
return
66
86
}
67
87
68
- // Parse the timestamp using the Europe/Stockholm time zone
69
88
date , error := time .ParseInLocation ("2006-01-02T15:04:05" , rec .TimeStamp , location )
70
89
if error != nil {
71
90
fmt .Println (error )
0 commit comments