Added href parsing
This commit is contained in:
parent
738142f27f
commit
c0f781fc7a
6 changed files with 78 additions and 7 deletions
15
cardsinfo/format.go
Normal file
15
cardsinfo/format.go
Normal file
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
package cardsinfo
|
||||
|
||||
type CardPrice struct {
|
||||
Name string
|
||||
Price float64
|
||||
Name string
|
||||
Price float64
|
||||
Link string
|
||||
Edition string
|
||||
}
|
||||
|
|
|
|||
2
main.go
2
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")
|
||||
}
|
||||
|
|
|
|||
27
tests/price_format_test.go
Normal file
27
tests/price_format_test.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue