diff --git a/src/serra/root.go b/src/serra/root.go index fab992d..5a6415d 100644 --- a/src/serra/root.go +++ b/src/serra/root.go @@ -142,12 +142,25 @@ func ShowSet(setname string) error { } -func Update() { +func Update() error { LogMessage(fmt.Sprintf("Serra %v\n", version), "green") 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}} filter := bson.D{{}} cards, _ := coll.storage_find(filter, sort) @@ -155,8 +168,6 @@ func Update() { for i, card := range cards { 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)) if err != nil { LogMessage(fmt.Sprintf("%v", err), "red") @@ -174,6 +185,7 @@ func Update() { } storage_disconnect(client) + return nil } func Stats() { @@ -195,16 +207,16 @@ func Stats() { fmt.Printf("* %s %d\n", set["_id"], set["count"]) } - LogMessage(fmt.Sprintf("Mana costs in Collection"), "green") - groupStage = bson.D{ - {"$group", bson.D{ - {"_id", "$manacost"}, - {"count", bson.D{{"$sum", 1}}}, - }}} - m, _ := coll.storage_aggregate(groupStage) + // LogMessage(fmt.Sprintf("Mana costs in Collection"), "green") + // groupStage = bson.D{ + // {"$group", bson.D{ + // {"_id", "$manacost"}, + // {"count", bson.D{{"$sum", 1}}}, + // }}} + // m, _ := coll.storage_aggregate(groupStage) - for _, manacosts := range m { - // TODO fix primitiveA Problem with loop and reflect - fmt.Printf("* %s %d\n", manacosts["_id"], manacosts["count"]) - } + // for _, manacosts := range m { + // // TODO fix primitiveA Problem with loop and reflect + // fmt.Printf("* %s %d\n", manacosts["_id"], manacosts["count"]) + // } } diff --git a/src/serra/scryfall.go b/src/serra/scryfall.go index c964eb6..ec20544 100644 --- a/src/serra/scryfall.go +++ b/src/serra/scryfall.go @@ -124,21 +124,22 @@ type PriceEntry struct { Value float64 `bson:"value"` } +// Sets + +type SetList struct { + Data []Set `json:"data"` +} + type Set struct { - ArenaCode string `json:"arena_code"` - Block string `json:"block"` - BlockCode string `json:"block_code"` CardCount int64 `json:"card_count"` Code string `json:"code"` Digital bool `json:"digital"` FoilOnly bool `json:"foil_only"` IconSvgURI string `json:"icon_svg_uri"` - ID string `json:"id"` - MtgoCode string `json:"mtgo_code"` + ID string `json:"id" bson:"_id"` Name string `json:"name"` NonfoilOnly bool `json:"nonfoil_only"` Object string `json:"object"` - PrintedSize int64 `json:"printed_size"` ReleasedAt string `json:"released_at"` ScryfallURI string `json:"scryfall_uri"` SearchURI string `json:"search_uri"` @@ -184,29 +185,29 @@ func fetch_card(path string) (*Card, error) { return val, nil } -func fetch_set(path string) (*Set, error) { +func fetch_sets() (*SetList, error) { // 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 { log.Fatalln(err) - return &Set{}, err + return &SetList{}, err } if resp.StatusCode != 200 { - err := errors.New(fmt.Sprintf("Error: %s not found", path)) - return &Set{}, err + err := errors.New(fmt.Sprintf("Error: /sets not found")) + return &SetList{}, err } //We Read the response body on the line below. body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatalln(err) - return &Set{}, err + return &SetList{}, err } r := bytes.NewReader(body) decoder := json.NewDecoder(r) - val := &Set{} + val := &SetList{} err = decoder.Decode(val) return val, nil diff --git a/src/serra/storage.go b/src/serra/storage.go index 973aa05..e6abde3 100644 --- a/src/serra/storage.go +++ b/src/serra/storage.go @@ -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) { opts := options.Find().SetSort(sort)