Tentative support for pages

This commit is contained in:
Hamcha 2018-09-17 15:15:19 +02:00
parent 4a250024c9
commit 5578ecef2a
Signed by: hamcha
GPG key ID: A40413D21021EAEE
2 changed files with 17 additions and 6 deletions

14
main.go
View file

@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"os"
"strconv"
"github.com/hamcha/tg"
)
@ -52,14 +53,18 @@ func webhook(update tg.APIUpdate) {
go func() {
query := update.Inline.Query
//TODO OFFSET
results, err := scryfallSearch(query)
offset, _ := strconv.Atoi(update.Inline.Offset)
results, err := scryfallSearch(query, offset)
if err != nil {
fmt.Println(err)
// DO SOMETHING
return
}
nextcard := ""
if results.HasMore {
nextcard = strconv.Itoa(offset + len(results.Data))
}
photos := make([]tg.APIInlineQueryResultPhoto, len(results.Data))
for i, card := range results.Data {
caption := fmt.Sprintf("EDHREC rank: #%d - cardmarket: € %s", card.EdhrecRank, card.Eur)
@ -88,8 +93,9 @@ func webhook(update tg.APIUpdate) {
}
err = api.AnswerInlineQuery(tg.InlineQueryResponse{
QueryID: update.Inline.QueryID,
Results: photos,
QueryID: update.Inline.QueryID,
Results: photos,
NextOffset: nextcard,
})
if err != nil {
fmt.Println(err)

View file

@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
@ -53,14 +54,18 @@ var netClient = &http.Client{
Timeout: time.Second * 10,
}
func scryfallSearch(query string) (results CardSearchResults, err error) {
func scryfallSearch(query string, offset int) (results CardSearchResults, err error) {
query = url.QueryEscape(query)
response, err := netClient.Get("https://api.scryfall.com/cards/search?order=name&q=" + query)
page := offset / 175
cardoffset := offset % 175
requrl := fmt.Sprintf("https://api.scryfall.com/cards/search?include_multilingual=true&order=name&page=%d&q=%s", page, query)
response, err := netClient.Get(requrl)
if err != nil {
return
}
defer response.Body.Close()
err = json.NewDecoder(response.Body).Decode(&results)
results.Data = results.Data[cardoffset:]
return
}