-
Notifications
You must be signed in to change notification settings - Fork 0
/
decklog.go
72 lines (56 loc) · 1.5 KB
/
decklog.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
type decklog struct{}
type decklogCardInfo struct {
CardID string `json:"card_number"`
Amount int `json:"num"`
}
type decklogResponse struct {
Cards []decklogCardInfo `json:"list"`
}
func (e decklog) name() string {
return "decklog"
}
func (e decklog) getCardDecksInfoList(url string) ([][]Card, error) {
var decks = [][]Card{}
info, err := e.getCardDeckInfo(url)
if err != nil {
fmt.Println(err)
}
decks = append(decks, info)
return decks, nil
}
func (e decklog) getCardDeckInfo(url string) ([]Card, error) {
var cardsDeck = []Card{}
var decklogResp decklogResponse
splits := strings.Split(url, ".com/")
apiurl := fmt.Sprintf("%s.com/system/app/api/%s", splits[0], splits[1])
client := &http.Client{}
req, _ := http.NewRequest("POST", apiurl, nil)
req.Header.Set("referer", url)
resp, errresp := client.Do(req)
if errresp != nil {
fmt.Println("Error on resquest from decklog")
return cardsDeck, errresp
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
parseerr := decoder.Decode(&decklogResp)
if parseerr != nil {
fmt.Println("Error on decode from decklog")
return cardsDeck, parseerr
}
for _, info := range decklogResp.Cards {
// card := Card{Amount: info.Amount, ID: info.CardID}
cardsDeck = append(cardsDeck, Card{Amount: info.Amount, ID: strings.ToUpper(info.CardID)})
}
return cardsDeck, nil
}
func (e decklog) isMine(url string) bool {
return strings.Contains(url, "decklog")
}