Skip to content

Commit

Permalink
Implement basic GoodReads templates (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis030195 authored Oct 31, 2020
1 parent 1195417 commit 74544ef
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 14 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion gists.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand All @@ -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=
Expand Down
21 changes: 21 additions & 0 deletions goodreads.go
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 16 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"flag"
"fmt"
"github.com/KyleBanks/goodreads"
"html/template"
"io/ioutil"
"net/http"
Expand All @@ -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")
)
Expand Down Expand Up @@ -44,6 +47,9 @@ func main() {
"sponsors": sponsors,
/* RSS */
"rss": rssFeed,
/* GoodReads */
"goodReadsReviews": goodReadsReviews,
"goodReadsCurrentlyReading": goodReadsCurrentlyReading,
/* Utils */
"humanize": humanized,
}).Parse(string(tplIn))
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion sponsors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 74544ef

Please sign in to comment.