-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01f11cb
commit 60e406c
Showing
3 changed files
with
116 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
import "github.com/bobesa/chalk" | ||
|
||
func printQuote(quote Quote, showTags bool) { | ||
if showTags { | ||
tagString := chalk.White().Faint().Sprint(quote.Tags) | ||
chalk.Cyan().Italic().Printf("%s %s\n", quote.Body, tagString) | ||
} else { | ||
chalk.Cyan().Italic().Printf("%s\n", quote.Body) | ||
} | ||
if quote.Author != "" { | ||
chalk.Magenta().Printf("%s\n", quote.Author) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/dghubble/sling" | ||
"os" | ||
) | ||
|
||
var ( | ||
quoteApi = sling.New().Base("https://favqs.com/api/") | ||
qotdPath = "qotd.json" | ||
quotesPath = "quotes" | ||
defaultApiKey = "1501a5726d3be7d5839dc9085ba47848" | ||
favqsApiKey string | ||
) | ||
|
||
func init() { | ||
var ok bool | ||
favqsApiKey, ok = os.LookupEnv("FAVQS_API_KEY") | ||
if !ok { | ||
favqsApiKey = defaultApiKey | ||
} | ||
} | ||
|
||
type Quote struct { | ||
Id int `json:"id"` | ||
Body string `json:"body"` | ||
Author string `json:"author"` | ||
Tags []string `json:"tags"` | ||
} | ||
|
||
type QOTDResponse struct { | ||
Quote Quote `json:"quote"` | ||
} | ||
|
||
type QuotesResponse struct { | ||
Quotes []Quote `json:"quotes"` | ||
} | ||
|
||
// Quotes params | ||
type QuoteParams struct { | ||
Filter string `url:"filter,omitempty"` | ||
Type string `url:"type,omitempty"` | ||
Page int `url:"page,omitempty"` | ||
} | ||
|
||
func GetRandomQuote() (Quote, error) { | ||
qotdResponse := QOTDResponse{} | ||
_, err := quoteApi.Get(qotdPath).ReceiveSuccess(&qotdResponse) | ||
return qotdResponse.Quote, err | ||
} | ||
|
||
func GetQuotes(search, author, tag string, max int) ([]Quote, error) { | ||
quotesResponse := QuotesResponse{} | ||
quotesQuery := QuoteParams{ | ||
Filter: search, | ||
} | ||
if author != "" { | ||
quotesQuery.Filter = author | ||
quotesQuery.Type = "author" | ||
} | ||
if tag != "" { | ||
quotesQuery.Filter = tag | ||
quotesQuery.Type = "tag" | ||
} | ||
_, err := quoteApi.Get(quotesPath).Add("Authorization", fmt.Sprintf("Token token=%s", favqsApiKey)).QueryStruct("esQuery).ReceiveSuccess("esResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
quotes := quotesResponse.Quotes | ||
if max < len(quotes) { | ||
quotes = quotes[:max] | ||
} | ||
return quotes, nil | ||
} |