This commit is contained in:
Florian Baumann 2021-12-30 21:02:31 +01:00
parent ed96740799
commit c93cde3c7c
5 changed files with 89 additions and 23 deletions

View File

@ -13,7 +13,7 @@ func main() {
Usage:
serra add <card>...
serra list
serra cards
serra sets
serra update
@ -28,8 +28,8 @@ Options:
serra.Add(args["<card>"].([]string))
}
if args["list"].(bool) {
serra.List()
if args["cards"].(bool) {
serra.Cards()
}
if args["sets"].(bool) {

View File

@ -9,9 +9,16 @@ import (
"log"
"net/http"
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type Card struct {
// Added by Serra
SerraCount int64 `bson:"serra_count"`
SerraPrices []PriceEntry `bson:"serra_prices"`
SerraUpdated string `bson:"serra_updated"`
Artist string `json:"artist"`
ArtistIds []string `json:"artist_ids"`
Booster bool `json:"booster"`
@ -109,19 +116,14 @@ type Card struct {
TypeLine string `json:"type_line"`
URI string `json:"uri"`
Variation bool `json:"variation"`
// Added by Serra
_count int64 `bson:"_count"`
_prices []PriceEntry `bson:"_prices"`
_updated string `bson:"_updated"`
}
type PriceEntry struct {
date string `bson:"date"`
value float64 `bson:"value"`
Date primitive.DateTime `bson:"date"`
Value float64 `bson:"value"`
}
func fetch(path string) (*Card, error) {
func fetch_card(path string) (*Card, error) {
// TODO better URL Building...
resp, err := http.Get(fmt.Sprintf("https://api.scryfall.com/cards/%s/", path))
@ -148,10 +150,10 @@ func fetch(path string) (*Card, error) {
err = decoder.Decode(val)
// Increase counter
val._count = val._count + 1
val.SerraCount = val.SerraCount + 1
// Increase Price
val._prices = append(val._prices, PriceEntry{time.Now().Format("2006-01-02 15:04:05"), val.Prices.Eur})
val.SerraPrices = append(val.SerraPrices, PriceEntry{primitive.NewDateTimeFromTime(time.Now()), val.Prices.Eur})
return val, nil
}

View File

@ -21,7 +21,7 @@ func Add(cards []string) {
for _, card := range cards {
// Fetch card from scryfall
c, err := fetch(card)
c, err := fetch_card(card)
if err != nil {
LogMessage(fmt.Sprintf("%v", err), "red")
continue
@ -36,10 +36,11 @@ func Add(cards []string) {
LogMessage(fmt.Sprintf("\"%s\" (%.2f Eur) added to Collection.", c.Name, c.Prices.Eur), "purple")
}
storage_disconnect(client)
}
func List() {
func Cards() {
LogMessage(fmt.Sprintf("Serra %v\n", version), "green")
client := storage_connect()
@ -48,6 +49,7 @@ func List() {
for _, card := range cards {
fmt.Printf("%s (%s) %.2f\n", card.Name, card.Set, card.Prices.Eur)
}
storage_disconnect(client)
}
func Sets() {
@ -69,5 +71,6 @@ func Sets() {
for _, set := range sets {
fmt.Printf("* %s (%.2f Eur)\n", set["_id"], set["sum"])
}
storage_disconnect(client)
}

62
src/serra/sets.go Normal file
View File

@ -0,0 +1,62 @@
package serra
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
)
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"`
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"`
SetType string `json:"set_type"`
TcgplayerID int64 `json:"tcgplayer_id"`
URI string `json:"uri"`
}
func fetch_set(path string) (*Set, error) {
// TODO better URL Building...
resp, err := http.Get(fmt.Sprintf("https://api.scryfall.com/sets/%s/", path))
if err != nil {
log.Fatalln(err)
return &Set{}, err
}
if resp.StatusCode != 200 {
err := errors.New(fmt.Sprintf("set: %s not found", path))
return &Set{}, 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
}
r := bytes.NewReader(body)
decoder := json.NewDecoder(r)
val := &Set{}
err = decoder.Decode(val)
return val, nil
}

View File

@ -24,12 +24,6 @@ func storage_connect() *mongo.Client {
panic(err)
}
// defer func() {
// if err := client.Disconnect(context.TODO()); err != nil {
// panic(err)
// }
// }()
return client
}
@ -51,8 +45,6 @@ func storage_find(coll *mongo.Collection) ([]Card, error) {
log.Fatal(err)
}
// Get a list of all returned documents and print them out.
// See the mongo.Cursor documentation for more examples of using cursors.
var results []Card
if err = cursor.All(context.TODO(), &results); err != nil {
log.Fatal(err)
@ -83,3 +75,10 @@ func storage_aggregate(coll *mongo.Collection, groupstage bson.D) ([]primitive.M
return results, nil
}
func storage_disconnect(client *mongo.Client) error {
if err := client.Disconnect(context.TODO()); err != nil {
return err
}
return nil
}