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" "flag"
"fmt" "fmt"
"os" "os"
"strconv"
"github.com/hamcha/tg" "github.com/hamcha/tg"
) )
@ -52,14 +53,18 @@ func webhook(update tg.APIUpdate) {
go func() { go func() {
query := update.Inline.Query query := update.Inline.Query
//TODO OFFSET offset, _ := strconv.Atoi(update.Inline.Offset)
results, err := scryfallSearch(query) results, err := scryfallSearch(query, offset)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
// DO SOMETHING // DO SOMETHING
return return
} }
nextcard := ""
if results.HasMore {
nextcard = strconv.Itoa(offset + len(results.Data))
}
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 {
caption := fmt.Sprintf("EDHREC rank: #%d - cardmarket: € %s", card.EdhrecRank, card.Eur) 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{ err = api.AnswerInlineQuery(tg.InlineQueryResponse{
QueryID: update.Inline.QueryID, QueryID: update.Inline.QueryID,
Results: photos, Results: photos,
NextOffset: nextcard,
}) })
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)

View file

@ -2,6 +2,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"net/url" "net/url"
"time" "time"
@ -53,14 +54,18 @@ var netClient = &http.Client{
Timeout: time.Second * 10, 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) 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 { if err != nil {
return return
} }
defer response.Body.Close() defer response.Body.Close()
err = json.NewDecoder(response.Body).Decode(&results) err = json.NewDecoder(response.Body).Decode(&results)
results.Data = results.Data[cardoffset:]
return return
} }