diff --git a/serra.go b/serra.go new file mode 100644 index 0000000..9244df9 --- /dev/null +++ b/serra.go @@ -0,0 +1,35 @@ +// Package main provides a typing test +package main + +import ( + "github.com/docopt/docopt-go" + "github.com/noqqe/serra/src/serra" +) + +// Main Loop +func main() { + + usage := `Archivar + +Usage: + serra new ... + serra update ... + serra value ... + +Options: + -h --help Show this screen. + --version Show version. +` + + args, _ := docopt.ParseDoc(usage) + + if args["new"].(bool) { + serra.New(args[""].(string)) + } + if args["update"].(bool) { + for _, i := range args[""].([]string) { + serra.Update(i) + } + } + +} diff --git a/src/serra/card.go b/src/serra/card.go new file mode 100644 index 0000000..9f1e2c6 --- /dev/null +++ b/src/serra/card.go @@ -0,0 +1,140 @@ +package serra + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" +) + +type Card struct { + Artist string `json:"artist"` + ArtistIds []string `json:"artist_ids"` + Booster bool `json:"booster"` + BorderColor string `json:"border_color"` + CardBackID string `json:"card_back_id"` + Cmc int64 `json:"cmc"` + CollectorNumber string `json:"collector_number"` + ColorIdentity []string `json:"color_identity"` + Colors []string `json:"colors"` + Digital bool `json:"digital"` + EdhrecRank int64 `json:"edhrec_rank"` + Finishes []string `json:"finishes"` + Foil bool `json:"foil"` + Frame string `json:"frame"` + FullArt bool `json:"full_art"` + Games []string `json:"games"` + HighresImage bool `json:"highres_image"` + ID string `json:"id"` + IllustrationID string `json:"illustration_id"` + ImageStatus string `json:"image_status"` + ImageUris struct { + ArtCrop string `json:"art_crop"` + BorderCrop string `json:"border_crop"` + Large string `json:"large"` + Normal string `json:"normal"` + Png string `json:"png"` + Small string `json:"small"` + } `json:"image_uris"` + Keywords []interface{} `json:"keywords"` + Lang string `json:"lang"` + Layout string `json:"layout"` + Legalities struct { + Alchemy string `json:"alchemy"` + Brawl string `json:"brawl"` + Commander string `json:"commander"` + Duel string `json:"duel"` + Future string `json:"future"` + Gladiator string `json:"gladiator"` + Historic string `json:"historic"` + Historicbrawl string `json:"historicbrawl"` + Legacy string `json:"legacy"` + Modern string `json:"modern"` + Oldschool string `json:"oldschool"` + Pauper string `json:"pauper"` + Paupercommander string `json:"paupercommander"` + Penny string `json:"penny"` + Pioneer string `json:"pioneer"` + Premodern string `json:"premodern"` + Standard string `json:"standard"` + Vintage string `json:"vintage"` + } `json:"legalities"` + ManaCost string `json:"mana_cost"` + MultiverseIds []interface{} `json:"multiverse_ids"` + Name string `json:"name"` + Nonfoil bool `json:"nonfoil"` + Object string `json:"object"` + OracleID string `json:"oracle_id"` + OracleText string `json:"oracle_text"` + Oversized bool `json:"oversized"` + Prices struct { + Eur interface{} `json:"eur"` + EurFoil interface{} `json:"eur_foil"` + Tix interface{} `json:"tix"` + Usd interface{} `json:"usd"` + UsdEtched interface{} `json:"usd_etched"` + UsdFoil interface{} `json:"usd_foil"` + } `json:"prices"` + PrintedName string `json:"printed_name"` + PrintedText string `json:"printed_text"` + PrintedTypeLine string `json:"printed_type_line"` + PrintsSearchURI string `json:"prints_search_uri"` + Promo bool `json:"promo"` + PurchaseUris struct { + Cardhoarder string `json:"cardhoarder"` + Cardmarket string `json:"cardmarket"` + Tcgplayer string `json:"tcgplayer"` + } `json:"purchase_uris"` + Rarity string `json:"rarity"` + RelatedUris struct { + Edhrec string `json:"edhrec"` + Mtgtop8 string `json:"mtgtop8"` + TcgplayerInfiniteArticles string `json:"tcgplayer_infinite_articles"` + TcgplayerInfiniteDecks string `json:"tcgplayer_infinite_decks"` + } `json:"related_uris"` + ReleasedAt string `json:"released_at"` + Reprint bool `json:"reprint"` + Reserved bool `json:"reserved"` + RulingsURI string `json:"rulings_uri"` + ScryfallSetURI string `json:"scryfall_set_uri"` + ScryfallURI string `json:"scryfall_uri"` + Set string `json:"set"` + SetID string `json:"set_id"` + SetName string `json:"set_name"` + SetSearchURI string `json:"set_search_uri"` + SetType string `json:"set_type"` + SetURI string `json:"set_uri"` + StorySpotlight bool `json:"story_spotlight"` + Textless bool `json:"textless"` + TypeLine string `json:"type_line"` + URI string `json:"uri"` + Variation bool `json:"variation"` +} + +func fetch(path string) (*Card, bool) { + resp, err := http.Get(fmt.Sprintf("https://api.scryfall.com/cards/%s/", path)) + if err != nil { + log.Fatalln(err) + return &Card{}, false + } + + if resp.StatusCode != 200 { + LogMessage(fmt.Sprintf("Card %s not found", path), "yellow") + return &Card{}, false + } + + //We Read the response body on the line below. + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatalln(err) + return &Card{}, false + } + + r := bytes.NewReader(body) + decoder := json.NewDecoder(r) + val := &Card{} + err = decoder.Decode(val) + return val, true +} diff --git a/src/serra/root.go b/src/serra/root.go new file mode 100644 index 0000000..7092e63 --- /dev/null +++ b/src/serra/root.go @@ -0,0 +1,58 @@ +package serra + +import ( + "fmt" + "strconv" + "time" +) + +const ( + version string = "0.0.1" +) + +// Create new set file +func New(set_file string) { + + var s Setfile + s.Write(set_file) +} + +// Update values and names in a setfile +func Update(set_file string) { + + var s Setfile + var total float32 + + s.ReadFile(set_file) + + LogMessage(fmt.Sprintf("Archivar %v\n", version), "green") + + fmt.Printf("Set: %s\n", s.Description) + + // Loop over different challenges + for _, entry := range s.Cards { + card, ok := fetch(entry) + + // catch empty cards + if ok == false { + continue + } + + t, _ := strconv.ParseFloat(card.Prices.Eur.(string), 32) + total = total + float32(t) + time.Sleep(100 * time.Millisecond) + } + + // build new valueset + v := &Value{} + v.Date = time.Now().Format("2006-01-02 15:04:05") + v.Value = total + + // add new valueset to set + s.Value = append(s.Value, *v) + + LogMessage(fmt.Sprintf("Total value in this set %.2f", total), "green") + + s.Write(set_file) + +} diff --git a/src/serra/set.go b/src/serra/set.go new file mode 100644 index 0000000..41c4935 --- /dev/null +++ b/src/serra/set.go @@ -0,0 +1,6 @@ +package serra + +type Set struct { + Description string `description` + Cards []string `cards` +} diff --git a/src/serra/setfile.go b/src/serra/setfile.go new file mode 100644 index 0000000..1e6ec07 --- /dev/null +++ b/src/serra/setfile.go @@ -0,0 +1,54 @@ +package serra + +import ( + "fmt" + "io/ioutil" + "os" + + "gopkg.in/yaml.v2" +) + +type Setfile struct { + Description string `description` + Cards []string `cards` + Value []Value `value` +} + +type Value struct { + Date string + Value float32 +} + +// Read formatted yaml file +func (s *Setfile) ReadFile(path string) *Setfile { + + yamlFile, err := ioutil.ReadFile(path) + if err != nil { + LogMessage("Could not open file", "red") + os.Exit(1) + } + + err = yaml.Unmarshal(yamlFile, s) + if err != nil { + LogMessage(fmt.Sprintf("Unmarshal %v", err), "red") + os.Exit(1) + } + + return s +} + +func (s *Setfile) Write(path string) *Setfile { + data, err := yaml.Marshal(*s) + if err != nil { + LogMessage(fmt.Sprintf("Marshal %v", err), "red") + os.Exit(1) + } + + err = ioutil.WriteFile(path, data, 0644) + if err != nil { + LogMessage("Could not write file", "red") + os.Exit(1) + } + + return s +} diff --git a/src/serra/utils.go b/src/serra/utils.go new file mode 100644 index 0000000..4939e9c --- /dev/null +++ b/src/serra/utils.go @@ -0,0 +1,23 @@ +package serra + +import "fmt" + +const ( + challengeLimiter string = ">> " + colorCyan string = "\033[36m" + colorGreen string = "\033[32m" + colorPurple string = "\033[35m" + colorRed string = "\033[31m" + colorReset string = "\033[0m" +) + +// Colored output on commandline +func LogMessage(message string, color string) { + if color == "red" { + fmt.Printf("%s%s%s\n", colorRed, message, colorReset) + } else if color == "green" { + fmt.Printf("%s%s%s\n", colorGreen, message, colorReset) + } else { + fmt.Printf("%s\n", message) + } +}