Skip to content

Commit 3070077

Browse files
committed
added gzip compression
1 parent 714eac3 commit 3070077

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

main.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package main
22

33
import (
4+
"compress/gzip"
45
"context"
56
"encoding/json"
67
"fmt"
78
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
9+
"io"
810
"io/ioutil"
911
"net/http"
1012
"os"
@@ -15,7 +17,7 @@ import (
1517
)
1618

1719
var (
18-
version = "0.0.11"
20+
version = "0.0.12"
1921
debug bool // Add this line for the debug flag
2022
)
2123

@@ -36,17 +38,35 @@ func doEvery(d time.Duration, f func(time.Time)) {
3638

3739
func pushToInflux(t time.Time) {
3840
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"
4243
req, _ := http.NewRequest("GET", url, nil)
4344
req.Header.Set("User-Agent", fmt.Sprintf("vattenfall-to-influxdb/%s (+https://github.com/rvoitenko/vattenfall-to-influxdb)", version))
4445
resp, err := httpClient.Do(req)
46+
if err != nil {
47+
fmt.Println("Error on request:", err)
48+
return
49+
}
4550
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)
4765
if err != nil {
48-
fmt.Println("No response from request")
66+
fmt.Println("Error reading response body:", err)
67+
return
4968
}
69+
5070
var result Response
5171
if err := json.Unmarshal(body, &result); err != nil {
5272
fmt.Println("Can not unmarshal JSON")
@@ -65,7 +85,6 @@ func pushToInflux(t time.Time) {
6585
return
6686
}
6787

68-
// Parse the timestamp using the Europe/Stockholm time zone
6988
date, error := time.ParseInLocation("2006-01-02T15:04:05", rec.TimeStamp, location)
7089
if error != nil {
7190
fmt.Println(error)

0 commit comments

Comments
 (0)