-
Notifications
You must be signed in to change notification settings - Fork 0
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
e26da10
commit 86f0492
Showing
7 changed files
with
913 additions
and
3 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
FROM alpine:3.14 | ||
# hadolint ignore=DL3018 | ||
RUN apk add --no-cache git | ||
COPY labdoc / | ||
ENTRYPOINT ["/labdoc"] |
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,4 @@ | ||
## Recently Read Books | ||
{{ range goodreadsRecentlyRead 10 }} | ||
- [{{ .Title }}]({{ .Link }}) by {{ .Author }} {{ .PubDate | builtinAgo }} | ||
{{- end }} |
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,96 @@ | ||
package goodreads | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"html/template" | ||
"os" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/drewstinnett/labdoc/pkg/labdoc" | ||
"github.com/mmcdole/gofeed" | ||
) | ||
|
||
type plug struct{} | ||
|
||
func (p *plug) TemplateFunctions() (template.FuncMap, error) { | ||
templ := template.FuncMap{ | ||
"recentlyRead": recentlyRead, | ||
} | ||
return templ, nil | ||
} | ||
|
||
func (p *plug) Examples() string { | ||
return `## Recently Read Books | ||
{{ range goodreadsRecentlyRead 10 }} | ||
- {{ .Title }} by {{ .Author }} {{ .PubDate | builtinAgo }} | ||
{{- end }` | ||
} | ||
|
||
func recentlyRead(limit int) ([]ReadEntry, error) { | ||
user := os.Getenv("GOODREADS_RSSUSERID") | ||
if user == "" { | ||
return nil, errors.New("Must Set GOODREADS_RSSUSERID") | ||
} | ||
key := os.Getenv("GOODREADS_RSSKEY") | ||
if key == "" { | ||
return nil, errors.New("Must Set GOODREADS_RSSKEY") | ||
} | ||
fp := gofeed.NewParser() | ||
url := fmt.Sprintf("https://www.goodreads.com/review/list_rss/%v?key=%v&shelf=read", user, key) | ||
feed, err := fp.ParseURL(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
items := parseRSSRead(feed, limit) | ||
|
||
return items, nil | ||
} | ||
|
||
type ReadEntry struct { | ||
Title string | ||
Link string | ||
PubDate *time.Time | ||
Description string | ||
Author string | ||
UserRating *float64 | ||
// DateAdded *time.Time | ||
} | ||
|
||
func parseRSSRead(feed *gofeed.Feed, limit int) []ReadEntry { | ||
entries := make([]ReadEntry, 0, limit) | ||
|
||
for _, fi := range feed.Items { | ||
// Get the user rating if we can | ||
userRatingGot, err := strconv.ParseFloat(fi.Custom["user_rating"], 64) | ||
var userRating *float64 | ||
if err != nil { | ||
userRating = nil | ||
} else { | ||
userRating = &userRatingGot | ||
} | ||
e := ReadEntry{ | ||
Title: fi.Title, | ||
Link: fi.Link, | ||
PubDate: fi.PublishedParsed, | ||
Description: fi.Description, | ||
Author: fi.Custom["author_name"], | ||
UserRating: userRating, | ||
// DateAdded: fi.Custom["date_added"], | ||
} | ||
entries = append(entries, e) | ||
if len(entries) == limit { | ||
break | ||
} | ||
} | ||
|
||
return entries | ||
} | ||
|
||
func init() { | ||
labdoc.Add("goodreads", func() labdoc.Plugin { | ||
return &plug{} | ||
}) | ||
} |
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,27 @@ | ||
package goodreads | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/apex/log" | ||
"github.com/mmcdole/gofeed" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseRecentlyRead(t *testing.T) { | ||
file, err := os.Open("./testdata/read.xml") | ||
require.NoError(t, err) | ||
defer file.Close() | ||
fp := gofeed.NewParser() | ||
feed, _ := fp.Parse(file) | ||
entries := parseRSSRead(feed, 10) | ||
require.Greater(t, len(entries), 0) | ||
first := entries[0] | ||
log.Warnf("%+v", first) | ||
|
||
require.Equal(t, "Blankets", first.Title) | ||
require.Equal(t, "Craig Thompson", first.Author) | ||
require.Equal(t, float64(5), *first.UserRating) | ||
require.Equal(t, "https://www.goodreads.com/review/show/3262380767?utm_medium=api&utm_source=rss", first.Link) | ||
} |
Oops, something went wrong.