Added !s command and fixed links bug
This commit is contained in:
parent
27b1385a92
commit
3ee53b7ad1
5 changed files with 70 additions and 12 deletions
|
|
@ -5,6 +5,18 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func GetNameByCardId(set string, number string) string {
|
||||||
|
/*
|
||||||
|
From https://docs.magicthegathering.io/#api_v1cards_list
|
||||||
|
|
||||||
|
Number: This is a string, not an integer,
|
||||||
|
because some cards have letters in their numbers.
|
||||||
|
*/
|
||||||
|
cards, _, _ := mtg.NewQuery().Where(mtg.CardSet, set).Where(mtg.CardNumber, number).PageS(1, 1)
|
||||||
|
name := fetchCardNameFromSlice(cards)
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
func GetOriginalName(name string) string {
|
func GetOriginalName(name string) string {
|
||||||
langs := []string{"Russian", ""}
|
langs := []string{"Russian", ""}
|
||||||
channel := make(chan string)
|
channel := make(chan string)
|
||||||
|
|
@ -22,12 +34,16 @@ func GetOriginalName(name string) string {
|
||||||
|
|
||||||
func getOriginalNameFromLang(name, lang string, channel chan string) {
|
func getOriginalNameFromLang(name, lang string, channel chan string) {
|
||||||
cards, _, _ := mtg.NewQuery().Where(mtg.CardLanguage, lang).Where(mtg.CardName, name).PageS(1, 1)
|
cards, _, _ := mtg.NewQuery().Where(mtg.CardLanguage, lang).Where(mtg.CardName, name).PageS(1, 1)
|
||||||
|
originalName := fetchCardNameFromSlice(cards)
|
||||||
|
channel <- originalName
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchCardNameFromSlice(cards []*mtg.Card) string {
|
||||||
if len(cards) > 0 {
|
if len(cards) > 0 {
|
||||||
name := getCardName(cards[0])
|
name := getCardName(cards[0])
|
||||||
channel <- name
|
return name
|
||||||
} else {
|
|
||||||
channel <- ""
|
|
||||||
}
|
}
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCardName(card *mtg.Card) string {
|
func getCardName(card *mtg.Card) string {
|
||||||
|
|
|
||||||
|
|
@ -110,7 +110,7 @@ func parsePrice(container *html.Node) float64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseLink(container *html.Node) string {
|
func parseLink(container *html.Node) string {
|
||||||
linkNodes := htmlquery.Find(container, "//td[contains(@class, 'search_results_2')]/a")
|
linkNodes := htmlquery.Find(container, "//td[contains(@class, 'search_results_1')]/b/a")
|
||||||
if len(linkNodes) == 0 {
|
if len(linkNodes) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
main.go
2
main.go
|
|
@ -11,5 +11,5 @@ func main() {
|
||||||
rand.Seed(time.Now().UTC().UnixNano())
|
rand.Seed(time.Now().UTC().UnixNano())
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
r.POST("callback/message", vk.HandleMessage)
|
r.POST("callback/message", vk.HandleMessage)
|
||||||
r.Run(":8000")
|
r.Run(":80")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,3 +35,13 @@ func TestGetCardByStringWrong(t *testing.T) {
|
||||||
name := cardsinfo.GetOriginalName("fwijefiwjfew")
|
name := cardsinfo.GetOriginalName("fwijefiwjfew")
|
||||||
assert.Equal(t, "", name)
|
assert.Equal(t, "", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetCardBySetId(t *testing.T) {
|
||||||
|
name := cardsinfo.GetNameByCardId("DOM", "207")
|
||||||
|
assert.Equal(t, "Teferi, Hero of Dominaria", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetCardBySetIdWrong(t *testing.T) {
|
||||||
|
name := cardsinfo.GetNameByCardId("DOM", "1207")
|
||||||
|
assert.Equal(t, "", name)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
package vk
|
package vk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"github.com/flygrounder/mtg-price-vk/cardsinfo"
|
"github.com/flygrounder/mtg-price-vk/cardsinfo"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const CARDSLIMIT = 8
|
const CARDSLIMIT = 8
|
||||||
|
|
@ -18,16 +20,23 @@ func min(a, b int) int {
|
||||||
func HandleMessage(c *gin.Context) {
|
func HandleMessage(c *gin.Context) {
|
||||||
var req MessageRequest
|
var req MessageRequest
|
||||||
c.BindJSON(&req)
|
c.BindJSON(&req)
|
||||||
if (req.Type == "confirmation") && (req.GroupId == GROUPID) {
|
|
||||||
c.String(http.StatusOK, CONFIRMATION_STRING)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer c.String(http.StatusOK, "ok")
|
|
||||||
if req.Secret != SECRET_KEY {
|
if req.Secret != SECRET_KEY {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cardName := cardsinfo.GetOriginalName(req.Object.Body)
|
switch req.Type {
|
||||||
if cardName == "" {
|
case "confirmation":
|
||||||
|
handleConfirmation(c, &req)
|
||||||
|
case "message_new":
|
||||||
|
handleSearch(c, &req)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSearch(c *gin.Context, req *MessageRequest) {
|
||||||
|
defer c.String(http.StatusOK, "ok")
|
||||||
|
cardName, err := getCardNameByCommand(req.Object.Body)
|
||||||
|
if err != nil {
|
||||||
|
Message(req.Object.UserId, "Некорректная команда")
|
||||||
|
} else if cardName == "" {
|
||||||
Message(req.Object.UserId, "Карта не найдена")
|
Message(req.Object.UserId, "Карта не найдена")
|
||||||
} else {
|
} else {
|
||||||
prices, _ := cardsinfo.GetSCGPrices(cardName)
|
prices, _ := cardsinfo.GetSCGPrices(cardName)
|
||||||
|
|
@ -37,3 +46,26 @@ func HandleMessage(c *gin.Context) {
|
||||||
Message(req.Object.UserId, priceInfo)
|
Message(req.Object.UserId, priceInfo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getCardNameByCommand(command string) (string, error) {
|
||||||
|
var name string
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(command, "!s"):
|
||||||
|
split := strings.Split(command, " ")
|
||||||
|
if len(split) < 3 {
|
||||||
|
return "", errors.New("wrong command")
|
||||||
|
}
|
||||||
|
set := split[1]
|
||||||
|
number := split[2]
|
||||||
|
name = cardsinfo.GetNameByCardId(set, number)
|
||||||
|
default:
|
||||||
|
name = cardsinfo.GetOriginalName(command)
|
||||||
|
}
|
||||||
|
return name, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleConfirmation(c *gin.Context, req *MessageRequest) {
|
||||||
|
if (req.Type == "confirmation") && (req.GroupId == GROUPID) {
|
||||||
|
c.String(http.StatusOK, CONFIRMATION_STRING)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue