add current books
Jes Olson j3s@c3f.net
Sat, 26 Nov 2022 19:40:49 -0800
3 files changed,
62 insertions(+),
3 deletions(-)
M
main.go
→
main.go
@@ -17,6 +17,7 @@ "filippo.io/age"
"filippo.io/age/armor" "git.j3s.sh/j3s.sh/atom" "git.j3s.sh/j3s.sh/feed" + "git.j3s.sh/j3s.sh/openlibrary" "github.com/SlyMarbo/rss" )@@ -29,7 +30,8 @@ // data for use in templates. it's useful for it to be
// global since it may need to be available in arbitrary // contexts. maybe that sucks. but idfk! type templateData struct { - Feeds []rss.Feed + Feeds []rss.Feed + CurrentBooks *openlibrary.CurrentBooks } var data templateData@@ -37,11 +39,14 @@
// the populate function populates the global "data" variable // with ... data. with which to pass into templates. func (t *templateData) populate() { + // call for books first bc it's fast + t.CurrentBooks = openlibrary.GetCurrentBooks() t.Feeds = feed.GetAllFeeds() - // refresh all rss feeds periodically idk 60 mins seems + // refresh all feeds periodically idk 60 mins seems // like a fine-ass interval for range time.Tick(time.Minute * 60) { - log.Println("updating feeds tbh") + log.Println("updating fetched objects") + t.CurrentBooks = openlibrary.GetCurrentBooks() for i := range t.Feeds { err := t.Feeds[i].Update() if err != nil {@@ -103,6 +108,7 @@ return template.Template{}, err
} funcMap := template.FuncMap{ + "toLower": strings.ToLower, "randomheaderphrase": randomJesPhrase, "randomrotationdegrees": randomRotationDegrees, }
A
openlibrary/openlibrary.go
@@ -0,0 +1,41 @@
+package openlibrary + +import ( + "encoding/json" + "fmt" + "io" + "net/http" +) + +type CurrentBooks struct { + ReadingLogEntries []struct { + Work struct { + Title string `json:"title"` + Key string `json:"key"` + AuthorKeys []string `json:"author_keys"` + AuthorNames []string `json:"author_names"` + FirstPublishYear int `json:"first_publish_year"` + LendingEditionS interface{} `json:"lending_edition_s"` + EditionKey []string `json:"edition_key"` + CoverID int `json:"cover_id"` + CoverEditionKey string `json:"cover_edition_key"` + } `json:"work"` + LoggedEdition string `json:"logged_edition"` + LoggedDate string `json:"logged_date"` + } `json:"reading_log_entries"` +} + +func GetCurrentBooks() *CurrentBooks { + cb := CurrentBooks{} + resp, err := http.Get("https://openlibrary.org/people/j3sj3sj3s/books/currently-reading.json") + if err != nil { + fmt.Println(err) + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + err = json.Unmarshal(body, &cb) + if err != nil { + fmt.Println(err) + } + return &cb +}
M
templates/now.html
→
templates/now.html
@@ -2,6 +2,18 @@ {{define "title"}}now{{end}}
{{define "body"}} <h3>now</h3> <p>this page descibes what i'm up to _now_</p> +<h4>books</h4> +<p>books i'm reading currently: +{{- range $entry := .CurrentBooks.ReadingLogEntries }} +<li> + <a href="https://openlibrary.org{{ $entry.Work.Key }}">{{ $entry.Work.Title | toLower }}</a> +</li> +{{- end }} +</p> +<p> +books i have finished: <a href="https://openlibrary.org/people/j3sj3sj3s/books/already-read">linky</a> +</p> +<h4>journal</h4> <p><b>2022-11-05</b></p> <li>finished version 1 of the van</li> <li>traveled to the west coast</li>