add sets
This commit is contained in:
parent
dcbe0706a7
commit
2267469da8
6
serra.go
6
serra.go
@ -14,8 +14,8 @@ func main() {
|
|||||||
Usage:
|
Usage:
|
||||||
serra add <card>...
|
serra add <card>...
|
||||||
serra list
|
serra list
|
||||||
|
serra sets
|
||||||
serra update <path>...
|
serra update <path>...
|
||||||
serra value <path>...
|
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-h --help Show this screen.
|
-h --help Show this screen.
|
||||||
@ -32,4 +32,8 @@ Options:
|
|||||||
serra.List()
|
serra.List()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if args["sets"].(bool) {
|
||||||
|
serra.Sets()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,7 +8,6 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -73,12 +72,8 @@ type Card struct {
|
|||||||
OracleText string `json:"oracle_text"`
|
OracleText string `json:"oracle_text"`
|
||||||
Oversized bool `json:"oversized"`
|
Oversized bool `json:"oversized"`
|
||||||
Prices struct {
|
Prices struct {
|
||||||
Eur interface{} `json:"eur"`
|
Eur float64 `json:"eur,string"`
|
||||||
EurFoil interface{} `json:"eur_foil"`
|
EurFoil float64 `json:"eur_foil,string"`
|
||||||
Tix interface{} `json:"tix"`
|
|
||||||
Usd interface{} `json:"usd"`
|
|
||||||
UsdEtched interface{} `json:"usd_etched"`
|
|
||||||
UsdFoil interface{} `json:"usd_foil"`
|
|
||||||
} `json:"prices"`
|
} `json:"prices"`
|
||||||
PrintedName string `json:"printed_name"`
|
PrintedName string `json:"printed_name"`
|
||||||
PrintedText string `json:"printed_text"`
|
PrintedText string `json:"printed_text"`
|
||||||
@ -136,8 +131,7 @@ func fetch(path string) (*Card, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != 200 {
|
if resp.StatusCode != 200 {
|
||||||
LogMessage(fmt.Sprintf("Card %s not found", path), "yellow")
|
err := errors.New(fmt.Sprintf("card: %s not found", path))
|
||||||
err := errors.New("card: not found")
|
|
||||||
return &Card{}, err
|
return &Card{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,8 +151,7 @@ func fetch(path string) (*Card, error) {
|
|||||||
val._count = val._count + 1
|
val._count = val._count + 1
|
||||||
|
|
||||||
// Increase Price
|
// 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"), val.Prices.Eur})
|
||||||
val._prices = append(val._prices, PriceEntry{time.Now().Format("2006-01-02 15:04:05"), v})
|
|
||||||
|
|
||||||
return val, nil
|
return val, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,8 @@ package serra
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -22,6 +24,7 @@ func Add(cards []string) {
|
|||||||
c, err := fetch(card)
|
c, err := fetch(card)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogMessage(fmt.Sprintf("%v", err), "red")
|
LogMessage(fmt.Sprintf("%v", err), "red")
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write card to mongodb
|
// Write card to mongodb
|
||||||
@ -31,7 +34,7 @@ func Add(cards []string) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
LogMessage(fmt.Sprintf("\"%s\" (%s Eur) added to Collection.", c.Name, c.Prices.Eur), "purple")
|
LogMessage(fmt.Sprintf("\"%s\" (%.2f Eur) added to Collection.", c.Name, c.Prices.Eur), "purple")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -43,7 +46,28 @@ func List() {
|
|||||||
coll := client.Database("serra").Collection("cards")
|
coll := client.Database("serra").Collection("cards")
|
||||||
cards, _ := storage_find(coll)
|
cards, _ := storage_find(coll)
|
||||||
for _, card := range cards {
|
for _, card := range cards {
|
||||||
fmt.Printf("%s (%s) %s\n", card.Name, card.Set, card.Prices.Eur)
|
fmt.Printf("%s (%s) %.2f\n", card.Name, card.Set, card.Prices.Eur)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Sets() {
|
||||||
|
LogMessage(fmt.Sprintf("Archivar %v\n", version), "green")
|
||||||
|
|
||||||
|
client := storage_connect()
|
||||||
|
coll := client.Database("serra").Collection("cards")
|
||||||
|
|
||||||
|
groupStage := bson.D{
|
||||||
|
{"$group", bson.D{
|
||||||
|
{"_id", "$setname"},
|
||||||
|
{"sum", bson.D{
|
||||||
|
{"$sum", "$prices.eur"},
|
||||||
|
}},
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
|
||||||
|
sets, _ := storage_aggregate(coll, groupStage)
|
||||||
|
for _, set := range sets {
|
||||||
|
fmt.Printf("* %s (%.2f Eur)\n", set["_id"], set["sum"])
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,8 +4,10 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
@ -59,3 +61,25 @@ func storage_find(coll *mongo.Collection) ([]Card, error) {
|
|||||||
return results, nil
|
return results, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func storage_aggregate(coll *mongo.Collection, groupstage bson.D) ([]primitive.M, error) {
|
||||||
|
|
||||||
|
// db.cards.aggregate([ {$group: { _id: "$setname", sum: { $sum: "$prices.eur"}}}])
|
||||||
|
opts := options.Aggregate().SetMaxTime(2 * time.Second)
|
||||||
|
cursor, err := coll.Aggregate(
|
||||||
|
context.TODO(),
|
||||||
|
mongo.Pipeline{groupstage},
|
||||||
|
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 results, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user