Implement writing price to db
This commit is contained in:
35
main.go
35
main.go
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
@@ -8,8 +9,6 @@ import (
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
_ "embed"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
@@ -56,20 +55,24 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
price := Price{
|
||||
Price: 100,
|
||||
Timestamp: time.Now(),
|
||||
log.Printf("Fetching price from url %s", url)
|
||||
price, err := GetPrice(url)
|
||||
if err != nil {
|
||||
Error.Fatalf("Error getting price: %v", err)
|
||||
return
|
||||
}
|
||||
log.Printf("%#v", price)
|
||||
log.Printf("Fetched price as '%d' from url %q", price, url)
|
||||
|
||||
err = SavePrice(&db, price)
|
||||
if err != nil {
|
||||
Error.Fatalf("Error saving price: %v", err)
|
||||
return
|
||||
}
|
||||
log.Printf("Price '%d' saved to db", price)
|
||||
}
|
||||
|
||||
type Price struct {
|
||||
Price int
|
||||
Timestamp time.Time
|
||||
}
|
||||
|
||||
func GetPrice() (Price, error) {
|
||||
price := Price{Timestamp: time.Now()}
|
||||
func GetPrice(url string) (int, error) {
|
||||
price := 0
|
||||
|
||||
res, err := http.Get(url)
|
||||
if err != nil {
|
||||
@@ -93,7 +96,7 @@ func GetPrice() (Price, error) {
|
||||
Error.Fatalf("failed parsing price: %q", res)
|
||||
return
|
||||
}
|
||||
price.Price, err = strconv.Atoi(res[0])
|
||||
price, err = strconv.Atoi(res[0])
|
||||
if err != nil {
|
||||
Error.Fatalf("failed converting price to int: %v", err)
|
||||
return
|
||||
@@ -102,7 +105,3 @@ func GetPrice() (Price, error) {
|
||||
|
||||
return price, nil
|
||||
}
|
||||
|
||||
func SavePrice(price Price) error {
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user