stappabot/scryfall.go

67 lines
1.8 KiB
Go
Raw Normal View History

2018-09-17 10:26:24 +00:00
package main
import (
"encoding/json"
"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"`
}
type CardData struct {
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 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"`
} `json:"image_uris"`
Eur string `json:"eur"`
2018-09-17 12:19:11 +00:00
EdhrecRank int `json:"edhrec_rank"`
2018-09-17 10:26:24 +00:00
RelatedUris struct {
Gatherer string `json:"gatherer"`
TcgplayerDecks string `json:"tcgplayer_decks"`
Edhrec string `json:"edhrec"`
Mtgtop8 string `json:"mtgtop8"`
} `json:"related_uris"`
PurchaseUris struct {
Amazon string `json:"amazon"`
Ebay string `json:"ebay"`
Tcgplayer string `json:"tcgplayer"`
Magiccardmarket string `json:"magiccardmarket"`
Cardhoarder string `json:"cardhoarder"`
CardKingdom string `json:"card_kingdom"`
MtgoTraders string `json:"mtgo_traders"`
Coolstuffinc string `json:"coolstuffinc"`
} `json:"purchase_uris"`
}
var netClient = &http.Client{
Timeout: time.Second * 10,
}
func scryfallSearch(query string) (results CardSearchResults, err error) {
query = url.QueryEscape(query)
response, err := netClient.Get("https://api.scryfall.com/cards/search?order=name&q=" + query)
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-17 10:26:24 +00:00
return
}