mlpcardbot/mlpapi1.go

65 lines
1.3 KiB
Go
Raw Normal View History

2019-08-20 12:36:26 +00:00
package main
import (
2019-08-20 12:57:57 +00:00
"crypto/tls"
2019-08-20 12:36:26 +00:00
"encoding/json"
"fmt"
2019-08-20 13:04:38 +00:00
"log"
2019-08-20 12:36:26 +00:00
"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,
2019-08-20 12:57:57 +00:00
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
2019-08-20 12:36:26 +00:00
}
func mlpapiSearch(query string) (results CardSearchResults, err error) {
query = url.QueryEscape(query)
requrl := fmt.Sprintf("https://www.ferrictorus.com/mlpapi1/cards?query=%s", query)
2019-08-20 13:04:38 +00:00
if cfg.Log {
log.Println(requrl)
}
2019-08-20 12:36:26 +00:00
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
}