mlpcardbot/mlpapi1.go
2019-08-20 14:57:57 +02:00

60 lines
1.3 KiB
Go

package main
import (
"crypto/tls"
"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,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
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
}