Refactoring
This commit is contained in:
parent
f7a703769f
commit
4cd5311301
2 changed files with 100 additions and 45 deletions
|
|
@ -3,6 +3,7 @@ package cardsinfo
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/antchfx/htmlquery"
|
"github.com/antchfx/htmlquery"
|
||||||
|
"golang.org/x/net/html"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
@ -10,57 +11,54 @@ import (
|
||||||
const SCGURL = "http://www.starcitygames.com/results?name="
|
const SCGURL = "http://www.starcitygames.com/results?name="
|
||||||
|
|
||||||
func GetSCGPrices(name string) ([]CardPrice, error) {
|
func GetSCGPrices(name string) ([]CardPrice, error) {
|
||||||
splitted := strings.Split(name, " ")
|
url := getSCGUrl(name)
|
||||||
scgName := strings.Join(splitted, "+")
|
|
||||||
url := SCGURL + scgName
|
|
||||||
doc, err := htmlquery.LoadURL(url)
|
doc, err := htmlquery.LoadURL(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
return fetchPrices(doc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchPrices(doc *html.Node) ([]CardPrice, error) {
|
||||||
|
priceContainers := getPriceContainers(doc)
|
||||||
|
prices := make([]CardPrice, 0)
|
||||||
|
for _, container := range priceContainers {
|
||||||
|
name := parseName(container)
|
||||||
|
edition := parseEdition(container)
|
||||||
|
price := parsePrice(container)
|
||||||
|
link := parseLink(container)
|
||||||
|
cardPrice := buildCardPrice(name, edition, price, link)
|
||||||
|
if isValidPrice(&cardPrice) {
|
||||||
|
prices = append(prices, cardPrice)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return prices, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isValidPrice(price *CardPrice) bool {
|
||||||
|
isValid := true
|
||||||
|
isValid = isValid && (price.Name != "")
|
||||||
|
isValid = isValid && (price.Edition != "")
|
||||||
|
isValid = isValid && (price.Price != 0.0)
|
||||||
|
isValid = isValid && (price.Link != "")
|
||||||
|
return isValid
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildCardPrice(name, edition string, price float64, link string) CardPrice {
|
||||||
|
cardPrice := CardPrice{
|
||||||
|
Name: name,
|
||||||
|
Edition: edition,
|
||||||
|
Price: price,
|
||||||
|
Link: link,
|
||||||
|
}
|
||||||
|
return cardPrice
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPriceContainers(doc *html.Node) []*html.Node {
|
||||||
nodesOdd := htmlquery.Find(doc, "//tr[contains(@class, 'deckdbbody_row')]")
|
nodesOdd := htmlquery.Find(doc, "//tr[contains(@class, 'deckdbbody_row')]")
|
||||||
nodesEven := htmlquery.Find(doc, "//tr[contains(@class, 'deckdbbody2_row')]")
|
nodesEven := htmlquery.Find(doc, "//tr[contains(@class, 'deckdbbody2_row')]")
|
||||||
nodes := append(nodesOdd, nodesEven...)
|
nodes := append(nodesOdd, nodesEven...)
|
||||||
prices := make([]CardPrice, 0)
|
return nodes
|
||||||
for _, node := range nodes {
|
|
||||||
nameNode := htmlquery.FindOne(node, "//td[contains(@class, 'search_results_1')]")
|
|
||||||
if nameNode == nil {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
priceS := htmlquery.InnerText(priceNode)
|
|
||||||
price, err := fetchPrice(priceS)
|
|
||||||
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,
|
|
||||||
Edition: edition,
|
|
||||||
Link: link,
|
|
||||||
}
|
|
||||||
prices = append(prices, obj)
|
|
||||||
}
|
|
||||||
return prices, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchPrice(price string) (float64, error) {
|
func fetchPrice(price string) (float64, error) {
|
||||||
|
|
@ -72,3 +70,60 @@ func fetchPrice(price string) (float64, error) {
|
||||||
v, err := strconv.ParseFloat(p, 64)
|
v, err := strconv.ParseFloat(p, 64)
|
||||||
return v, err
|
return v, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getSCGUrl(name string) string {
|
||||||
|
splitted := strings.Split(name, " ")
|
||||||
|
scgName := strings.Join(splitted, "+")
|
||||||
|
url := SCGURL + scgName
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseName(container *html.Node) string {
|
||||||
|
nameNode := htmlquery.FindOne(container, "//td[contains(@class, 'search_results_1')]")
|
||||||
|
if nameNode == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
name := htmlquery.InnerText(nameNode)
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseEdition(container *html.Node) string {
|
||||||
|
editionNode := htmlquery.FindOne(container, "//td[contains(@class, 'search_results_2')]")
|
||||||
|
if editionNode == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
edition := strings.Trim(htmlquery.InnerText(editionNode), "\n ")
|
||||||
|
return edition
|
||||||
|
}
|
||||||
|
|
||||||
|
func parsePrice(container *html.Node) float64 {
|
||||||
|
priceNode := htmlquery.FindOne(container, "//td[contains(@class, 'search_results_9')]")
|
||||||
|
if priceNode == nil {
|
||||||
|
return 0.0
|
||||||
|
}
|
||||||
|
priceString := htmlquery.InnerText(priceNode)
|
||||||
|
price, err := fetchPrice(priceString)
|
||||||
|
if err != nil {
|
||||||
|
return 0.0
|
||||||
|
}
|
||||||
|
return price
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseLink(container *html.Node) string {
|
||||||
|
linkNodes := htmlquery.Find(container, "//td[contains(@class, 'search_results_2')]/a")
|
||||||
|
if len(linkNodes) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
linkNode := linkNodes[0]
|
||||||
|
link := fetchLink(linkNode)
|
||||||
|
return link
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchLink(linkContainer *html.Node) string {
|
||||||
|
for _, v := range linkContainer.Attr {
|
||||||
|
if v.Key == "href" {
|
||||||
|
return v.Val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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(":80")
|
r.Run(":8000")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue