Added different formatting for telegram bot

This commit is contained in:
Artyom Belousov 2021-06-06 17:21:29 +03:00
parent 35f8fa5a57
commit 89623f5f6a
18 changed files with 233 additions and 173 deletions

View file

@ -1,18 +1,13 @@
package cardsinfo
import (
"fmt"
"strings"
)
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 ScgCardPrice struct {
Price string
Edition string
Link string
}
type card struct {

View file

@ -1,24 +0,0 @@
package cardsinfo
import (
"fmt"
)
func (f *Fetcher) GetFormattedCardPrices(name string) (string, error) {
prices, err := f.getPrices(name)
if err != nil {
return "", err
}
return f.formatCardPrices(name, prices), nil
}
func (f *Fetcher) formatCardPrices(name string, prices []scgCardPrice) string {
message := fmt.Sprintf("Оригинальное название: %v\n\n", name)
for i, v := range prices {
message += fmt.Sprintf("%v. %v", i+1, v.format())
}
if len(prices) == 0 {
message += "Цен не найдено\n"
}
return message
}

View file

@ -1,42 +0,0 @@
package cardsinfo
import (
"net/http"
"os"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)
func TestFetcher_GetFormattedCardPrices_Error(t *testing.T) {
defer gock.Off()
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusInternalServerError)
f := &Fetcher{}
_, err := f.GetFormattedCardPrices("card")
assert.NotNil(t, err)
}
func TestFetcher_GetFormattedCardPrices_Empty(t *testing.T) {
defer gock.Off()
resp, _ := os.Open("test_data/EmptyTest.html")
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusOK).Body(resp)
f := &Fetcher{}
msg, err := f.GetFormattedCardPrices("card")
assert.Nil(t, err)
assert.Equal(t, "Оригинальное название: card\n\nЦен не найдено\n", msg)
}
func TestFormatCardPrices(t *testing.T) {
f := &Fetcher{}
formatted := f.formatCardPrices("card", []scgCardPrice{
{
price: "1.5$",
edition: "ED",
link: "scg.com",
},
})
assert.Equal(t, "Оригинальное название: card\n\n1. ED: 1.5$\nscg.com\n", formatted)
}

View file

@ -11,7 +11,7 @@ import (
const scgDomain = "https://starcitygames.com"
const scgSearchUrlTemplate = "https://starcitygames.hawksearch.com/sites/starcitygames/?search_query="
func (f *Fetcher) getPrices(name string) ([]scgCardPrice, error) {
func (f *Fetcher) GetPrices(name string) ([]ScgCardPrice, error) {
prices, err := getPricesScg(name)
if err != nil {
return nil, err
@ -22,7 +22,7 @@ func (f *Fetcher) getPrices(name string) ([]scgCardPrice, error) {
return prices, nil
}
func getPricesScg(name string) ([]scgCardPrice, error) {
func getPricesScg(name string) ([]ScgCardPrice, error) {
escapedName := url.QueryEscape(name)
searchUrl := scgSearchUrlTemplate + escapedName
node, err := htmlquery.LoadURL(searchUrl)
@ -30,18 +30,18 @@ func getPricesScg(name string) ([]scgCardPrice, error) {
return nil, errors.Wrap(err, "cannot load url")
}
blocks := htmlquery.Find(node, "//div[@class=\"hawk-results-item\"]")
var results []scgCardPrice
var results []ScgCardPrice
for _, block := range blocks {
price := scgCardPrice{}
price := ScgCardPrice{}
linkNode := htmlquery.FindOne(block, "//h2/a")
price.link = scgDomain + htmlquery.SelectAttr(linkNode, "href")
price.Link = scgDomain + htmlquery.SelectAttr(linkNode, "href")
editionNode := htmlquery.FindOne(block, "//p[@class=\"hawk-results-item__category\"]/a")
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 ')]")
price.price = priceNode.FirstChild.Data
price.Price = priceNode.FirstChild.Data
results = append(results, price)
}
return results, nil

View file

@ -16,33 +16,33 @@ func TestGetPrices_Ok(t *testing.T) {
file, _ := os.Open("test_data/AcademyRuinsTest.html")
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusOK).Body(file)
f := &Fetcher{}
prices, err := f.getPrices("card")
prices, err := f.GetPrices("card")
assert.Nil(t, err)
assert.Equal(t, []scgCardPrice{
assert.Equal(t, []ScgCardPrice{
{
price: "$6.99",
edition: "Double Masters",
link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm-309-enn/?sku=SGL-MTG-2XM-309-ENN1",
Price: "$6.99",
Edition: "Double Masters",
Link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm-309-enn/?sku=SGL-MTG-2XM-309-ENN1",
},
{
price: "$9.99",
edition: "Double Masters (Foil)",
link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm-309-enf/?sku=SGL-MTG-2XM-309-ENF1",
Price: "$9.99",
Edition: "Double Masters (Foil)",
Link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm-309-enf/?sku=SGL-MTG-2XM-309-ENF1",
},
{
price: "$11.99",
edition: "Double Masters - Variants",
link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm2-369-enn/?sku=SGL-MTG-2XM2-369-ENN1",
Price: "$11.99",
Edition: "Double Masters - Variants",
Link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm2-369-enn/?sku=SGL-MTG-2XM2-369-ENN1",
},
{
price: "$14.99",
edition: "Double Masters - Variants (Foil)",
link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm2-369-enf/?sku=SGL-MTG-2XM2-369-ENF1",
Price: "$14.99",
Edition: "Double Masters - Variants (Foil)",
Link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm2-369-enf/?sku=SGL-MTG-2XM2-369-ENF1",
},
{
price: "$7.99",
edition: "Modern Masters: 2013 Edition",
link: "https://starcitygames.com/academy-ruins-sgl-mtg-mma-219-enn/?sku=SGL-MTG-MMA-219-ENN1",
Price: "$7.99",
Edition: "Modern Masters: 2013 Edition",
Link: "https://starcitygames.com/academy-ruins-sgl-mtg-mma-219-enn/?sku=SGL-MTG-MMA-219-ENN1",
},
}, prices)
}
@ -52,7 +52,7 @@ func TestGetPrices_Unavailable(t *testing.T) {
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusBadGateway)
f := &Fetcher{}
_, err := f.getPrices("card")
_, err := f.GetPrices("card")
assert.NotNil(t, err)
}
@ -62,7 +62,7 @@ func TestGetPrices_Empty(t *testing.T) {
file, _ := os.Open("test_data/EmptyTest.html")
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusOK).Body(file)
f := &Fetcher{}
prices, err := f.getPrices("card")
prices, err := f.GetPrices("card")
assert.Nil(t, err)
assert.Nil(t, prices)
}
@ -73,33 +73,33 @@ func TestGetPrices_FilterNonCards(t *testing.T) {
file, _ := os.Open("test_data/NonCards.html")
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusOK).Body(file)
f := &Fetcher{}
prices, err := f.getPrices("card")
prices, err := f.GetPrices("card")
assert.Nil(t, err)
expected := []scgCardPrice{
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: "$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: "$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,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: "$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",
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)