Added href parsing

This commit is contained in:
Artyom Belousov 2019-05-11 19:00:58 +03:00
parent 738142f27f
commit c0f781fc7a
6 changed files with 78 additions and 7 deletions

15
cardsinfo/format.go Normal file
View 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
}

View file

@ -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)
}

View file

@ -1,6 +1,8 @@
package cardsinfo
type CardPrice struct {
Name string
Price float64
Name string
Price float64
Link string
Edition string
}