Refactor A LOT OF STUFF

This commit is contained in:
Hamcha 2018-12-13 13:55:39 +01:00
parent e25cfa7d52
commit 4d8f57e633
Signed by: hamcha
GPG key ID: A40413D21021EAEE
2 changed files with 75 additions and 75 deletions

136
main.go
View file

@ -78,55 +78,14 @@ func webhook(update tg.APIUpdate) {
}
photos := make([]tg.APIInlineQueryResultPhoto, len(results.Data))
for i, card := range results.Data {
buttons := []tg.APIInlineKeyboardButton{
{
Text: "Scryfall",
URL: card.ScryfallURI,
},
}
if card.RelatedUris.Edhrec != "" {
buttons = append(buttons, tg.APIInlineKeyboardButton{
Text: "EDHREC",
URL: card.RelatedUris.Edhrec,
})
}
if card.PurchaseUris.Cardmarket != "" {
buttons = append(buttons, tg.APIInlineKeyboardButton{
Text: "MCM",
URL: card.PurchaseUris.Cardmarket,
})
}
captions := []string{}
if card.EdhrecRank != nil {
captions = append(captions, fmt.Sprintf("EDHREC rank: #%d", *card.EdhrecRank))
}
if card.Eur != "" {
captions = append(captions, fmt.Sprintf("cardmarket: € %s", card.Eur))
}
face := card.ImageUris
if card.ImageUris.Large == "" && card.CardFaces != nil {
face = card.CardFaces[0].ImageUris
facedata, _ := json.Marshal(CardFaceFlipPics{
Normal: face.Large,
Flipped: card.CardFaces[1].ImageUris.Large,
Scryfall: card.ScryfallURI,
Edhrec: card.RelatedUris.Edhrec,
MCM: card.PurchaseUris.Cardmarket,
})
buttons = append(buttons, tg.APIInlineKeyboardButton{
Text: "🔄",
CallbackData: string(facedata),
})
}
face, caption, buttons := getCardEntry(card, 0)
photos[i] = tg.APIInlineQueryResultPhoto{
Type: "photo",
ResultID: card.ID,
PhotoURL: face.Large,
ThumbURL: face.Normal,
Title: card.Name,
Caption: strings.Join(captions, " - "),
Caption: caption,
Width: 672,
Height: 936,
ReplyMarkup: &tg.APIInlineKeyboardMarkup{
@ -161,7 +120,7 @@ func webhook(update tg.APIUpdate) {
cardmedia := []tg.APIInputMediaPhoto{}
errlist := []string{}
for _, cardname := range requests {
card, err := scryfallGetCard(cardname)
card, err := scryfallGetCardByName(cardname)
if err != nil {
errlist = append(errlist, cardname)
} else {
@ -199,38 +158,24 @@ func webhook(update tg.APIUpdate) {
// Handle inline callback requests (flipped cards)
if update.Callback != nil {
var data CardFaceFlipPics
err := json.Unmarshal([]byte(*update.Callback.Data), &data)
if err == nil {
buttons := []tg.APIInlineKeyboardButton{
{
Text: "Scryfall",
URL: data.Scryfall,
},
}
if data.Edhrec != "" {
buttons = append(buttons, tg.APIInlineKeyboardButton{
Text: "EDHREC",
URL: data.Edhrec,
if update.Callback.Data != nil && strings.HasPrefix(*update.Callback.Data, "FLIP") {
parts := strings.SplitN(*update.Callback.Data, ",", 3)
card, err := scryfallGetCardByID(parts[1])
facenum, _ := strconv.Atoi(parts[2])
if err == nil {
face, _, buttons := getCardEntry(card, facenum)
api.EditMedia(tg.ClientEditMediaData{
ChatID: update.Message.Chat.ChatID,
MessageID: update.Message.MessageID,
Media: tg.APIInputMediaPhoto{
Type: "photo",
Media: face.Large,
},
ReplyMarkup: &tg.APIInlineKeyboardMarkup{
InlineKeyboard: [][]tg.APIInlineKeyboardButton{buttons},
},
})
}
if data.MCM != "" {
buttons = append(buttons, tg.APIInlineKeyboardButton{
Text: "MCM",
URL: data.MCM,
})
}
api.EditMedia(tg.ClientEditMediaData{
ChatID: update.Message.Chat.ChatID,
MessageID: update.Message.MessageID,
Media: tg.APIInputMediaPhoto{
Type: "photo",
Media: data.Flipped,
},
ReplyMarkup: &tg.APIInlineKeyboardMarkup{
InlineKeyboard: [][]tg.APIInlineKeyboardButton{buttons},
},
})
} else {
fmt.Println("Unknown callback: ", *update.Callback.Data)
}
@ -256,3 +201,46 @@ func getCardRequests(str string) (out []string) {
}
return
}
func getCardEntry(card CardData, facenum int) (CardImage, string, []tg.APIInlineKeyboardButton) {
buttons := []tg.APIInlineKeyboardButton{
{
Text: "Scryfall",
URL: card.ScryfallURI,
},
}
if card.RelatedUris.Edhrec != "" {
buttons = append(buttons, tg.APIInlineKeyboardButton{
Text: "EDHREC",
URL: card.RelatedUris.Edhrec,
})
}
if card.PurchaseUris.Cardmarket != "" {
buttons = append(buttons, tg.APIInlineKeyboardButton{
Text: "MCM",
URL: card.PurchaseUris.Cardmarket,
})
}
captions := []string{}
if card.EdhrecRank != nil {
captions = append(captions, fmt.Sprintf("EDHREC rank: #%d", *card.EdhrecRank))
}
if card.Eur != "" {
captions = append(captions, fmt.Sprintf("cardmarket: € %s", card.Eur))
}
face := card.ImageUris
if card.ImageUris.Large == "" && card.CardFaces != nil {
face = card.CardFaces[facenum].ImageUris
num := 1
if facenum != 0 {
num = 0
}
buttons = append(buttons, tg.APIInlineKeyboardButton{
Text: "🔄",
CallbackData: fmt.Sprintf("FLIP,%s,%d", card.ID, num),
})
}
return face, strings.Join(captions, " - "), buttons
}

View file

@ -81,7 +81,7 @@ func scryfallSearch(query string, offset int) (results CardSearchResults, err er
return
}
func scryfallGetCard(cardname string) (card CardData, err error) {
func scryfallGetCardByName(cardname string) (card CardData, err error) {
cardname = url.QueryEscape(cardname)
requrl := fmt.Sprintf("https://api.scryfall.com/cards/named?fuzzy=%s", cardname)
response, err := netClient.Get(requrl)
@ -93,3 +93,15 @@ func scryfallGetCard(cardname string) (card CardData, err error) {
err = json.NewDecoder(response.Body).Decode(&card)
return
}
func scryfallGetCardByID(id string) (card CardData, err error) {
requrl := fmt.Sprintf("https://api.scryfall.com/cards/%s", id)
response, err := netClient.Get(requrl)
if err != nil {
return
}
defer response.Body.Close()
err = json.NewDecoder(response.Body).Decode(&card)
return
}