mlpcardbot/mlpapi1.go

57 lines
1.2 KiB
Go
Raw Normal View History

2019-08-20 12:36:26 +00:00
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)
type CardSearchResults struct {
Data []CardData `json:"data"`
}
type CardSearchResultsSingle struct {
Data CardData `json:"data"`
}
type CardData struct {
AllIDs []string `json:"allids"`
GUID int64 `json:"card_version_guid"`
FullName string `json:"fullname"`
Set string `json:"set"`
Type string `json:"type"`
}
var netClient = &http.Client{
Timeout: time.Second * 20,
}
func mlpapiSearch(query string) (results CardSearchResults, err error) {
query = url.QueryEscape(query)
requrl := fmt.Sprintf("https://www.ferrictorus.com/mlpapi1/cards?query=%s", query)
response, err := netClient.Get(requrl)
if err != nil {
return
}
defer response.Body.Close()
err = json.NewDecoder(response.Body).Decode(&results)
return
}
func mlpapiGetCardByID(id string) (card CardData, err error) {
requrl := fmt.Sprintf("https://www.ferrictorus.com/mlpapi1/cards/%s", id)
response, err := netClient.Get(requrl)
if err != nil {
return
}
defer response.Body.Close()
var res CardSearchResultsSingle
err = json.NewDecoder(response.Body).Decode(&res)
card = res.Data
return
}