This commit is contained in:
Florian Baumann 2021-12-28 09:46:08 +01:00
parent c39399e9b8
commit 33bb189860
6 changed files with 83 additions and 4 deletions

View File

@ -4,3 +4,12 @@
go build .
./serra
# Todo
add - do search for cards by name
# What its not
* Gives a shit about conditions (NM, M, GD...)
* If the card is foil

View File

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

View File

@ -8,6 +8,8 @@ import (
"io/ioutil"
"log"
"net/http"
"strconv"
"time"
)
type Card struct {
@ -112,9 +114,21 @@ 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"`
}
func fetch(path string) (*Card, error) {
// TODO better URL Building...
resp, err := http.Get(fmt.Sprintf("https://api.scryfall.com/cards/%s/", path))
if err != nil {
log.Fatalln(err)
@ -138,5 +152,13 @@ func fetch(path string) (*Card, error) {
decoder := json.NewDecoder(r)
val := &Card{}
err = decoder.Decode(val)
// Increase counter
val._count = val._count + 1
// Increase Price
v, _ := strconv.ParseFloat(val.Prices.Eur.(string), 32)
val._prices = append(val._prices, PriceEntry{time.Now().Format("2006-01-02 15:04:05"), v})
return val, nil
}

View File

@ -15,14 +15,32 @@ func Add(cards []string) {
client := storage_connect()
coll := client.Database("serra").Collection("cards")
// Loop over different challenges
// Loop over different cards
for _, card := range cards {
// Fetch card from scryfall
c, err := fetch(card)
if err != nil {
LogMessage(fmt.Sprintf("%v", err), "red")
}
storage_add(coll, c)
fmt.Println(c)
// Write card to mongodb
err = storage_add(coll, c)
if err != nil {
LogMessage(fmt.Sprintf("%v", err), "red")
continue
}
LogMessage(fmt.Sprintf("\"%s\" (%s Eur) added to Collection.", c.Name, c.Prices.Eur), "purple")
}
}
func List() {
LogMessage(fmt.Sprintf("Archivar %v\n", version), "green")
client := storage_connect()
coll := client.Database("serra").Collection("cards")
storage_find(coll)
}

View File

@ -6,6 +6,7 @@ import (
"log"
"os"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
@ -35,7 +36,29 @@ func storage_add(coll *mongo.Collection, card *Card) error {
_, err := coll.InsertOne(context.TODO(), card)
if err != nil {
fmt.Println(err)
return err
}
return nil
}
func storage_find(coll *mongo.Collection) error {
opts := options.Find().SetSort(bson.D{{"collectornumber", 1}})
cursor, err := coll.Find(context.TODO(), bson.D{{}}, opts)
if err != nil {
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 []bson.M
if err = cursor.All(context.TODO(), &results); err != nil {
log.Fatal(err)
return err
}
for _, result := range results {
fmt.Println(result)
}
return nil

View File

@ -17,6 +17,8 @@ func LogMessage(message string, color string) {
fmt.Printf("%s%s%s\n", colorRed, message, colorReset)
} else if color == "green" {
fmt.Printf("%s%s%s\n", colorGreen, message, colorReset)
} else if color == "purple" {
fmt.Printf("%s%s%s\n", colorPurple, message, colorReset)
} else {
fmt.Printf("%s\n", message)
}