Added different formatting for telegram bot
This commit is contained in:
parent
35f8fa5a57
commit
89623f5f6a
18 changed files with 233 additions and 173 deletions
|
|
@ -1,9 +1,12 @@
|
||||||
package caching
|
package caching
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis"
|
"github.com/go-redis/redis"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CacheClient struct {
|
type CacheClient struct {
|
||||||
|
|
@ -22,10 +25,17 @@ func NewClient(addr string, passwd string, expiration time.Duration, db int) *Ca
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *CacheClient) Set(key string, value string) {
|
func (client *CacheClient) Set(key string, prices []cardsinfo.ScgCardPrice) {
|
||||||
|
value, _ := json.Marshal(prices)
|
||||||
client.Storage.Set(key, value, client.Expiration)
|
client.Storage.Set(key, value, client.Expiration)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *CacheClient) Get(key string) (string, error) {
|
func (client *CacheClient) Get(key string) ([]cardsinfo.ScgCardPrice, error) {
|
||||||
return client.Storage.Get(key).Result()
|
c, err := client.Storage.Get(key).Result()
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "No such key in cache")
|
||||||
|
}
|
||||||
|
var prices []cardsinfo.ScgCardPrice
|
||||||
|
json.Unmarshal([]byte(c), &prices)
|
||||||
|
return prices, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/alicebob/miniredis/v2"
|
"github.com/alicebob/miniredis/v2"
|
||||||
"github.com/go-redis/redis"
|
"github.com/go-redis/redis"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetClient(t *testing.T) {
|
func TestGetClient(t *testing.T) {
|
||||||
|
|
@ -23,7 +24,13 @@ func TestGetSet(t *testing.T) {
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
|
|
||||||
keyName := "test_key"
|
keyName := "test_key"
|
||||||
value := "test_value"
|
value := []cardsinfo.ScgCardPrice{
|
||||||
|
{
|
||||||
|
Price: "1",
|
||||||
|
Edition: "Alpha",
|
||||||
|
Link: "scg",
|
||||||
|
},
|
||||||
|
}
|
||||||
client.Set(keyName, value)
|
client.Set(keyName, value)
|
||||||
val, err := client.Get(keyName)
|
val, err := client.Get(keyName)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
@ -36,11 +43,11 @@ func TestExpiration(t *testing.T) {
|
||||||
|
|
||||||
client.Expiration = time.Millisecond
|
client.Expiration = time.Millisecond
|
||||||
keyName := "test_key"
|
keyName := "test_key"
|
||||||
value := "test_value"
|
var value []cardsinfo.ScgCardPrice
|
||||||
client.Set(keyName, value)
|
client.Set(keyName, value)
|
||||||
s.FastForward(time.Millisecond * 2)
|
s.FastForward(time.Millisecond * 2)
|
||||||
val, err := client.Get(keyName)
|
val, err := client.Get(keyName)
|
||||||
assert.Zero(t, val)
|
assert.Nil(t, val)
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,13 @@
|
||||||
package cardsinfo
|
package cardsinfo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type scgCardPrice struct {
|
type ScgCardPrice struct {
|
||||||
price string
|
Price string
|
||||||
edition string
|
Edition string
|
||||||
link 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 {
|
type card struct {
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
@ -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)
|
|
||||||
}
|
|
||||||
|
|
@ -11,7 +11,7 @@ import (
|
||||||
const scgDomain = "https://starcitygames.com"
|
const scgDomain = "https://starcitygames.com"
|
||||||
const scgSearchUrlTemplate = "https://starcitygames.hawksearch.com/sites/starcitygames/?search_query="
|
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)
|
prices, err := getPricesScg(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -22,7 +22,7 @@ func (f *Fetcher) getPrices(name string) ([]scgCardPrice, error) {
|
||||||
return prices, nil
|
return prices, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPricesScg(name string) ([]scgCardPrice, error) {
|
func getPricesScg(name string) ([]ScgCardPrice, error) {
|
||||||
escapedName := url.QueryEscape(name)
|
escapedName := url.QueryEscape(name)
|
||||||
searchUrl := scgSearchUrlTemplate + escapedName
|
searchUrl := scgSearchUrlTemplate + escapedName
|
||||||
node, err := htmlquery.LoadURL(searchUrl)
|
node, err := htmlquery.LoadURL(searchUrl)
|
||||||
|
|
@ -30,18 +30,18 @@ func getPricesScg(name string) ([]scgCardPrice, error) {
|
||||||
return nil, errors.Wrap(err, "cannot load url")
|
return nil, errors.Wrap(err, "cannot load url")
|
||||||
}
|
}
|
||||||
blocks := htmlquery.Find(node, "//div[@class=\"hawk-results-item\"]")
|
blocks := htmlquery.Find(node, "//div[@class=\"hawk-results-item\"]")
|
||||||
var results []scgCardPrice
|
var results []ScgCardPrice
|
||||||
for _, block := range blocks {
|
for _, block := range blocks {
|
||||||
price := scgCardPrice{}
|
price := ScgCardPrice{}
|
||||||
linkNode := htmlquery.FindOne(block, "//h2/a")
|
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")
|
editionNode := htmlquery.FindOne(block, "//p[@class=\"hawk-results-item__category\"]/a")
|
||||||
if !strings.HasPrefix(htmlquery.SelectAttr(editionNode, "href"), "/shop/singles/") {
|
if !strings.HasPrefix(htmlquery.SelectAttr(editionNode, "href"), "/shop/singles/") {
|
||||||
continue
|
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 ')]")
|
||||||
price.price = priceNode.FirstChild.Data
|
price.Price = priceNode.FirstChild.Data
|
||||||
results = append(results, price)
|
results = append(results, price)
|
||||||
}
|
}
|
||||||
return results, nil
|
return results, nil
|
||||||
|
|
|
||||||
|
|
@ -16,33 +16,33 @@ func TestGetPrices_Ok(t *testing.T) {
|
||||||
file, _ := os.Open("test_data/AcademyRuinsTest.html")
|
file, _ := os.Open("test_data/AcademyRuinsTest.html")
|
||||||
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusOK).Body(file)
|
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusOK).Body(file)
|
||||||
f := &Fetcher{}
|
f := &Fetcher{}
|
||||||
prices, err := f.getPrices("card")
|
prices, err := f.GetPrices("card")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, []scgCardPrice{
|
assert.Equal(t, []ScgCardPrice{
|
||||||
{
|
{
|
||||||
price: "$6.99",
|
Price: "$6.99",
|
||||||
edition: "Double Masters",
|
Edition: "Double Masters",
|
||||||
link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm-309-enn/?sku=SGL-MTG-2XM-309-ENN1",
|
Link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm-309-enn/?sku=SGL-MTG-2XM-309-ENN1",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
price: "$9.99",
|
Price: "$9.99",
|
||||||
edition: "Double Masters (Foil)",
|
Edition: "Double Masters (Foil)",
|
||||||
link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm-309-enf/?sku=SGL-MTG-2XM-309-ENF1",
|
Link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm-309-enf/?sku=SGL-MTG-2XM-309-ENF1",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
price: "$11.99",
|
Price: "$11.99",
|
||||||
edition: "Double Masters - Variants",
|
Edition: "Double Masters - Variants",
|
||||||
link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm2-369-enn/?sku=SGL-MTG-2XM2-369-ENN1",
|
Link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm2-369-enn/?sku=SGL-MTG-2XM2-369-ENN1",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
price: "$14.99",
|
Price: "$14.99",
|
||||||
edition: "Double Masters - Variants (Foil)",
|
Edition: "Double Masters - Variants (Foil)",
|
||||||
link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm2-369-enf/?sku=SGL-MTG-2XM2-369-ENF1",
|
Link: "https://starcitygames.com/academy-ruins-sgl-mtg-2xm2-369-enf/?sku=SGL-MTG-2XM2-369-ENF1",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
price: "$7.99",
|
Price: "$7.99",
|
||||||
edition: "Modern Masters: 2013 Edition",
|
Edition: "Modern Masters: 2013 Edition",
|
||||||
link: "https://starcitygames.com/academy-ruins-sgl-mtg-mma-219-enn/?sku=SGL-MTG-MMA-219-ENN1",
|
Link: "https://starcitygames.com/academy-ruins-sgl-mtg-mma-219-enn/?sku=SGL-MTG-MMA-219-ENN1",
|
||||||
},
|
},
|
||||||
}, prices)
|
}, prices)
|
||||||
}
|
}
|
||||||
|
|
@ -52,7 +52,7 @@ func TestGetPrices_Unavailable(t *testing.T) {
|
||||||
|
|
||||||
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusBadGateway)
|
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusBadGateway)
|
||||||
f := &Fetcher{}
|
f := &Fetcher{}
|
||||||
_, err := f.getPrices("card")
|
_, err := f.GetPrices("card")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -62,7 +62,7 @@ func TestGetPrices_Empty(t *testing.T) {
|
||||||
file, _ := os.Open("test_data/EmptyTest.html")
|
file, _ := os.Open("test_data/EmptyTest.html")
|
||||||
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusOK).Body(file)
|
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusOK).Body(file)
|
||||||
f := &Fetcher{}
|
f := &Fetcher{}
|
||||||
prices, err := f.getPrices("card")
|
prices, err := f.GetPrices("card")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Nil(t, prices)
|
assert.Nil(t, prices)
|
||||||
}
|
}
|
||||||
|
|
@ -73,33 +73,33 @@ func TestGetPrices_FilterNonCards(t *testing.T) {
|
||||||
file, _ := os.Open("test_data/NonCards.html")
|
file, _ := os.Open("test_data/NonCards.html")
|
||||||
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusOK).Body(file)
|
gock.New(scgSearchUrlTemplate + "card").Reply(http.StatusOK).Body(file)
|
||||||
f := &Fetcher{}
|
f := &Fetcher{}
|
||||||
prices, err := f.getPrices("card")
|
prices, err := f.GetPrices("card")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
expected := []scgCardPrice{
|
expected := []ScgCardPrice{
|
||||||
{
|
{
|
||||||
price: "$72.99",
|
Price: "$72.99",
|
||||||
edition: "3rd Edition - Black Border",
|
Edition: "3rd Edition - Black Border",
|
||||||
link: "https://starcitygames.com/sol-ring-sgl-mtg-3bb-274-frn/?sku=SGL-MTG-3BB-274-FRN3",
|
Link: "https://starcitygames.com/sol-ring-sgl-mtg-3bb-274-frn/?sku=SGL-MTG-3BB-274-FRN3",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
price: "$24.99",
|
Price: "$24.99",
|
||||||
edition: "3rd Edition/Revised",
|
Edition: "3rd Edition/Revised",
|
||||||
link: "https://starcitygames.com/sol-ring-sgl-mtg-3ed-274-enn/?sku=SGL-MTG-3ED-274-ENN1",
|
Link: "https://starcitygames.com/sol-ring-sgl-mtg-3ed-274-enn/?sku=SGL-MTG-3ED-274-ENN1",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
price: "$1,999.99",
|
Price: "$1,999.99",
|
||||||
edition: "Alpha",
|
Edition: "Alpha",
|
||||||
link: "https://starcitygames.com/sol-ring-sgl-mtg-lea-269-enn/?sku=SGL-MTG-LEA-269-ENN1",
|
Link: "https://starcitygames.com/sol-ring-sgl-mtg-lea-269-enn/?sku=SGL-MTG-LEA-269-ENN1",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
price: "$1,199.99",
|
Price: "$1,199.99",
|
||||||
edition: "Beta",
|
Edition: "Beta",
|
||||||
link: "https://starcitygames.com/sol-ring-sgl-mtg-leb-270-enn/?sku=SGL-MTG-LEB-270-ENN1",
|
Link: "https://starcitygames.com/sol-ring-sgl-mtg-leb-270-enn/?sku=SGL-MTG-LEB-270-ENN1",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
price: "$99.99",
|
Price: "$99.99",
|
||||||
edition: "Collectors' Edition",
|
Edition: "Collectors' Edition",
|
||||||
link: "https://starcitygames.com/sol-ring-sgl-mtg-ced-270-enn/?sku=SGL-MTG-CED-270-ENN1",
|
Link: "https://starcitygames.com/sol-ring-sgl-mtg-ced-270-enn/?sku=SGL-MTG-CED-270-ENN1",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
assert.Equal(t, expected, prices)
|
assert.Equal(t, expected, prices)
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -25,18 +27,19 @@ type UserMessage struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type CardCache interface {
|
type CardCache interface {
|
||||||
Get(cardName string) (string, error)
|
Get(cardName string) ([]cardsinfo.ScgCardPrice, error)
|
||||||
Set(cardName string, message string)
|
Set(cardName string, prices []cardsinfo.ScgCardPrice)
|
||||||
}
|
}
|
||||||
|
|
||||||
type CardInfoFetcher interface {
|
type CardInfoFetcher interface {
|
||||||
GetFormattedCardPrices(name string) (string, error)
|
|
||||||
GetNameByCardId(set string, number string) string
|
GetNameByCardId(set string, number string) string
|
||||||
GetOriginalName(name string) string
|
GetOriginalName(name string) string
|
||||||
|
GetPrices(name string) ([]cardsinfo.ScgCardPrice, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Sender interface {
|
type Sender interface {
|
||||||
Send(userId int64, message string)
|
Send(userId int64, message string)
|
||||||
|
SendPrices(userId int64, cardName string, prices []cardsinfo.ScgCardPrice)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scenario) HandleSearch(msg *UserMessage) {
|
func (s *Scenario) HandleSearch(msg *UserMessage) {
|
||||||
|
|
@ -48,29 +51,22 @@ func (s *Scenario) HandleSearch(msg *UserMessage) {
|
||||||
s.Sender.Send(msg.UserId, cardNotFoundMessage)
|
s.Sender.Send(msg.UserId, cardNotFoundMessage)
|
||||||
s.Logger.Printf("[info] Could not find card. User input: %s", msg.Body)
|
s.Logger.Printf("[info] Could not find card. User input: %s", msg.Body)
|
||||||
} else {
|
} else {
|
||||||
message, err := s.getMessage(cardName)
|
prices, err := s.Cache.Get(cardName)
|
||||||
|
if err == nil {
|
||||||
|
s.Sender.SendPrices(msg.UserId, cardName, prices)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
prices, err = s.InfoFetcher.GetPrices(cardName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Sender.Send(msg.UserId, pricesUnavailableMessage)
|
s.Sender.Send(msg.UserId, pricesUnavailableMessage)
|
||||||
s.Logger.Printf("[error] Could not find SCG prices. Message: %s card name: %s", err.Error(), cardName)
|
s.Logger.Printf("[error] Could not find SCG prices. Message: %s card name: %s", err.Error(), cardName)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.Sender.Send(msg.UserId, message)
|
s.Cache.Set(cardName, prices)
|
||||||
|
s.Sender.SendPrices(msg.UserId, cardName, prices)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scenario) getMessage(cardName string) (string, error) {
|
|
||||||
val, err := s.Cache.Get(cardName)
|
|
||||||
if err != nil {
|
|
||||||
message, err := s.InfoFetcher.GetFormattedCardPrices(cardName)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
s.Cache.Set(cardName, message)
|
|
||||||
return message, nil
|
|
||||||
}
|
|
||||||
return val, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Scenario) getCardNameByCommand(command string) (string, error) {
|
func (s *Scenario) getCardNameByCommand(command string) (string, error) {
|
||||||
var name string
|
var name string
|
||||||
switch {
|
switch {
|
||||||
|
|
|
||||||
|
|
@ -77,6 +77,6 @@ func TestScenario_HandleSearch_Uncached(t *testing.T) {
|
||||||
message: "uncached",
|
message: "uncached",
|
||||||
},
|
},
|
||||||
}, testCtx.Sender.sent)
|
}, testCtx.Sender.sent)
|
||||||
msg, _ := testCtx.Scenario.Cache.Get("uncached")
|
_, err := testCtx.Scenario.Cache.Get("uncached")
|
||||||
assert.Equal(t, "uncached", msg)
|
assert.Nil(t, err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,23 @@
|
||||||
package scenario
|
package scenario
|
||||||
|
|
||||||
import "errors"
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||||
|
)
|
||||||
|
|
||||||
type testCache struct {
|
type testCache struct {
|
||||||
table map[string]string
|
table map[string][]cardsinfo.ScgCardPrice
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *testCache) Get(cardName string) (string, error) {
|
func (t *testCache) Get(cardName string) ([]cardsinfo.ScgCardPrice, error) {
|
||||||
msg, ok := t.table[cardName]
|
msg, ok := t.table[cardName]
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", errors.New("test")
|
return nil, errors.New("test")
|
||||||
}
|
}
|
||||||
return msg, nil
|
return msg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *testCache) Set(cardName string, message string) {
|
func (t *testCache) Set(cardName string, prices []cardsinfo.ScgCardPrice) {
|
||||||
t.table[cardName] = message
|
t.table[cardName] = prices
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@ package scenario
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TestScenarioCtx struct {
|
type TestScenarioCtx struct {
|
||||||
|
|
@ -21,8 +23,14 @@ func GetTestScenarioCtx() TestScenarioCtx {
|
||||||
Logger: log.New(buf, "", 0),
|
Logger: log.New(buf, "", 0),
|
||||||
InfoFetcher: &testInfoFetcher{},
|
InfoFetcher: &testInfoFetcher{},
|
||||||
Cache: &testCache{
|
Cache: &testCache{
|
||||||
table: map[string]string{
|
table: map[string][]cardsinfo.ScgCardPrice{
|
||||||
"good": "good",
|
"good": {
|
||||||
|
{
|
||||||
|
Price: "1",
|
||||||
|
Edition: "alpha",
|
||||||
|
Link: "scg",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -2,15 +2,17 @@ package scenario
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||||
)
|
)
|
||||||
|
|
||||||
type testInfoFetcher struct{}
|
type testInfoFetcher struct{}
|
||||||
|
|
||||||
func (t *testInfoFetcher) GetFormattedCardPrices(name string) (string, error) {
|
func (t *testInfoFetcher) GetPrices(name string) ([]cardsinfo.ScgCardPrice, error) {
|
||||||
if name == "good" || name == "uncached" {
|
if name == "good" || name == "uncached" {
|
||||||
return name, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
return "", errors.New("test")
|
return nil, errors.New("test")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *testInfoFetcher) GetNameByCardId(_ string, _ string) string {
|
func (t *testInfoFetcher) GetNameByCardId(_ string, _ string) string {
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,18 @@
|
||||||
package scenario
|
package scenario
|
||||||
|
|
||||||
|
import "gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||||
|
|
||||||
type testSender struct {
|
type testSender struct {
|
||||||
sent []testMessage
|
sent []testMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *testSender) SendPrices(userId int64, cardName string, prices []cardsinfo.ScgCardPrice) {
|
||||||
|
s.sent = append(s.sent, testMessage{
|
||||||
|
userId: userId,
|
||||||
|
message: cardName,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
type testMessage struct {
|
type testMessage struct {
|
||||||
userId int64
|
userId int64
|
||||||
message string
|
message string
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,38 @@
|
||||||
package telegram
|
package telegram
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
||||||
|
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Sender struct {
|
type Sender struct {
|
||||||
API *tgbotapi.BotAPI
|
API *tgbotapi.BotAPI
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Sender) SendPrices(userId int64, cardName string, prices []cardsinfo.ScgCardPrice) {
|
||||||
|
msg := formatCardPrices(cardName, prices)
|
||||||
|
s.Send(userId, msg)
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Sender) Send(userId int64, message string) {
|
func (h *Sender) Send(userId int64, message string) {
|
||||||
msg := tgbotapi.NewMessage(userId, message)
|
msg := tgbotapi.NewMessage(userId, message)
|
||||||
msg.DisableWebPagePreview = true
|
msg.DisableWebPagePreview = true
|
||||||
h.API.Send(msg)
|
h.API.Send(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func formatCardPrices(name string, prices []cardsinfo.ScgCardPrice) string {
|
||||||
|
message := fmt.Sprintf("Оригинальное название: %v\n\n", name)
|
||||||
|
for i, v := range prices {
|
||||||
|
message += fmt.Sprintf("%v. %v", i+1, formatPrice(v))
|
||||||
|
}
|
||||||
|
if len(prices) == 0 {
|
||||||
|
message += "Цен не найдено\n"
|
||||||
|
}
|
||||||
|
return message
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatPrice(s cardsinfo.ScgCardPrice) string {
|
||||||
|
return fmt.Sprintf("[%v](%v): %v\n", s.Edition, s.Link, s.Price)
|
||||||
|
}
|
||||||
|
|
|
||||||
25
internal/telegram/sender_test.go
Normal file
25
internal/telegram/sender_test.go
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
package telegram
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_formatCardPrices(t *testing.T) {
|
||||||
|
prices := []cardsinfo.ScgCardPrice{
|
||||||
|
{
|
||||||
|
Price: "1",
|
||||||
|
Edition: "Alpha",
|
||||||
|
Link: "scg1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Price: "2",
|
||||||
|
Edition: "Beta",
|
||||||
|
Link: "scg2",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
result := formatCardPrices("card", prices)
|
||||||
|
assert.Equal(t, "Оригинальное название: card\n\n1. [Alpha](scg1): 1\n2. [Beta](scg2): 2\n", result)
|
||||||
|
}
|
||||||
22
internal/vk/format.go
Normal file
22
internal/vk/format.go
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
package vk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func formatCardPrices(name string, prices []cardsinfo.ScgCardPrice) string {
|
||||||
|
message := fmt.Sprintf("Оригинальное название: %v\n\n", name)
|
||||||
|
for i, v := range prices {
|
||||||
|
message += fmt.Sprintf("%v. %v", i+1, formatPrice(v))
|
||||||
|
}
|
||||||
|
if len(prices) == 0 {
|
||||||
|
message += "Цен не найдено\n"
|
||||||
|
}
|
||||||
|
return message
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatPrice(s cardsinfo.ScgCardPrice) string {
|
||||||
|
return fmt.Sprintf("%v: %v\n%v\n", s.Edition, s.Price, s.Link)
|
||||||
|
}
|
||||||
19
internal/vk/format_test.go
Normal file
19
internal/vk/format_test.go
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
package vk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFormatCardPrices(t *testing.T) {
|
||||||
|
formatted := formatCardPrices("card", []cardsinfo.ScgCardPrice{
|
||||||
|
{
|
||||||
|
Price: "1.5$",
|
||||||
|
Edition: "ED",
|
||||||
|
Link: "scg.com",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
assert.Equal(t, "Оригинальное название: card\n\n1. ED: 1.5$\nscg.com\n", formatted)
|
||||||
|
}
|
||||||
|
|
@ -9,6 +9,8 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||||
)
|
)
|
||||||
|
|
||||||
const sendMessageUrl = "https://api.vk.com/method/messages.send"
|
const sendMessageUrl = "https://api.vk.com/method/messages.send"
|
||||||
|
|
@ -22,6 +24,11 @@ type ApiSender struct {
|
||||||
Logger *log.Logger
|
Logger *log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *ApiSender) SendPrices(userId int64, cardName string, prices []cardsinfo.ScgCardPrice) {
|
||||||
|
msg := formatCardPrices(cardName, prices)
|
||||||
|
s.Send(userId, msg)
|
||||||
|
}
|
||||||
|
|
||||||
type sendMessageResponse struct {
|
type sendMessageResponse struct {
|
||||||
Error errorResponse `json:"error"`
|
Error errorResponse `json:"error"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue