From 74544efed3ada86d84ea74714f3c17562fd38144 Mon Sep 17 00:00:00 2001 From: Louis Beaumont Date: Sat, 31 Oct 2020 13:58:48 +0100 Subject: [PATCH] Implement basic GoodReads templates (#18) --- README.md | 28 ++++++++++++++++++++++++++++ gists.go | 2 +- go.mod | 1 + go.sum | 6 ++++++ goodreads.go | 21 +++++++++++++++++++++ main.go | 23 ++++++++++++++++------- repos.go | 6 +++--- sponsors.go | 2 +- users.go | 4 ++-- 9 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 goodreads.go diff --git a/README.md b/README.md index ac4e096..5d0b6dc 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,26 @@ Created: {{humanize .CreatedAt}} This function requires GitHub authentication! +### Your GoodReads reviews + +``` +{{range goodReadsReviews 5}} +- {{.Book.Title}} - {{.Book.Link}} - {{.Rating}} - {{humanize .DateUpdated}} +{{- end}} +``` + +This function requires GoodReads API key! + +### Your GoodReads currently reading books + +``` +{{range goodReadsCurrentlyReading 5}} +- {{.Book.Title}} - {{.Book.Link}} - {{humanize .DateUpdated}} +{{- end}} +``` + +This function requires GoodReads API key! + ## Template Engine markscribe uses Go's powerful template engine. You can find its documentation @@ -152,6 +172,14 @@ create a new token by going to your profile settings: `Developer settings` > `Personal access tokens` > `Generate new token` +## GoodReads API key + +In order to access some of GoodReads' API, markscribe requires you to provide a +valid GoodReads key in an environment variable called `GOODREADS_TOKEN`. You can +create a new token by going [here](https://www.goodreads.com/api/keys). +Then you need to go to your repository and add it, `Settings -> Secrets -> New secret`. +You also need to set your GoodReads user ID in your secrets as `GOODREADS_USER_ID`. + ## FAQ Q: That's awesome, but can you expose more APIs and data? diff --git a/gists.go b/gists.go index 9051bf1..a903772 100644 --- a/gists.go +++ b/gists.go @@ -27,7 +27,7 @@ func gists(count int) []Gist { "username": githubv4.String(username), "count": githubv4.Int(count), } - err := client.Query(context.Background(), &gistsQuery, variables) + err := gitHubClient.Query(context.Background(), &gistsQuery, variables) if err != nil { panic(err) } diff --git a/go.mod b/go.mod index 4263d92..2adfad9 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/muesli/markscribe go 1.14 require ( + github.com/KyleBanks/goodreads v0.0.0-20200527082926-28539417959b github.com/dustin/go-humanize v1.0.0 github.com/golang/protobuf v1.3.2 // indirect github.com/mmcdole/gofeed v1.0.0 diff --git a/go.sum b/go.sum index d32f2df..710d5f9 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,11 @@ +github.com/KyleBanks/goodreads v0.0.0-20200527082926-28539417959b h1:PH3E8P/BzsV5duxi1yFgxWokNrn9KcwWJ2MI83qHBME= +github.com/KyleBanks/goodreads v0.0.0-20200527082926-28539417959b/go.mod h1:7+z+03v0GI8UMBav4Iaqajq/ARtCx9jcX5RONXP3iRM= github.com/PuerkitoBio/goquery v1.5.0 h1:uGvmFXOA73IKluu/F84Xd1tt/z07GYm8X49XKHP7EJk= github.com/PuerkitoBio/goquery v1.5.0/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg= github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o= github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= github.com/codegangsta/cli v1.20.0/go.mod h1:/qJNoX69yVSKu5o4jLyXAENLRyk1uhi7zkbQ3slBdOA= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= @@ -19,8 +22,11 @@ github.com/shurcooL/githubv4 v0.0.0-20191127044304-8f68eb5628d0 h1:T9uus1QvcPgeL github.com/shurcooL/githubv4 v0.0.0-20191127044304-8f68eb5628d0/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo= github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f h1:tygelZueB1EtXkPI6mQ4o9DQ0+FKW41hTbunoXZCTqk= github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f/go.mod h1:AuYgA5Kyo4c7HfUmvRGs/6rGlMMV/6B1bVnB9JxJEEg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= diff --git a/goodreads.go b/goodreads.go new file mode 100644 index 0000000..1962a9c --- /dev/null +++ b/goodreads.go @@ -0,0 +1,21 @@ +package main + +import ( + "github.com/KyleBanks/goodreads/responses" +) + +func goodReadsReviews(count int) []responses.Review { + reviews, err := goodReadsClient.ReviewList(goodReadsID, "read", "date_read", "", "d", 1, count) + if err != nil { + panic(err) + } + return reviews +} + +func goodReadsCurrentlyReading(count int) []responses.Review { + reviews, err := goodReadsClient.ReviewList(goodReadsID, "currently-reading", "date_updated", "", "d", 1, count) + if err != nil { + panic(err) + } + return reviews +} diff --git a/main.go b/main.go index 14a9480..da6a320 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "context" "flag" "fmt" + "github.com/KyleBanks/goodreads" "html/template" "io/ioutil" "net/http" @@ -14,8 +15,10 @@ import ( ) var ( - client *githubv4.Client - username string + gitHubClient *githubv4.Client + goodReadsClient *goodreads.Client + goodReadsID string + username string write = flag.String("write", "", "write output to") ) @@ -44,6 +47,9 @@ func main() { "sponsors": sponsors, /* RSS */ "rss": rssFeed, + /* GoodReads */ + "goodReadsReviews": goodReadsReviews, + "goodReadsCurrentlyReading": goodReadsCurrentlyReading, /* Utils */ "humanize": humanized, }).Parse(string(tplIn)) @@ -53,16 +59,19 @@ func main() { } var httpClient *http.Client - token := os.Getenv("GITHUB_TOKEN") - if len(token) > 0 { + gitHubToken := os.Getenv("GITHUB_TOKEN") + goodReadsToken := os.Getenv("GOODREADS_TOKEN") + goodReadsID = os.Getenv("GOODREADS_USER_ID") + if len(gitHubToken) > 0 { httpClient = oauth2.NewClient(context.Background(), oauth2.StaticTokenSource( - &oauth2.Token{AccessToken: token}, + &oauth2.Token{AccessToken: gitHubToken}, )) } - client = githubv4.NewClient(httpClient) + gitHubClient = githubv4.NewClient(httpClient) + goodReadsClient = goodreads.NewClient(goodReadsToken) - if len(token) > 0 { + if len(gitHubToken) > 0 { username, err = getUsername() if err != nil { fmt.Println("Can't retrieve GitHub profile:", err) diff --git a/repos.go b/repos.go index 4a1fe6a..e63ea3a 100644 --- a/repos.go +++ b/repos.go @@ -63,7 +63,7 @@ func recentContributions(count int) []Contribution { variables := map[string]interface{}{ "username": githubv4.String(username), } - err := client.Query(context.Background(), &recentContributionsQuery, variables) + err := gitHubClient.Query(context.Background(), &recentContributionsQuery, variables) if err != nil { panic(err) } @@ -104,7 +104,7 @@ func recentRepos(count int) []Repo { "username": githubv4.String(username), "count": githubv4.Int(count + 1), // +1 in case we encounter the meta-repo itself } - err := client.Query(context.Background(), &recentReposQuery, variables) + err := gitHubClient.Query(context.Background(), &recentReposQuery, variables) if err != nil { panic(err) } @@ -136,7 +136,7 @@ func recentReleases(count int) []Repo { "username": githubv4.String(username), "after": after, } - err := client.Query(context.Background(), &recentReleasesQuery, variables) + err := gitHubClient.Query(context.Background(), &recentReleasesQuery, variables) if err != nil { panic(err) } diff --git a/sponsors.go b/sponsors.go index 3f7dd57..449526e 100644 --- a/sponsors.go +++ b/sponsors.go @@ -32,7 +32,7 @@ func sponsors(count int) []Sponsor { "username": githubv4.String(username), "count": githubv4.Int(count), } - err := client.Query(context.Background(), &sponsorsQuery, variables) + err := gitHubClient.Query(context.Background(), &sponsorsQuery, variables) if err != nil { panic(err) } diff --git a/users.go b/users.go index 9b6b69a..0899f5f 100644 --- a/users.go +++ b/users.go @@ -26,7 +26,7 @@ var recentFollowersQuery struct { } func getUsername() (string, error) { - err := client.Query(context.Background(), &viewerQuery, nil) + err := gitHubClient.Query(context.Background(), &viewerQuery, nil) if err != nil { return "", err } @@ -42,7 +42,7 @@ func recentFollowers(count int) []User { "username": githubv4.String(username), "count": githubv4.Int(count), } - err := client.Query(context.Background(), &recentFollowersQuery, variables) + err := gitHubClient.Query(context.Background(), &recentFollowersQuery, variables) if err != nil { panic(err) }