init work on multi currency
This commit is contained in:
parent
cb726856ae
commit
292a9da383
@ -165,13 +165,26 @@ func print_price_history(prices []PriceEntry, prefix string) {
|
||||
|
||||
var before float64
|
||||
for _, e := range prices {
|
||||
if e.Value > before && before != 0 {
|
||||
fmt.Printf("%s%s%s %.2f EUR%s (%+.2f%%)\n", prefix, stringToTime(e.Date), Green, e.Value, Reset, (e.Value/before*100)-100)
|
||||
} else if e.Value < before {
|
||||
fmt.Printf("%s%s%s %.2f EUR%s (%+.2f%%)\n", prefix, stringToTime(e.Date), Red, e.Value, Reset, (e.Value/before*100)-100)
|
||||
|
||||
// TODO: Make currency configurable
|
||||
value := e.Eur
|
||||
if value > before && before != 0 {
|
||||
fmt.Printf("%s%s%s %.2f EUR%s (%+.2f%%)\n", prefix, stringToTime(e.Date), Green, value, Reset, (value/before*100)-100)
|
||||
} else if value < before {
|
||||
fmt.Printf("%s%s%s %.2f EUR%s (%+.2f%%)\n", prefix, stringToTime(e.Date), Red, value, Reset, (value/before*100)-100)
|
||||
} else {
|
||||
fmt.Printf("%s%s %.2f EUR%s\n", prefix, stringToTime(e.Date), e.Value, Reset)
|
||||
fmt.Printf("%s%s %.2f EUR%s\n", prefix, stringToTime(e.Date), value, Reset)
|
||||
}
|
||||
before = e.Value
|
||||
before = value
|
||||
}
|
||||
}
|
||||
|
||||
// Sometimes its exhausting dealing with those types...
|
||||
// Usecase: price can be 3.14 or nil
|
||||
func toFloat(p interface{}) float64 {
|
||||
fmt.Printf("%v\n", p)
|
||||
if p != nil {
|
||||
return float64(p.(float64))
|
||||
}
|
||||
return float64(0)
|
||||
}
|
||||
|
||||
@ -16,10 +16,12 @@ import (
|
||||
|
||||
type Card struct {
|
||||
// Added by Serra
|
||||
SerraCount int64 `bson:"serra_count"`
|
||||
SerraPrices []PriceEntry `bson:"serra_prices"`
|
||||
SerraCreated primitive.DateTime `bson:"serra_created"`
|
||||
SerraUpdated primitive.DateTime `bson:"serra_updated"`
|
||||
SerraCount int64 `bson:"serra_count"`
|
||||
SerraCountFoil int64 `bson:"serra_count_foil"`
|
||||
SerraCountEtched int64 `bson:"serra_count_etched"`
|
||||
SerraPrices []PriceEntry `bson:"serra_prices"`
|
||||
SerraCreated primitive.DateTime `bson:"serra_created"`
|
||||
SerraUpdated primitive.DateTime `bson:"serra_updated"`
|
||||
|
||||
Artist string `json:"artist"`
|
||||
ArtistIds []string `json:"artist_ids"`
|
||||
@ -72,27 +74,20 @@ type Card struct {
|
||||
Standard string `json:"standard"`
|
||||
Vintage string `json:"vintage"`
|
||||
} `json:"legalities"`
|
||||
ManaCost string `json:"mana_cost"`
|
||||
MultiverseIds []interface{} `json:"multiverse_ids"`
|
||||
Name string `json:"name"`
|
||||
Nonfoil bool `json:"nonfoil"`
|
||||
Object string `json:"object"`
|
||||
OracleID string `json:"oracle_id"`
|
||||
OracleText string `json:"oracle_text"`
|
||||
Oversized bool `json:"oversized"`
|
||||
Prices struct {
|
||||
Eur float64 `json:"eur,string"`
|
||||
EurFoil float64 `json:"eur_foil,string"`
|
||||
Tix float64 `json:"tix,string"`
|
||||
Usd float64 `json:"usd,string"`
|
||||
UsdEtched float64 `json:"usd_etched,string"`
|
||||
UsdFoil float64 `json:"usd_foil,string"`
|
||||
} `json:"prices"`
|
||||
PrintedName string `json:"printed_name"`
|
||||
PrintedText string `json:"printed_text"`
|
||||
PrintedTypeLine string `json:"printed_type_line"`
|
||||
PrintsSearchURI string `json:"prints_search_uri"`
|
||||
Promo bool `json:"promo"`
|
||||
ManaCost string `json:"mana_cost"`
|
||||
MultiverseIds []interface{} `json:"multiverse_ids"`
|
||||
Name string `json:"name"`
|
||||
Nonfoil bool `json:"nonfoil"`
|
||||
Object string `json:"object"`
|
||||
OracleID string `json:"oracle_id"`
|
||||
OracleText string `json:"oracle_text"`
|
||||
Oversized bool `json:"oversized"`
|
||||
Prices PriceEntry `json:"prices"`
|
||||
PrintedName string `json:"printed_name"`
|
||||
PrintedText string `json:"printed_text"`
|
||||
PrintedTypeLine string `json:"printed_type_line"`
|
||||
PrintsSearchURI string `json:"prints_search_uri"`
|
||||
Promo bool `json:"promo"`
|
||||
PurchaseUris struct {
|
||||
Cardhoarder string `json:"cardhoarder"`
|
||||
Cardmarket string `json:"cardmarket"`
|
||||
@ -125,8 +120,13 @@ type Card struct {
|
||||
}
|
||||
|
||||
type PriceEntry struct {
|
||||
Date primitive.DateTime `bson:"date"`
|
||||
Value float64 `bson:"value"`
|
||||
Date primitive.DateTime `bson:"date"`
|
||||
Eur float64 `json:"eur,string"`
|
||||
EurFoil float64 `json:"eur_foil,string"`
|
||||
Tix float64 `json:"tix,string"`
|
||||
Usd float64 `json:"usd,string"`
|
||||
UsdEtched float64 `json:"usd_etched,string"`
|
||||
UsdFoil float64 `json:"usd_foil,string"`
|
||||
}
|
||||
|
||||
// Sets
|
||||
@ -191,7 +191,8 @@ func fetch_card(path string) (*Card, error) {
|
||||
val.SerraCreated = primitive.NewDateTimeFromTime(time.Now())
|
||||
|
||||
// Increase Price
|
||||
val.SerraPrices = append(val.SerraPrices, PriceEntry{primitive.NewDateTimeFromTime(time.Now()), val.Prices.Eur})
|
||||
val.Prices.Date = primitive.NewDateTimeFromTime(time.Now())
|
||||
val.SerraPrices = append(val.SerraPrices, val.Prices)
|
||||
|
||||
return val, nil
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ package serra
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/schollz/progressbar/v3"
|
||||
@ -67,10 +68,11 @@ var updateCmd = &cobra.Command{
|
||||
continue
|
||||
}
|
||||
|
||||
updated_card.Prices.Date = primitive.NewDateTimeFromTime(time.Now())
|
||||
|
||||
update := bson.M{
|
||||
"$set": bson.M{"serra_updated": primitive.NewDateTimeFromTime(time.Now()), "prices": updated_card.Prices, "collectornumber": updated_card.CollectorNumber},
|
||||
"$push": bson.M{"serra_prices": bson.M{"date": primitive.NewDateTimeFromTime(time.Now()),
|
||||
"value": updated_card.Prices.Eur}},
|
||||
"$set": bson.M{"serra_updated": primitive.NewDateTimeFromTime(time.Now()), "prices": updated_card.Prices, "collectornumber": updated_card.CollectorNumber},
|
||||
"$push": bson.M{"serra_prices": updated_card.Prices},
|
||||
}
|
||||
coll.storage_update(bson.M{"_id": bson.M{"$eq": card.ID}}, update)
|
||||
}
|
||||
@ -84,23 +86,47 @@ var updateCmd = &cobra.Command{
|
||||
{"set", set.Code},
|
||||
}},
|
||||
}
|
||||
// Eur float64 `json:"eur,string"`
|
||||
// EurFoil float64 `json:"eur_foil,string"`
|
||||
// Tix float64 `json:"tix,string"`
|
||||
// Usd float64 `json:"usd,string"`
|
||||
// UsdEtched float64 `json:"usd_etched,string"`
|
||||
// UsdFoil float64 `json:"usd_foil,string"`
|
||||
groupStage := bson.D{
|
||||
{"$group", bson.D{
|
||||
{"_id", "$set"},
|
||||
{"value", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.eur", "$serra_count"}}}}}},
|
||||
{"eur", bson.D{{"$toDouble", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.eur", "$serra_count"}}}}}}}},
|
||||
{"eurfoil", bson.D{{"$toDouble", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.eurfoil", "$serra_count_foil"}}}}}}}},
|
||||
{"usd", bson.D{{"$toDouble", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.usd", "$serra_count_foil"}}}}}}}},
|
||||
{"usdetched", bson.D{{"$toDouble", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.usdetched", "$serra_count_etched"}}}}}}}},
|
||||
{"usdfoil", bson.D{{"$toDouble", bson.D{{"$sum", bson.D{{"$multiply", bson.A{"$prices.usd", "$serra_count_foil"}}}}}}}},
|
||||
}}}
|
||||
setvalue, _ := coll.storage_aggregate(mongo.Pipeline{matchStage, groupStage})
|
||||
setvalue, err := coll.storage_aggregate(mongo.Pipeline{matchStage, groupStage})
|
||||
if err != nil {
|
||||
LogMessage(fmt.Sprintf("%s", err), "red")
|
||||
}
|
||||
|
||||
p := PriceEntry{}
|
||||
s := setvalue[0]
|
||||
|
||||
p.Date = primitive.NewDateTimeFromTime(time.Now())
|
||||
p.Eur = toFloat(s["eur"])
|
||||
p.EurFoil = toFloat(s["eurfoil"])
|
||||
p.Usd = toFloat(s["usd"])
|
||||
p.UsdEtched = toFloat(s["usdetched"])
|
||||
p.UsdFoil = toFloat(s["usdfoil"])
|
||||
|
||||
// do the update
|
||||
set_update := bson.M{
|
||||
"$set": bson.M{"serra_updated": primitive.NewDateTimeFromTime(time.Now())},
|
||||
"$push": bson.M{"serra_prices": bson.M{"date": primitive.NewDateTimeFromTime(time.Now()),
|
||||
"value": setvalue[0]["value"]}},
|
||||
"$set": bson.M{"serra_updated": p.Date},
|
||||
"$push": bson.M{"serra_prices": p},
|
||||
}
|
||||
// fmt.Printf("Set %s%s%s (%s) is now worth %s%.02f EUR%s\n", Pink, set.Name, Reset, set.Code, Yellow, setvalue[0]["value"], Reset)
|
||||
setscoll.storage_update(bson.M{"code": bson.M{"$eq": set.Code}}, set_update)
|
||||
}
|
||||
|
||||
os.Exit(1)
|
||||
|
||||
// calculate total summary over all sets
|
||||
overall_value := mongo.Pipeline{
|
||||
bson.D{{"$match",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user