Changed API to ScryFall
This commit is contained in:
parent
002c175af1
commit
4ca07883a9
2 changed files with 38 additions and 42 deletions
|
|
@ -1,59 +1,39 @@
|
|||
package cardsinfo
|
||||
|
||||
import (
|
||||
mtg "github.com/MagicTheGathering/mtg-sdk-go"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const SCRYFALL_URL = "https://api.scryfall.com"
|
||||
|
||||
func GetNameByCardId(set string, number string) string {
|
||||
/*
|
||||
From https://docs.magicthegathering.io/#api_v1cards_list
|
||||
|
||||
Number: This is a string, not an integer,
|
||||
because some cards have letters in their numbers.
|
||||
Note: number is string because some cards contain letters in their numbers.
|
||||
*/
|
||||
cards, _, _ := mtg.NewQuery().Where(mtg.CardSet, set).Where(mtg.CardNumber, number).PageS(1, 1)
|
||||
name := fetchCardNameFromSlice(cards)
|
||||
return name
|
||||
path := SCRYFALL_URL + "/cards/" + strings.ToLower(set) + "/" + number
|
||||
return GetCardByUrl(path)
|
||||
}
|
||||
|
||||
func GetOriginalName(name string) string {
|
||||
langs := []string{"Russian", ""}
|
||||
channel := make(chan string)
|
||||
for i := range langs {
|
||||
go getOriginalNameFromLang(name, langs[i], channel)
|
||||
}
|
||||
var res string
|
||||
for i := 0; i < len(langs); i++ {
|
||||
name := <-channel
|
||||
if name != "" {
|
||||
res = name
|
||||
}
|
||||
}
|
||||
return res
|
||||
path := SCRYFALL_URL + "/cards/named?fuzzy=" + url.QueryEscape(name)
|
||||
return GetCardByUrl(path)
|
||||
}
|
||||
|
||||
func getOriginalNameFromLang(name, lang string, channel chan string) {
|
||||
cards, _, _ := mtg.NewQuery().Where(mtg.CardLanguage, lang).Where(mtg.CardName, name).PageS(1, 1)
|
||||
originalName := fetchCardNameFromSlice(cards)
|
||||
channel <- originalName
|
||||
}
|
||||
|
||||
func fetchCardNameFromSlice(cards []*mtg.Card) string {
|
||||
if len(cards) > 0 {
|
||||
name := getCardName(cards[0])
|
||||
return name
|
||||
}
|
||||
func GetCardByUrl(path string) string {
|
||||
response, err := http.Get(path)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
func getCardName(card *mtg.Card) string {
|
||||
switch card.Layout {
|
||||
case "split":
|
||||
return strings.Join(card.Names, " // ")
|
||||
case "transform":
|
||||
return strings.Join(card.Names, " | ")
|
||||
default:
|
||||
return card.Name
|
||||
}
|
||||
defer response.Body.Close()
|
||||
data, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
var v Card
|
||||
json.Unmarshal(data, &v)
|
||||
return v.getName()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,24 @@
|
|||
package cardsinfo
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type CardPrice struct {
|
||||
Name string
|
||||
Price float64
|
||||
Link string
|
||||
Edition string
|
||||
}
|
||||
|
||||
type Card struct {
|
||||
Name string `json:"name"`
|
||||
Layout string `json:"layout"`
|
||||
}
|
||||
|
||||
func (c *Card) getName() string {
|
||||
if c.Layout == "transform" {
|
||||
return strings.Replace(c.Name, "//", "|", 1)
|
||||
}
|
||||
return c.Name
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue