Added dict with additional cards

This commit is contained in:
Artyom Belousov 2021-02-03 19:09:08 +03:00
parent 181f3bc0aa
commit 88ea431a27
26 changed files with 232 additions and 35 deletions

View file

@ -0,0 +1,14 @@
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", i+1, v.Format())
}
return message
}

View file

@ -0,0 +1,59 @@
package cardsinfo
import (
"encoding/json"
"gitlab.com/flygrounder/go-mtg-vk/internal/dicttranslate"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
const ScryfallUrl = "https://api.scryfall.com"
func GetNameByCardId(set string, number string) string {
/*
Note: number is string because some cards contain letters in their numbers.
*/
path := ScryfallUrl + "/cards/" + strings.ToLower(set) + "/" + number
return GetCardByUrl(path)
}
func GetOriginalName(name string, dict io.Reader) string {
path := ScryfallUrl + "/cards/named?fuzzy=" + ApplyFilters(name)
result := GetCardByUrl(path)
if result == "" && dict != nil {
result, _ = dicttranslate.FindFromReader(name, dict, 5)
}
return result
}
func ApplyFilters(name string) string {
/*
Despite of the rules of Russian language, letter ё is replaced with e on cards
Sometimes it leads to wrong search results
*/
name = strings.ReplaceAll(name, "ё", "е")
return url.QueryEscape(name)
}
func GetCardByUrl(path string) string {
response, err := http.Get(path)
if err != nil {
return ""
}
defer func() {
_ = response.Body.Close()
}()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return ""
}
var v Card
err = json.Unmarshal(data, &v)
if err != nil {
return ""
}
return v.getName()
}

View file

@ -0,0 +1,53 @@
package cardsinfo
import (
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func TestGetCardByStringFull(t *testing.T) {
name := GetOriginalName("Шок", nil)
assert.Equal(t, "Shock", name)
}
func TestGetCardByStringSplit(t *testing.T) {
name := GetOriginalName("commit", nil)
assert.Equal(t, "Commit // Memory", name)
}
func TestGetCardByStringDouble(t *testing.T) {
name := GetOriginalName("Legion's landing", nil)
assert.Equal(t, "Legion's Landing | Adanto, the First Fort", name)
}
func TestGetCardByStringPrefix(t *testing.T) {
name := GetOriginalName("Тефери, герой", nil)
assert.Equal(t, "Teferi, Hero of Dominaria", name)
}
func TestGetCardByStringEnglish(t *testing.T) {
name := GetOriginalName("Teferi, Hero of Dominaria", nil)
assert.Equal(t, "Teferi, Hero of Dominaria", name)
}
func TestGetCardByStringWrong(t *testing.T) {
name := GetOriginalName("fwijefiwjfew", nil)
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)
}
func TestGetCardByStringDict(t *testing.T) {
dictContent := "{\"n0suchc8rdc8n3x1s1\":\"Success\"}"
name := GetOriginalName("n0suchc8rdc8n3x1s1", strings.NewReader(dictContent))
assert.Equal(t, "Success", 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

@ -0,0 +1,84 @@
package cardsinfo
import (
"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) {
prices, err := GetPricesScg(name)
if err != nil {
return nil, err
}
if len(prices) > 5 {
return prices[:5], nil
}
return prices, nil
}
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")
if editionNode.FirstChild != nil {
price.Edition = editionNode.FirstChild.Data
}
priceNode := htmlquery.FindOne(block, "//div[contains(concat(' ',normalize-space(@class),' '),' hawk-results-item__options-table-cell--price ')]")
if priceNode.FirstChild != nil {
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")
}
ctx := context.Background()
opts := scryfall.SearchCardsOptions{
Unique: scryfall.UniqueModePrints,
}
resp, err := client.SearchCards(ctx, fmt.Sprintf("!\"%v\"", name), opts)
var prices []CardPrice
for _, card := range resp.Cards {
edition := card.SetName + " #" + card.CollectorNumber
if card.Prices.USD == "" && card.Prices.USDFoil == "" {
continue
}
cardPrice := &TcgCardPrice{
Edition: edition,
Price: card.Prices.USD,
PriceFoil: card.Prices.USDFoil,
Name: card.Name,
Link: card.PurchaseURIs.TCGPlayer,
}
prices = append(prices, cardPrice)
}
return prices, nil
}

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

@ -0,0 +1,52 @@
package cardsinfo
import (
"fmt"
"strings"
)
type CardPrice interface {
Format() string
}
type TcgCardPrice struct {
FullArt bool
Name string
Price string
PriceFoil string
Link string
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"`
}
func (c *Card) getName() string {
if c.Layout == "transform" {
return strings.Replace(c.Name, "//", "|", 1)
}
return c.Name
}