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,49 @@
package caching
import (
"fmt"
"testing"
"time"
"github.com/alicebob/miniredis/v2"
"github.com/go-redis/redis"
"github.com/stretchr/testify/assert"
)
func TestGetSet(t *testing.T) {
client, s := getTestClient()
defer s.Close()
keyName := "test_key"
value := "test_value"
client.Set(keyName, value)
val, err := client.Get(keyName)
assert.Nil(t, err)
assert.Equal(t, value, val)
}
func TestExpiration(t *testing.T) {
client, s := getTestClient()
defer s.Close()
client.Expiration = time.Millisecond
keyName := "test_key"
value := "test_value"
client.Set(keyName, value)
s.FastForward(time.Millisecond * 2)
val, err := client.Get(keyName)
assert.Zero(t, val)
assert.NotNil(t, err)
}
func getTestClient() (*CacheClient, *miniredis.Miniredis) {
s, _ := miniredis.Run()
fmt.Println(s.Addr())
c := redis.NewClient(&redis.Options{
Addr: s.Addr(),
})
return &CacheClient{
Storage: c,
Expiration: 0,
}, s
}

View file

@ -0,0 +1,39 @@
package caching
import (
"github.com/go-redis/redis"
"time"
)
type CacheClient struct {
Storage *redis.Client
Expiration time.Duration
}
var client *CacheClient
func GetClient() *CacheClient {
if client != nil {
return client
}
client = new(CacheClient)
client.Init()
return client
}
func (client *CacheClient) Init() {
client.Storage = redis.NewClient(&redis.Options{
Addr: HostName,
Password: Password,
DB: 0,
})
client.Expiration = CacheExpiration
}
func (client *CacheClient) Set(key string, value string) {
client.Storage.Set(key, value, client.Expiration)
}
func (client *CacheClient) Get(key string) (string, error) {
return client.Storage.Get(key).Result()
}

View file

@ -0,0 +1,9 @@
package caching
import (
"time"
)
const HostName = "redis:6379"
const Password = ""
const CacheExpiration = time.Hour * 24

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
}

View file

@ -0,0 +1,23 @@
package dicttranslate
import (
"encoding/json"
"io"
"io/ioutil"
)
func find(query string, dict map[string]string, maxDist int) (string, bool) {
var keys []string
for i := range dict {
keys = append(keys, i)
}
key, f := match(query, keys, maxDist)
return dict[key], f
}
func FindFromReader(query string, reader io.Reader, maxDist int) (string, bool) {
content, _ := ioutil.ReadAll(reader)
dict := map[string]string{}
_ = json.Unmarshal(content, &dict)
return find(query, dict, maxDist)
}

View file

@ -0,0 +1,33 @@
package dicttranslate
import (
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func TestFindEmpty(t *testing.T) {
dict := map[string]string{}
_, f := find("", dict, 0)
assert.False(t, f)
}
func TestFindEntry(t *testing.T) {
dict := map[string]string{
"entry": "value",
}
val, f := find("entry", dict, 0)
assert.True(t, f)
assert.Equal(t, "value", val)
}
func TestFindFromReaderFail(t *testing.T) {
_, f := FindFromReader("entry", strings.NewReader("{}"), 0)
assert.False(t, f)
}
func TestFindFromReaderSuccess(t *testing.T) {
value, f := FindFromReader("entry", strings.NewReader("{\"entry\":\"value\"}"), 0)
assert.True(t, f)
assert.Equal(t, "value", value)
}

View file

@ -0,0 +1,21 @@
package dicttranslate
import "github.com/texttheater/golang-levenshtein/levenshtein"
func match(query string, opts []string, maxDist int) (string, bool) {
bestInd := -1
bestDist := 0
for i, s := range opts {
cfg := levenshtein.DefaultOptions
cfg.SubCost = 1
dist := levenshtein.DistanceForStrings([]rune(s), []rune(query), cfg)
if dist <= maxDist && (bestInd == -1 || dist < bestDist) {
bestInd = i
bestDist = dist
}
}
if bestInd == -1 {
return "", false
}
return opts[bestInd], true
}

View file

@ -0,0 +1,52 @@
package dicttranslate
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestMatch(t *testing.T) {
type testCase struct {
name string
query string
opts []string
shouldFind bool
match string
}
tests := []testCase{
{
name: "No options",
query: "opt",
opts: []string{},
shouldFind: false,
},
{
name: "Match one",
query: "option",
opts: []string{"opt1on"},
shouldFind: true,
match: "opt1on",
},
{
name: "Match exact",
query: "opt1on",
opts: []string{"option", "opt1on"},
shouldFind: true,
match: "opt1on",
},
{
name: "Do not match bad options",
query: "random",
opts: []string{"option", "opt1on"},
shouldFind: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
val, f := match(test.query, test.opts, 1)
assert.Equal(t, test.shouldFind, f)
assert.Equal(t, test.match, val)
})
}
}

