2018-09-17 10:26:24 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-09-17 13:15:19 +00:00
|
|
|
"fmt"
|
2018-09-17 10:26:24 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CardSearchResults struct {
|
|
|
|
TotalCards int `json:"total_cards"`
|
|
|
|
HasMore bool `json:"has_more"`
|
|
|
|
Data []CardData `json:"data"`
|
|
|
|
NextPage string `json:"next_page"`
|
|
|
|
}
|
|
|
|
|
2018-12-13 11:01:24 +00:00
|
|
|
type CardImage struct {
|
|
|
|
Small string `json:"small"`
|
|
|
|
Normal string `json:"normal"`
|
|
|
|
Large string `json:"large"`
|
|
|
|
Png string `json:"png"`
|
|
|
|
ArtCrop string `json:"art_crop"`
|
|
|
|
BorderCrop string `json:"border_crop"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CardFaces struct {
|
|
|
|
ImageUris CardImage `json:"image_uris"`
|
|
|
|
}
|
|
|
|
|
2018-09-17 10:26:24 +00:00
|
|
|
type CardData struct {
|
2018-12-13 11:01:24 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
URI string `json:"uri"`
|
|
|
|
ScryfallURI string `json:"scryfall_uri"`
|
|
|
|
Layout string `json:"layout"`
|
|
|
|
HighresImage bool `json:"highres_image"`
|
|
|
|
ImageUris CardImage `json:"image_uris"`
|
|
|
|
CardFaces []CardFaces `json:"card_faces"`
|
|
|
|
Eur string `json:"eur"`
|
|
|
|
EdhrecRank *int `json:"edhrec_rank"`
|
|
|
|
RelatedUris struct {
|
2018-09-17 10:26:24 +00:00
|
|
|
Gatherer string `json:"gatherer"`
|
|
|
|
TcgplayerDecks string `json:"tcgplayer_decks"`
|
|
|
|
Edhrec string `json:"edhrec"`
|
|
|
|
Mtgtop8 string `json:"mtgtop8"`
|
|
|
|
} `json:"related_uris"`
|
|
|
|
PurchaseUris struct {
|
2018-12-13 10:22:10 +00:00
|
|
|
Amazon string `json:"amazon"`
|
|
|
|
Ebay string `json:"ebay"`
|
|
|
|
Tcgplayer string `json:"tcgplayer"`
|
|
|
|
Cardmarket string `json:"cardmarket"`
|
|
|
|
Cardhoarder string `json:"cardhoarder"`
|
|
|
|
CardKingdom string `json:"card_kingdom"`
|
|
|
|
MtgoTraders string `json:"mtgo_traders"`
|
|
|
|
Coolstuffinc string `json:"coolstuffinc"`
|
2018-09-17 10:26:24 +00:00
|
|
|
} `json:"purchase_uris"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var netClient = &http.Client{
|
2018-09-17 13:32:31 +00:00
|
|
|
Timeout: time.Second * 20,
|
2018-09-17 10:26:24 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:15:19 +00:00
|
|
|
func scryfallSearch(query string, offset int) (results CardSearchResults, err error) {
|
2018-09-17 10:26:24 +00:00
|
|
|
query = url.QueryEscape(query)
|
2018-09-17 13:15:19 +00:00
|
|
|
page := offset / 175
|
|
|
|
cardoffset := offset % 175
|
|
|
|
requrl := fmt.Sprintf("https://api.scryfall.com/cards/search?include_multilingual=true&order=name&page=%d&q=%s", page, query)
|
|
|
|
response, err := netClient.Get(requrl)
|
2018-09-17 10:26:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
2018-09-17 12:02:10 +00:00
|
|
|
err = json.NewDecoder(response.Body).Decode(&results)
|
2018-09-18 08:31:02 +00:00
|
|
|
maxoffset := cardoffset + 50
|
|
|
|
if maxoffset > len(results.Data) {
|
|
|
|
maxoffset = len(results.Data)
|
|
|
|
}
|
|
|
|
results.Data = results.Data[cardoffset:maxoffset]
|
2018-09-17 10:26:24 +00:00
|
|
|
return
|
|
|
|
}
|
2018-10-16 13:33:48 +00:00
|
|
|
|
2018-12-13 12:55:39 +00:00
|
|
|
func scryfallGetCardByName(cardname string) (card CardData, err error) {
|
2018-10-16 13:33:48 +00:00
|
|
|
cardname = url.QueryEscape(cardname)
|
2018-10-16 13:39:35 +00:00
|
|
|
requrl := fmt.Sprintf("https://api.scryfall.com/cards/named?fuzzy=%s", cardname)
|
2018-10-16 13:33:48 +00:00
|
|
|
response, err := netClient.Get(requrl)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&card)
|
|
|
|
return
|
|
|
|
}
|
2018-12-13 12:55:39 +00:00
|
|
|
|
|
|
|
func scryfallGetCardByID(id string) (card CardData, err error) {
|
|
|
|
requrl := fmt.Sprintf("https://api.scryfall.com/cards/%s", id)
|
|
|
|
response, err := netClient.Get(requrl)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&card)
|
|
|
|
return
|
|
|
|
}
|