Returned StarCityGames support

This commit is contained in:
Aryom Belousov 2020-11-09 15:24:32 +03:00
parent de1187b96c
commit 7c6a6176dc
12 changed files with 133 additions and 72 deletions

View file

@ -8,15 +8,7 @@ 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\nRegular: %v\nFoil: %v\n", i+1, v.Edition, formatPrice(v.Price), formatPrice(v.PriceFoil))
message += fmt.Sprintf("%v\n", v.Link)
message += fmt.Sprintf("%v. %v", i + 1, v.Format())
}
return message
}
func formatPrice(price string) string {
if price == "" {
return "-"
}
return fmt.Sprintf("$%v", price)
}

46
cardsinfo/names_test.go Normal file
View file

@ -0,0 +1,46 @@
package cardsinfo
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestGetCardByStringFull(t *testing.T) {
name := GetOriginalName("Шок")
assert.Equal(t, "Shock", name)
}
func TestGetCardByStringSplit(t *testing.T) {
name := GetOriginalName("commit")
assert.Equal(t, "Commit // Memory", name)
}
func TestGetCardByStringDouble(t *testing.T) {
name := GetOriginalName("Legion's landing")
assert.Equal(t, "Legion's Landing | Adanto, the First Fort", name)
}
func TestGetCardByStringPrefix(t *testing.T) {
name := GetOriginalName("Тефери, герой")
assert.Equal(t, "Teferi, Hero of Dominaria", name)
}
func TestGetCardByStringEnglish(t *testing.T) {
name := GetOriginalName("Teferi, Hero of Dominaria")
assert.Equal(t, "Teferi, Hero of Dominaria", name)
}
func TestGetCardByStringWrong(t *testing.T) {
name := GetOriginalName("fwijefiwjfew")
assert.Equal(t, "", name)
}
func TestGetCardBySetId(t *testing.T) {
name := GetNameByCardId("DOM", "207")
assert.Equal(t, "Teferi, Hero of Dominaria", name)
}
func TestGetCardBySetIdWrong(t *testing.T) {
name := GetNameByCardId("DOM", "1207")
assert.Equal(t, "", name)
}

View file

@ -0,0 +1,26 @@
package cardsinfo
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestFormat(t *testing.T) {
data := []CardPrice{
&TcgCardPrice{
Name: "Green lotus",
PriceFoil: "22.8",
Link: "scg.com/1",
Edition: "alpha",
},
&TcgCardPrice{
Name: "White lotus",
Price: "3.22",
Link: "scg.com/2",
Edition: "gamma",
},
}
res := FormatCardPrices("Black Lotus", data)
ans := "Оригинальное название: Black Lotus\nРезультатов: 2\n1. alpha\nRegular: -\nFoil: $22.8\nscg.com/1\n2. gamma\nRegular: $3.22\nFoil: -\nscg.com/2\n"
assert.Equal(t, ans, res)
}

View file

@ -1,14 +1,50 @@
package cardsinfo
import (
"fmt"
"context"
"fmt"
"net/url"
scryfall "github.com/BlueMonday/go-scryfall"
"github.com/antchfx/htmlquery"
"github.com/pkg/errors"
)
const scgDomain = "https://starcitygames.com"
const scgSearchUrlTemplate = "https://starcitygames.hawksearch.com/sites/starcitygames/?search_query=%v"
func GetPrices(name string) ([]CardPrice, error) {
return GetPricesScg(name)
}
func GetPricesScg(name string) ([]CardPrice, error) {
escapedName := url.QueryEscape(name)
searchUrl := fmt.Sprintf(scgSearchUrlTemplate, escapedName)
node, err := htmlquery.LoadURL(searchUrl)
if err != nil {
return nil, errors.Wrap(err, "cannot load url")
}
blocks := htmlquery.Find(node, "//div[@class=\"hawk-results-item\"]")
var results []CardPrice
for _, block := range blocks {
price := &ScgCardPrice{}
linkNode := htmlquery.FindOne(block, "//h2/a")
for _, attr := range linkNode.Attr {
if attr.Key == "href" {
price.Link = scgDomain + attr.Val
break
}
}
editionNode := htmlquery.FindOne(block, "//p[@class=\"hawk-results-item__category\"]/a")
price.Edition = editionNode.FirstChild.Data
priceNode := htmlquery.FindOne(block, "//div[contains(concat(' ',normalize-space(@class),' '),' hawk-results-item__options-table-cell--price ')]")
price.Price = priceNode.FirstChild.Data
results = append(results, price)
}
return results, nil
}
func GetPricesTcg(name string) ([]CardPrice, error) {
client, err := scryfall.NewClient()
if err != nil {
return nil, errors.Wrap(err, "Cannot fetch prices")
@ -24,7 +60,7 @@ func GetPrices(name string) ([]CardPrice, error) {
if card.Prices.USD == "" && card.Prices.USDFoil == "" {
continue
}
cardPrice := CardPrice {
cardPrice := &TcgCardPrice {
Edition: edition,
Price: card.Prices.USD,
PriceFoil: card.Prices.USDFoil,

13
cardsinfo/prices_test.go Normal file
View file

@ -0,0 +1,13 @@
package cardsinfo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestParser(t *testing.T) {
prices, err := GetPrices("Black lotus")
assert.Nil(t, err)
assert.NotEmpty(t, prices)
}

View file

@ -1,10 +1,15 @@
package cardsinfo
import (
"fmt"
"strings"
)
type CardPrice struct {
type CardPrice interface {
Format() string
}
type TcgCardPrice struct {
FullArt bool
Name string
Price string
@ -13,6 +18,27 @@ type CardPrice struct {
Edition string
}
func (t *TcgCardPrice) Format() string {
return fmt.Sprintf("%v\nRegular: %v\nFoil: %v\n%v\n", t.Edition, formatTcgPrice(t.Price), formatTcgPrice(t.PriceFoil), t.Link)
}
func formatTcgPrice(price string) string {
if price == "" {
return "-"
}
return fmt.Sprintf("$%v", price)
}
type ScgCardPrice struct {
Price string
Edition string
Link string
}
func (s *ScgCardPrice) Format() string {
return fmt.Sprintf("%v: %v\n%v\n", s.Edition, s.Price, s.Link)
}
type Card struct {
Name string `json:"name"`
Layout string `json:"layout"`
@ -24,20 +50,3 @@ func (c *Card) getName() string {
}
return c.Name
}
type ScgResponse struct {
Response ScgResponseContainer `json:"response"`
}
type ScgResponseContainer struct {
Data []ScgConditionContainer `json:"data"`
}
type ScgConditionContainer struct {
Price float64 `json:"price"`
OptionValues []ScgCondition `json:"option_values"`
}
type ScgCondition struct {
Label string `json:"label"`
}