94
internal/vk/handlers.go Normal file
View file

@ -0,0 +1,94 @@
package vk
import (
"errors"
"log"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
"gitlab.com/flygrounder/go-mtg-vk/internal/caching"
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
)
var dictPath = "./assets/additional_cards.json"
func HandleMessage(c *gin.Context) {
var req MessageRequest
_ = c.BindJSON(&req)
if req.Secret != SecretKey {
return
}
switch req.Type {
case "confirmation":
handleConfirmation(c, &req)
case "message_new":
go handleSearch(&req)
c.String(http.StatusOK, "ok")
}
}
func handleSearch(req *MessageRequest) {
defer func() {
if r := recover(); r != nil {
log.Printf("[error] Search panicked. Exception info: %s", r)
}
}()
cardName, err := getCardNameByCommand(req.Object.Body)
if err != nil {
Message(req.Object.UserId, "Некорректная команда")
log.Printf("[info] Not correct command. Message: %s user input: %s", err.Error(), req.Object.Body)
} else if cardName == "" {
Message(req.Object.UserId, "Карта не найдена")
log.Printf("[info] Could not find card. User input: %s", req.Object.Body)
} else {
message, err := GetMessage(cardName)
if err != nil {
Message(req.Object.UserId, "Цены временно недоступны, попробуйте позже")
log.Printf("[error] Could not find SCG prices. Message: %s card name: %s", err.Error(), cardName)
return
}
Message(req.Object.UserId, message)
}
}
func GetMessage(cardName string) (string, error) {
client := caching.GetClient()
val, err := client.Get(cardName)
if err != nil {
prices, err := cardsinfo.GetPrices(cardName)
if err != nil {
return "", err
}
message := cardsinfo.FormatCardPrices(cardName, prices)
client.Set(cardName, message)
return message, nil
}
return val, nil
}
func getCardNameByCommand(command string) (string, error) {
var name string
switch {
case strings.HasPrefix(command, "!s"):
split := strings.Split(command, " ")
if len(split) < 3 {
return "", errors.New("wrong command")
}
set := split[1]
number := split[2]
name = cardsinfo.GetNameByCardId(set, number)
default:
dict, _ := os.Open(dictPath)
name = cardsinfo.GetOriginalName(command, dict)
}
return name, nil
}
func handleConfirmation(c *gin.Context, req *MessageRequest) {
if (req.Type == "confirmation") && (req.GroupId == GroupId) {
c.String(http.StatusOK, ConfirmationString)
}
}

37
internal/vk/message.go Normal file
View file

@ -0,0 +1,37 @@
package vk
import (
"encoding/json"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"strconv"
"strings"
)
const SendMessageUrl = "https://api.vk.com/method/messages.send"
func Message(userId int64, message string) {
randomId := rand.Int63()
params := []string{
"access_token=" + Token,
"peer_id=" + strconv.FormatInt(userId, 10),
"message=" + url.QueryEscape(message),
"v=5.95",
"random_id=" + strconv.FormatInt(randomId, 10),
}
paramString := strings.Join(params, "&")
resp, err := http.Get(SendMessageUrl + "?" + paramString)
if err != nil || resp.StatusCode != http.StatusOK {
log.Printf("[error] Could not send message. User: %d", userId)
return
}
responseBytes, _ := ioutil.ReadAll(resp.Body)
var response SendMessageResponse
_ = json.Unmarshal(responseBytes, &response)
if response.Error.ErrorCode != 0 {
log.Printf("[error] Message was not sent. User: %d error message: %s", userId, response.Error.ErrorMsg)
}
}

11
internal/vk/secrets.go Normal file
View file

@ -0,0 +1,11 @@
package vk
import (
"os"
"strconv"
)
var Token = os.Getenv("VK_TOKEN")
var SecretKey = os.Getenv("VK_SECRET_KEY")
var GroupId, _ = strconv.ParseInt(os.Getenv("VK_GROUP_ID"), 10, 64)
var ConfirmationString = os.Getenv("VK_CONFIRMATION_STRING")

22
internal/vk/structs.go Normal file
View file

@ -0,0 +1,22 @@
package vk
type MessageRequest struct {
Type string `json:"type"`
GroupId int64 `json:"group_id"`
Object UserMessage `json:"object"`
Secret string `json:"secret"`
}
type UserMessage struct {
Body string `json:"text"`
UserId int64 `json:"peer_id"`
}
type SendMessageResponse struct {
Error ErrorResponse `json:"error"`
}
type ErrorResponse struct {
ErrorCode int `json:"error_code"`
ErrorMsg string `json:"error_msg"`
}