From 5578ecef2ad75539d38b959e7f8f2e45eb3df1dd Mon Sep 17 00:00:00 2001 From: Hamcha Date: Mon, 17 Sep 2018 15:15:19 +0200 Subject: [PATCH] Tentative support for pages --- main.go | 14 ++++++++++---- scryfall.go | 9 +++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index f79e9ae..c102565 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/scryfall.go b/scryfall.go index 5abbf88..19d30ca 100644 --- a/scryfall.go +++ b/scryfall.go @@ -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 }