geizhals

package
v2.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2023 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidURL = errors.New("invalid URL")
View Source
var ErrTooManyRetries = errors.New("too many retries")

Functions

func LocationFromURL

func LocationFromURL(rawurl string) (string, error)

Types

type Currency

type Currency int

Currency represents the currency of an entity price.

const (
	EUR Currency = 1
	PLN Currency = 2
	GBP Currency = 3
)

func CurrencyFromLocation

func CurrencyFromLocation(location string) Currency

CurrencyFromLocation returns the currency of the given location.

func (Currency) String

func (c Currency) String() string

type Entity

type Entity struct {
	ID         int64 `json:"id"`
	GeizhalsID int64
	UpdatedAt  time.Time
	Prices     []EntityPrice `gorm:"foreignkey:EntityID;constraint:OnDelete:CASCADE;"`
	Name       string        `json:"name"`
	URL        string        `json:"url"`
	Type       EntityType    `json:"type"`
}

func DownloadEntity

func DownloadEntity(url string) (Entity, error)

DownloadEntity retrieves the metadata (name, price) for a given entity hosted on Geizhals.

func (Entity) FullURL

func (e Entity) FullURL(location string) string

FullURL returns the URL to download the HTML of the entity for the given location.

func (Entity) GetPrice

func (e Entity) GetPrice(location string) EntityPrice

GetPrice returns the price (EntityPrice) of the entity for the given location. If no price is found for the given location, an empty EntityPrice is returned.

func (Entity) String

func (e Entity) String() string

type EntityPrice

type EntityPrice struct {
	ID        int64
	EntityID  int64  `gorm:"not null;"`
	Location  string `gorm:"not null;"`
	UpdatedAt time.Time
	Price     float64  `gorm:"not null;default:0"`
	Currency  Currency `gorm:"not null;default:1"`
}

EntityPrice represents a price for a specific Entity for a given location.

func UpdateEntityPrice

func UpdateEntityPrice(entity Entity, location string) (EntityPrice, error)

UpdateEntityPrice returns an updated EntityPrice struct from a given input Entity

func (EntityPrice) String

func (e EntityPrice) String() string

type EntityType

type EntityType int
const (
	Product  EntityType = 1
	Wishlist EntityType = 2
)

type EntityURL

type EntityURL struct {
	SubmittedURL string
	CleanURL     string
	Path         string
	Location     string
	Locations    []string
	EntityID     int64
	Type         EntityType
}

type Price

type Price struct {
	Price    float64
	Currency Currency
}

type PriceEntry

type PriceEntry struct {
	Price     float64   `json:"price"`
	Timestamp time.Time `json:"ts"`
	Valid     bool      `json:"valid"`
}

func (*PriceEntry) UnmarshalJSON

func (entry *PriceEntry) UnmarshalJSON(p []byte) error

UnmarshalJSON implements a custom unmarshaller for the price history response. The API response is an array of length 3, containing the timestamp, price and validity.

type PriceHistory

type PriceHistory struct {
	Meta     PriceHistoryMeta `json:"meta"`
	Response []PriceEntry     `json:"response"`
	Location string           `json:"location"`
}

func DownloadPriceHistory

func DownloadPriceHistory(entityIDs, amounts []int64, location string) (PriceHistory, error)

func GetPriceHistory

func GetPriceHistory(entity Entity, location string) (PriceHistory, error)

GetPriceHistory returns the price history for the given entity either from cache or by downloading it.

type PriceHistoryMeta

type PriceHistoryMeta struct {
	LastFormatted string  `json:"last_formatted"`
	Min           float64 `json:"min"`
	Max           float64 `json:"max"`
	CurrentBest   float64 `json:"current_best"`
	FirstTS       float64 `json:"first_ts"`
	LastTS        float64 `json:"last_ts"`
	DownloadedAt  time.Time
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL