Added non-card products filtering
This commit is contained in:
parent
445110a778
commit
9ca4c0ce43
3 changed files with 4190 additions and 11 deletions
|
|
@ -2,6 +2,7 @@ package cardsinfo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/antchfx/htmlquery"
|
"github.com/antchfx/htmlquery"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
@ -33,20 +34,20 @@ func getPricesScg(name string) ([]scgCardPrice, error) {
|
||||||
for _, block := range blocks {
|
for _, block := range blocks {
|
||||||
price := scgCardPrice{}
|
price := scgCardPrice{}
|
||||||
linkNode := htmlquery.FindOne(block, "//h2/a")
|
linkNode := htmlquery.FindOne(block, "//h2/a")
|
||||||
for _, attr := range linkNode.Attr {
|
price.link = scgDomain + htmlquery.SelectAttr(linkNode, "href")
|
||||||
if attr.Key == "href" {
|
|
||||||
price.link = scgDomain + attr.Val
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
editionNode := htmlquery.FindOne(block, "//p[@class=\"hawk-results-item__category\"]/a")
|
editionNode := htmlquery.FindOne(block, "//p[@class=\"hawk-results-item__category\"]/a")
|
||||||
if editionNode.FirstChild != nil {
|
if editionNode.FirstChild == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(htmlquery.SelectAttr(editionNode, "href"), "/shop/singles/") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
price.edition = editionNode.FirstChild.Data
|
price.edition = editionNode.FirstChild.Data
|
||||||
}
|
|
||||||
priceNode := htmlquery.FindOne(block, "//span[@class='hawk-old-price']|//div[contains(concat(' ',normalize-space(@class),' '),' hawk-results-item__options-table-cell--price ')]")
|
priceNode := htmlquery.FindOne(block, "//span[@class='hawk-old-price']|//div[contains(concat(' ',normalize-space(@class),' '),' hawk-results-item__options-table-cell--price ')]")
|
||||||
if priceNode.FirstChild != nil {
|
if priceNode.FirstChild == nil {
|
||||||
price.price = priceNode.FirstChild.Data
|
continue
|
||||||
}
|
}
|
||||||
|
price.price = priceNode.FirstChild.Data
|
||||||
results = append(results, price)
|
results = append(results, price)
|
||||||
}
|
}
|
||||||
return results, nil
|
return results, nil
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
package cardsinfo
|
package cardsinfo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"gopkg.in/h2non/gock.v1"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"gopkg.in/h2non/gock.v1"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -65,3 +66,41 @@ func TestGetPrices_Empty(t *testing.T) {
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Nil(t, prices)
|
assert.Nil(t, prices)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetPrices_FilterNonCards(t *testing.T) {
|
||||||
|
defer gock.Off()
|
||||||
|
|
||||||
|
file, _ := os.Open("test_data/NonCards.html")
|
||||||
|
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusOK).Body(file)
|
||||||
|
f := &Fetcher{}
|
||||||
|
prices, err := f.getPrices("card")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
expected := []scgCardPrice{
|
||||||
|
{
|
||||||
|
price: "$72.99",
|
||||||
|
edition: "3rd Edition - Black Border",
|
||||||
|
link: "https://starcitygames.com/sol-ring-sgl-mtg-3bb-274-frn/?sku=SGL-MTG-3BB-274-FRN3",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
price: "$24.99",
|
||||||
|
edition: "3rd Edition/Revised",
|
||||||
|
link: "https://starcitygames.com/sol-ring-sgl-mtg-3ed-274-enn/?sku=SGL-MTG-3ED-274-ENN1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
price: "$1,999.99",
|
||||||
|
edition: "Alpha",
|
||||||
|
link: "https://starcitygames.com/sol-ring-sgl-mtg-lea-269-enn/?sku=SGL-MTG-LEA-269-ENN1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
price: "$1,199.99",
|
||||||
|
edition: "Beta",
|
||||||
|
link: "https://starcitygames.com/sol-ring-sgl-mtg-leb-270-enn/?sku=SGL-MTG-LEB-270-ENN1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
price: "$99.99",
|
||||||
|
edition: "Collectors' Edition",
|
||||||
|
link: "https://starcitygames.com/sol-ring-sgl-mtg-ced-270-enn/?sku=SGL-MTG-CED-270-ENN1",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
assert.Equal(t, expected, prices)
|
||||||
|
}
|
||||||
|
|
|
||||||
4139
internal/cardsinfo/test_data/NonCards.html
Normal file
4139
internal/cardsinfo/test_data/NonCards.html
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue