-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsnap.go
94 lines (83 loc) · 2.35 KB
/
snap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
raven "github.com/getsentry/raven-go"
log "gopkg.in/inconshreveable/log15.v2"
)
const snapAPI = "https://marvelsnap.io/api/search.php?database&n=%s&desc=%s&sort=name&limit=1&offset=0"
type SnapCard struct {
ID int `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Cost *int `json:"cost"`
Power *int `json:"power"`
Ability string `json:"ability"`
DateAdded string `json:"date_added"`
Status *string `json:"status"`
Variants string `json:"variants"`
PrettyURL string `json:"pretty_url"`
Method *string `json:"method"`
Slug string `json:"slug"`
}
type SnapResponse struct {
Card []SnapCard `json:"card"`
Paging struct {
TotalItems int `json:"total_items"`
TotalPages int `json:"total_pages"`
} `json:"paging"`
}
func handleSnapQuery(cardTokens []string) string {
for _, rc := range reduceCardSentence(cardTokens) {
card, err := searchSnapCard(rc)
log.Debug("Snap Card Func gave us", "CardID", card, "Err", err)
if err == nil {
return card
}
}
return ""
}
func formatSnapCard(c SnapCard) string {
var r []string
r = append(r, fmt.Sprintf("*<https://marvelsnap.io/card/%v|%v>* -", c.PrettyURL, c.Name))
if c.Cost != nil {
r = append(r, replaceManaCostForSlack(fmt.Sprintf(" {%d}", *c.Cost)))
}
if c.Power != nil {
r = append(r, fmt.Sprintf(" · ⚔️ %v ⚔️ ·", *c.Power))
}
r = append(r, fmt.Sprintf(" %v", c.Ability))
if c.Method != nil {
r = append(r, fmt.Sprintf(" · (_%v_)", *c.Method))
}
return strings.Join(r, "")
}
func searchSnapCard(input string) (string, error) {
req, err := http.NewRequest("GET", fmt.Sprintf(snapAPI, input, input), nil)
if err != nil {
raven.CaptureError(err, nil)
log.Warn("searchSnapCard: The HTTP request could not be created", "Error", err)
return "", err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
raven.CaptureError(err, nil)
log.Warn("searchSnapCard: The HTTP request failed", "Error", err)
return "", err
}
defer resp.Body.Close()
var sc SnapResponse
if resp.StatusCode == 200 {
if err := json.NewDecoder(resp.Body).Decode(&sc); err != nil {
raven.CaptureError(err, nil)
return "", err
}
if len(sc.Card) == 1 {
return formatSnapCard(sc.Card[0]), nil
}
}
return "", fmt.Errorf("Card not found")
}