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
|
package cardsinfo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
mtg "github.com/MagicTheGathering/mtg-sdk-go"
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const SCRYFALL_URL = "https://api.scryfall.com"
|
||||||
|
|
||||||
func GetNameByCardId(set string, number string) string {
|
func GetNameByCardId(set string, number string) string {
|
||||||
/*
|
/*
|
||||||
From https://docs.magicthegathering.io/#api_v1cards_list
|
Note: number is string because some cards contain letters in their numbers.
|
||||||
|
|
||||||
Number: This is a string, not an integer,
|
|
||||||
because some cards have letters in their numbers.
|
|
||||||
*/
|
*/
|
||||||
cards, _, _ := mtg.NewQuery().Where(mtg.CardSet, set).Where(mtg.CardNumber, number).PageS(1, 1)
|
path := SCRYFALL_URL + "/cards/" + strings.ToLower(set) + "/" + number
|
||||||
name := fetchCardNameFromSlice(cards)
|
return GetCardByUrl(path)
|
||||||
return name
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetOriginalName(name string) string {
|
func GetOriginalName(name string) string {
|
||||||
langs := []string{"Russian", ""}
|
path := SCRYFALL_URL + "/cards/named?fuzzy=" + url.QueryEscape(name)
|
||||||
channel := make(chan string)
|
return GetCardByUrl(path)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getOriginalNameFromLang(name, lang string, channel chan string) {
|
func GetCardByUrl(path string) string {
|
||||||
cards, _, _ := mtg.NewQuery().Where(mtg.CardLanguage, lang).Where(mtg.CardName, name).PageS(1, 1)
|
response, err := http.Get(path)
|
||||||
originalName := fetchCardNameFromSlice(cards)
|
if err != nil {
|
||||||
channel <- originalName
|
|
||||||
}
|
|
||||||
|
|
||||||
func fetchCardNameFromSlice(cards []*mtg.Card) string {
|
|
||||||
if len(cards) > 0 {
|
|
||||||
name := getCardName(cards[0])
|
|
||||||
return name
|
|
||||||
}
|
|
||||||
return ""
|
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
|
package cardsinfo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
type CardPrice struct {
|
type CardPrice struct {
|
||||||
Name string
|
Name string
|
||||||
Price float64
|
Price float64
|
||||||
Link string
|
Link string
|
||||||
Edition 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