Refactor A LOT OF STUFF
This commit is contained in:
parent
e25cfa7d52
commit
4d8f57e633
2 changed files with 75 additions and 75 deletions
136
main.go
136
main.go
|
@ -78,55 +78,14 @@ func webhook(update tg.APIUpdate) {
|
||||||
}
|
}
|
||||||
photos := make([]tg.APIInlineQueryResultPhoto, len(results.Data))
|
photos := make([]tg.APIInlineQueryResultPhoto, len(results.Data))
|
||||||
for i, card := range results.Data {
|
for i, card := range results.Data {
|
||||||
buttons := []tg.APIInlineKeyboardButton{
|
face, caption, buttons := getCardEntry(card, 0)
|
||||||
{
|
|
||||||
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),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
photos[i] = tg.APIInlineQueryResultPhoto{
|
photos[i] = tg.APIInlineQueryResultPhoto{
|
||||||
Type: "photo",
|
Type: "photo",
|
||||||
ResultID: card.ID,
|
ResultID: card.ID,
|
||||||
PhotoURL: face.Large,
|
PhotoURL: face.Large,
|
||||||
ThumbURL: face.Normal,
|
ThumbURL: face.Normal,
|
||||||
Title: card.Name,
|
Title: card.Name,
|
||||||
Caption: strings.Join(captions, " - "),
|
Caption: caption,
|
||||||
Width: 672,
|
Width: 672,
|
||||||
Height: 936,
|
Height: 936,
|
||||||
ReplyMarkup: &tg.APIInlineKeyboardMarkup{
|
ReplyMarkup: &tg.APIInlineKeyboardMarkup{
|
||||||
|
@ -161,7 +120,7 @@ func webhook(update tg.APIUpdate) {
|
||||||
cardmedia := []tg.APIInputMediaPhoto{}
|
cardmedia := []tg.APIInputMediaPhoto{}
|
||||||
errlist := []string{}
|
errlist := []string{}
|
||||||
for _, cardname := range requests {
|
for _, cardname := range requests {
|
||||||
card, err := scryfallGetCard(cardname)
|
card, err := scryfallGetCardByName(cardname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errlist = append(errlist, cardname)
|
errlist = append(errlist, cardname)
|
||||||
} else {
|
} else {
|
||||||
|
@ -199,38 +158,24 @@ func webhook(update tg.APIUpdate) {
|
||||||
|
|
||||||
// Handle inline callback requests (flipped cards)
|
// Handle inline callback requests (flipped cards)
|
||||||
if update.Callback != nil {
|
if update.Callback != nil {
|
||||||
var data CardFaceFlipPics
|
if update.Callback.Data != nil && strings.HasPrefix(*update.Callback.Data, "FLIP") {
|
||||||
err := json.Unmarshal([]byte(*update.Callback.Data), &data)
|
parts := strings.SplitN(*update.Callback.Data, ",", 3)
|
||||||
if err == nil {
|
card, err := scryfallGetCardByID(parts[1])
|
||||||
buttons := []tg.APIInlineKeyboardButton{
|
facenum, _ := strconv.Atoi(parts[2])
|
||||||
{
|
if err == nil {
|
||||||
Text: "Scryfall",
|
face, _, buttons := getCardEntry(card, facenum)
|
||||||
URL: data.Scryfall,
|
api.EditMedia(tg.ClientEditMediaData{
|
||||||
},
|
ChatID: update.Message.Chat.ChatID,
|
||||||
}
|
MessageID: update.Message.MessageID,
|
||||||
if data.Edhrec != "" {
|
Media: tg.APIInputMediaPhoto{
|
||||||
buttons = append(buttons, tg.APIInlineKeyboardButton{
|
Type: "photo",
|
||||||
Text: "EDHREC",
|
Media: face.Large,
|
||||||
URL: data.Edhrec,
|
},
|
||||||
|
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 {
|
} else {
|
||||||
fmt.Println("Unknown callback: ", *update.Callback.Data)
|
fmt.Println("Unknown callback: ", *update.Callback.Data)
|
||||||
}
|
}
|
||||||
|
@ -256,3 +201,46 @@ func getCardRequests(str string) (out []string) {
|
||||||
}
|
}
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
|
14
scryfall.go
14
scryfall.go
|
@ -81,7 +81,7 @@ func scryfallSearch(query string, offset int) (results CardSearchResults, err er
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func scryfallGetCard(cardname string) (card CardData, err error) {
|
func scryfallGetCardByName(cardname string) (card CardData, err error) {
|
||||||
cardname = url.QueryEscape(cardname)
|
cardname = url.QueryEscape(cardname)
|
||||||
requrl := fmt.Sprintf("https://api.scryfall.com/cards/named?fuzzy=%s", cardname)
|
requrl := fmt.Sprintf("https://api.scryfall.com/cards/named?fuzzy=%s", cardname)
|
||||||
response, err := netClient.Get(requrl)
|
response, err := netClient.Get(requrl)
|
||||||
|
@ -93,3 +93,15 @@ func scryfallGetCard(cardname string) (card CardData, err error) {
|
||||||
err = json.NewDecoder(response.Body).Decode(&card)
|
err = json.NewDecoder(response.Body).Decode(&card)
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue