diff --git a/cardsinfo/format.go b/cardsinfo/format.go new file mode 100644 index 0000000..7344c7b --- /dev/null +++ b/cardsinfo/format.go @@ -0,0 +1,15 @@ +package cardsinfo + +import ( + "fmt" +) + +func FormatCardPrices(name string, prices []CardPrice) string { + message := fmt.Sprintf("Оригинальное название: %v\n", name) + message += fmt.Sprintf("Результатов: %v\n", len(prices)) + for i, v := range prices { + message += fmt.Sprintf("%v. %v: $%v\n", i+1, v.Edition, v.Price) + message += fmt.Sprintf("%v\n", v.Link) + } + return message +} diff --git a/cardsinfo/scgprices.go b/cardsinfo/scgprices.go index 8c3cc6d..2874c1a 100644 --- a/cardsinfo/scgprices.go +++ b/cardsinfo/scgprices.go @@ -27,6 +27,11 @@ func GetSCGPrices(name string) ([]CardPrice, error) { continue } name := htmlquery.InnerText(nameNode) + editionNode := htmlquery.FindOne(node, "//td[contains(@class, 'search_results_2')]") + if editionNode == nil { + continue + } + edition := strings.Trim(htmlquery.InnerText(editionNode), "\n ") priceNode := htmlquery.FindOne(node, "//td[contains(@class, 'search_results_9')]") if priceNode == nil { continue @@ -36,9 +41,22 @@ func GetSCGPrices(name string) ([]CardPrice, error) { if err != nil { continue } + linkNodes := htmlquery.Find(node, "//td[contains(@class, 'search_results_2')]/a") + if len(linkNodes) == 0 { + continue + } + linkNode := linkNodes[0] + var link string + for _, v := range linkNode.Attr { + if v.Key == "href" { + link = v.Val + } + } obj := CardPrice{ - Name: name, - Price: price, + Name: name, + Price: price, + Edition: edition, + Link: link, } prices = append(prices, obj) } diff --git a/cardsinfo/structs.go b/cardsinfo/structs.go index a55b9a6..2f97b56 100644 --- a/cardsinfo/structs.go +++ b/cardsinfo/structs.go @@ -1,6 +1,8 @@ package cardsinfo type CardPrice struct { - Name string - Price float64 + Name string + Price float64 + Link string + Edition string } diff --git a/main.go b/main.go index 7d5c049..7877e63 100644 --- a/main.go +++ b/main.go @@ -11,5 +11,5 @@ func main() { rand.Seed(time.Now().UTC().UnixNano()) r := gin.Default() r.POST("callback/message", vk.HandleMessage) - r.Run(":80") + r.Run(":8000") } diff --git a/tests/price_format_test.go b/tests/price_format_test.go new file mode 100644 index 0000000..41ce244 --- /dev/null +++ b/tests/price_format_test.go @@ -0,0 +1,27 @@ +package tests + +import ( + "github.com/flygrounder/mtg-price-vk/cardsinfo" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestFormat(t *testing.T) { + data := []cardsinfo.CardPrice{ + { + Name: "Green lotus", + Price: 22.8, + Link: "scg.com/1", + Edition: "alpha", + }, + { + Name: "White lotus", + Price: 3.22, + Link: "scg.com/2", + Edition: "gamma", + }, + } + res := cardsinfo.FormatCardPrices("Black Lotus", data) + ans := "Оригинальное название: Black Lotus\nРезультатов: 2\n1. alpha: $22.8\nscg.com/1\n2. gamma: $3.22\nscg.com/2\n" + assert.Equal(t, res, ans) +} diff --git a/vk/handlers.go b/vk/handlers.go index 40a3a45..92a4149 100644 --- a/vk/handlers.go +++ b/vk/handlers.go @@ -6,6 +6,8 @@ import ( "net/http" ) +const CARDSLIMIT = 8 + func HandleMessage(c *gin.Context) { var req MessageRequest c.BindJSON(&req) @@ -13,10 +15,17 @@ func HandleMessage(c *gin.Context) { c.String(http.StatusOK, CONFIRMATION_STRING) return } + defer c.String(http.StatusOK, "ok") if req.Secret != SECRET_KEY { return } cardName := cardsinfo.GetOriginalName(req.Object.Body) - Message(req.Object.UserId, cardName) - c.String(http.StatusOK, "ok") + if cardName == "" { + Message(req.Object.UserId, "Карта не найдена") + } else { + prices, _ := cardsinfo.GetSCGPrices(cardName) + prices = prices[:CARDSLIMIT] + priceInfo := cardsinfo.FormatCardPrices(cardName, prices) + Message(req.Object.UserId, priceInfo) + } }