Skip to content

Commit 868249d

Browse files
committed
anti-caching tweak and PRICE_AREA variable
1 parent 988170f commit 868249d

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Vattenfall to InfluxDB exporter
22

3-
This is a simple exporter that scrapes the Vattenfall API and exports the prices to InfluxDB. It fetches the prices for the previous 24h and next 24h.
3+
This is a simple exporter that scrapes the Vattenfall API and exports the prices to InfluxDB. It fetches the prices for the previous 24h and next 24h.
44

55

66
## Usage with Docker compose
@@ -37,6 +37,7 @@ services:
3737
INFLUXDB_TOKEN: 'some_password'
3838
INFLUXDB_BUCKET: 'elprice'
3939
INFLUXDB_ORG: 'iot'
40+
PRICE_AREA: 'SN3'
4041
```
4142
4243
Then you add InfluxDB as a datasource in Grafana(choose Flux as query language):

main.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
var (
17-
version = "0.0.18"
17+
version = "0.0.19"
1818
debug bool
1919
)
2020

@@ -36,11 +36,24 @@ func doEvery(d time.Duration, f func(time.Time)) {
3636
func pushToInflux(t time.Time) {
3737
client := resty.New()
3838

39+
// Read the PRICE_AREA environment variable, default to "SN3" if not set
40+
priceArea := os.Getenv("PRICE_AREA")
41+
if priceArea == "" {
42+
fmt.Println("Error: PRICE_AREA environment variable is not set")
43+
os.Exit(1)
44+
}
45+
3946
endOfDay := time.Now().Truncate(24 * time.Hour).Add(24*time.Hour - 1*time.Second)
40-
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"
47+
url := fmt.Sprintf("https://www.vattenfall.se/api/price/spot/pricearea/%s/%s/%s?_=%d",
48+
time.Now().Format("2006-01-02"),
49+
endOfDay.AddDate(0, 0, 1).Format("2006-01-02"),
50+
priceArea,
51+
time.Now().UnixNano()) // UnixNano returns a unique number
4152

4253
resp, err := client.R().
4354
SetHeader("User-Agent", fmt.Sprintf("vattenfall-to-influxdb/%s (+https://github.com/rvoitenko/vattenfall-to-influxdb)", version)).
55+
SetHeader("Cache-Control", "no-cache, no-store, must-revalidate").
56+
SetHeader("Pragma", "no-cache").
4457
Get(url)
4558

4659
if err != nil {

0 commit comments

Comments
 (0)