This commit is contained in:
Florian Baumann 2022-01-04 18:00:31 +01:00
parent 5a3db4b0a2
commit bcdf6769bc
3 changed files with 51 additions and 28 deletions

View File

@ -142,12 +142,25 @@ func ShowSet(setname string) error {
} }
func Update() { func Update() error {
LogMessage(fmt.Sprintf("Serra %v\n", version), "green") LogMessage(fmt.Sprintf("Serra %v\n", version), "green")
client := storage_connect() client := storage_connect()
coll := &Collection{client.Database("serra").Collection("cards")}
// update sets
setscoll := &Collection{client.Database("serra").Collection("sets")}
sets, _ := fetch_sets()
for _, set := range sets.Data {
// setscoll.storage_remove(bson.M{"_id": ""})
// TODO: lol, no errorhandling, no dup key handling. but its fine. for now.
setscoll.storage_add_set(&set)
}
return nil
// update cards
coll := &Collection{client.Database("serra").Collection("cards")}
sort := bson.D{{"_id", 1}} sort := bson.D{{"_id", 1}}
filter := bson.D{{}} filter := bson.D{{}}
cards, _ := coll.storage_find(filter, sort) cards, _ := coll.storage_find(filter, sort)
@ -155,8 +168,6 @@ func Update() {
for i, card := range cards { for i, card := range cards {
fmt.Printf("Updating (%d/%d): %s (%s)...\n", i+1, len(cards), card.Name, card.SetName) fmt.Printf("Updating (%d/%d): %s (%s)...\n", i+1, len(cards), card.Name, card.SetName)
// TODO fetch new card
updated_card, err := fetch_card(fmt.Sprintf("%s/%d", card.Set, card.CollectorNumber)) updated_card, err := fetch_card(fmt.Sprintf("%s/%d", card.Set, card.CollectorNumber))
if err != nil { if err != nil {
LogMessage(fmt.Sprintf("%v", err), "red") LogMessage(fmt.Sprintf("%v", err), "red")
@ -174,6 +185,7 @@ func Update() {
} }
storage_disconnect(client) storage_disconnect(client)
return nil
} }
func Stats() { func Stats() {
@ -195,16 +207,16 @@ func Stats() {
fmt.Printf("* %s %d\n", set["_id"], set["count"]) fmt.Printf("* %s %d\n", set["_id"], set["count"])
} }
LogMessage(fmt.Sprintf("Mana costs in Collection"), "green") // LogMessage(fmt.Sprintf("Mana costs in Collection"), "green")
groupStage = bson.D{ // groupStage = bson.D{
{"$group", bson.D{ // {"$group", bson.D{
{"_id", "$manacost"}, // {"_id", "$manacost"},
{"count", bson.D{{"$sum", 1}}}, // {"count", bson.D{{"$sum", 1}}},
}}} // }}}
m, _ := coll.storage_aggregate(groupStage) // m, _ := coll.storage_aggregate(groupStage)
for _, manacosts := range m { // for _, manacosts := range m {
// TODO fix primitiveA Problem with loop and reflect // // TODO fix primitiveA Problem with loop and reflect
fmt.Printf("* %s %d\n", manacosts["_id"], manacosts["count"]) // fmt.Printf("* %s %d\n", manacosts["_id"], manacosts["count"])
} // }
} }

View File

@ -124,21 +124,22 @@ type PriceEntry struct {
Value float64 `bson:"value"` Value float64 `bson:"value"`
} }
// Sets
type SetList struct {
Data []Set `json:"data"`
}
type Set struct { type Set struct {
ArenaCode string `json:"arena_code"`
Block string `json:"block"`
BlockCode string `json:"block_code"`
CardCount int64 `json:"card_count"` CardCount int64 `json:"card_count"`
Code string `json:"code"` Code string `json:"code"`
Digital bool `json:"digital"` Digital bool `json:"digital"`
FoilOnly bool `json:"foil_only"` FoilOnly bool `json:"foil_only"`
IconSvgURI string `json:"icon_svg_uri"` IconSvgURI string `json:"icon_svg_uri"`
ID string `json:"id"` ID string `json:"id" bson:"_id"`
MtgoCode string `json:"mtgo_code"`
Name string `json:"name"` Name string `json:"name"`
NonfoilOnly bool `json:"nonfoil_only"` NonfoilOnly bool `json:"nonfoil_only"`
Object string `json:"object"` Object string `json:"object"`
PrintedSize int64 `json:"printed_size"`
ReleasedAt string `json:"released_at"` ReleasedAt string `json:"released_at"`
ScryfallURI string `json:"scryfall_uri"` ScryfallURI string `json:"scryfall_uri"`
SearchURI string `json:"search_uri"` SearchURI string `json:"search_uri"`
@ -184,29 +185,29 @@ func fetch_card(path string) (*Card, error) {
return val, nil return val, nil
} }
func fetch_set(path string) (*Set, error) { func fetch_sets() (*SetList, error) {
// TODO better URL Building... // TODO better URL Building...
resp, err := http.Get(fmt.Sprintf("https://api.scryfall.com/sets/%s/", path)) resp, err := http.Get(fmt.Sprintf("https://api.scryfall.com/sets"))
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
return &Set{}, err return &SetList{}, err
} }
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
err := errors.New(fmt.Sprintf("Error: %s not found", path)) err := errors.New(fmt.Sprintf("Error: /sets not found"))
return &Set{}, err return &SetList{}, err
} }
//We Read the response body on the line below. //We Read the response body on the line below.
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
return &Set{}, err return &SetList{}, err
} }
r := bytes.NewReader(body) r := bytes.NewReader(body)
decoder := json.NewDecoder(r) decoder := json.NewDecoder(r)
val := &Set{} val := &SetList{}
err = decoder.Decode(val) err = decoder.Decode(val)
return val, nil return val, nil

View File

@ -44,6 +44,16 @@ func (coll Collection) storage_add(card *Card) error {
} }
func (coll Collection) storage_add_set(set *Set) error {
_, err := coll.InsertOne(context.TODO(), set)
if err != nil {
return err
}
return nil
}
func (coll Collection) storage_find(filter, sort bson.D) ([]Card, error) { func (coll Collection) storage_find(filter, sort bson.D) ([]Card, error) {
opts := options.Find().SetSort(sort) opts := options.Find().SetSort(sort)