Refactored code and added logging

This commit is contained in:
Artyom Belousov 2019-11-03 12:16:45 +03:00
parent 8830920900
commit 221ab214cc
11 changed files with 51 additions and 44 deletions

View file

@ -8,26 +8,26 @@ import (
"strings"
)
const SCRYFALL_URL = "https://api.scryfall.com"
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.
Note: number is string because some cards contain letters in their numbers.
*/
path := SCRYFALL_URL + "/cards/" + strings.ToLower(set) + "/" + number
path := ScryfallUrl + "/cards/" + strings.ToLower(set) + "/" + number
return GetCardByUrl(path)
}
func GetOriginalName(name string) string {
path := SCRYFALL_URL + "/cards/named?fuzzy=" + ApplyFilters(name)
path := ScryfallUrl + "/cards/named?fuzzy=" + ApplyFilters(name)
return GetCardByUrl(path)
}
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
*/
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)
}
@ -43,6 +43,9 @@ func GetCardByUrl(path string) string {
return ""
}
var v Card
json.Unmarshal(data, &v)
err = json.Unmarshal(data, &v)
if err != nil {
return ""
}
return v.getName()
}

View file

@ -8,7 +8,7 @@ import (
"strings"
)
const SCGURL = "http://www.starcitygames.com/results?name="
const Scgurl = "http://www.starcitygames.com/results?name="
func GetSCGPrices(name string) ([]CardPrice, error) {
preprocessedName := preprocessNameForSearch(name)
@ -69,7 +69,7 @@ func getPriceContainers(doc *html.Node) []*html.Node {
func fetchPrice(price string) (float64, error) {
split := strings.Split(price, "$")
if len(split) < 2 {
return 0, errors.New("Not enough values")
return 0, errors.New("not enough values")
}
p := split[1]
v, err := strconv.ParseFloat(p, 64)
@ -77,9 +77,9 @@ func fetchPrice(price string) (float64, error) {
}
func getSCGUrl(name string) string {
splitted := strings.Split(name, " ")
scgName := strings.Join(splitted, "+")
url := SCGURL + scgName
words := strings.Split(name, " ")
scgName := strings.Join(words, "+")
url := Scgurl + scgName
return url
}