home

My NixOS systems configurations.
Log | Files | Refs | LICENSE

commit 815df0ead3913b61e27b604c0cbf05ae288e7831
parent 1c6eb8a5c7478989c36400450971a72a02a4d0e0
Author: Vincent Demeester <vincent@sbr.pm>
Date:   Mon, 17 Jun 2024 17:31:15 +0200

go-org-readwise: finishing touches

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Diffstat:
Mtools/go-org-readwise/internal/org/sync.go | 52+++++++++++++++++++++++++++++++---------------------
Mtools/go-org-readwise/internal/org/write.go | 4++--
Mtools/go-org-readwise/internal/readwise/readwise.go | 1-
Mtools/go-org-readwise/main.go | 7-------
4 files changed, 33 insertions(+), 31 deletions(-)

diff --git a/tools/go-org-readwise/internal/org/sync.go b/tools/go-org-readwise/internal/org/sync.go @@ -27,17 +27,6 @@ var ( replaceHypensRegexp = regexp.MustCompile("[-]+") ) -/* -For each results: -- Define a filename (denote naming — gonna be weird but meh) — from title + first highlight date -- Detect if the file exists -- If the file doesn't exist, create the file -- If the file exist, append - -For the file format: org file with denote naming -And use the update date to add new highlights -*/ - func Sync(ctx context.Context, target string, results []readwise.Result) error { for _, result := range results { // FIXME: handle the case where tags where added after @@ -46,7 +35,6 @@ func Sync(ctx context.Context, target string, results []readwise.Result) error { // use a regexp to "detect" part of the thing. denotefilename := denoteFilename(result) filename := filepath.Join(target, denotefilename) - fmt.Println("file", filename) if _, err := os.Stat(filename); err == nil { // Append to the file p := createPartialOrgDocument(result) @@ -54,7 +42,12 @@ func Sync(ctx context.Context, target string, results []readwise.Result) error { if err != nil { return err } - if err := os.WriteFile(filename, content, 0o644); err != nil { + f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o600) + if err != nil { + return err + } + defer f.Close() + if _, err = f.WriteString(string(content)); err != nil { return err } } else if errors.Is(err, os.ErrNotExist) { @@ -79,7 +72,7 @@ func Sync(ctx context.Context, target string, results []readwise.Result) error { func createNewOrgDocument(r readwise.Result) Document { var filetags []string if len(r.BookTags) > 0 { - filetags = make([]string, len(r.BookTags)+1) + filetags = make([]string, len(r.BookTags)) for i, t := range r.BookTags { filetags[i] = sluggify(t.Name) } @@ -100,15 +93,32 @@ func createNewOrgDocument(r readwise.Result) Document { } func createPartialOrgDocument(r readwise.Result) PartialDocument { + now := time.Now() return PartialDocument{ - Date: time.Now().Format(orgDateFormat), - Highlights: transformHighlights(r.Highlights), + Date: now.Format(orgDateFormat), + Highlights: transformHighlights(r.Highlights, func(h readwise.Highlight) bool { + if h.HighlightedAt.After(now) { + return true + } + return false + }), } } -func transformHighlights(highlights []readwise.Highlight) []Highlight { - orgHighlights := make([]Highlight, len(highlights)) - for i, h := range highlights { +func transformHighlights(highlights []readwise.Highlight, filters ...func(readwise.Highlight) bool) []Highlight { + orgHighlights := []Highlight{} + for _, h := range highlights { + skip := false + for _, filter := range filters { + // If a filter returns false, skip the item + if !filter(h) { + skip = true + break + } + } + if skip { + continue + } var tags []string if len(h.Tags) > 0 { tags = make([]string, len(h.Tags)) @@ -116,13 +126,13 @@ func transformHighlights(highlights []readwise.Highlight) []Highlight { tags[i] = sluggify(t.Name) } } - orgHighlights[i] = Highlight{ + orgHighlights = append(orgHighlights, Highlight{ ID: fmt.Sprintf("%d", h.ID), URL: h.ReadwiseURL, Date: h.HighlightedAt.Format(orgDateFormat), Note: h.Note, Text: h.Text, - } + }) } return orgHighlights } diff --git a/tools/go-org-readwise/internal/org/write.go b/tools/go-org-readwise/internal/org/write.go @@ -8,14 +8,14 @@ import ( ) var ( - additionnalTemplate = `* New Highlights on {{ .Date }} + additionnalTemplate = `{{ if .Highlights }}* New Highlights on [{{ .Date }}] {{ range $h := .Highlights -}} ** [{{ $h.Date }}] Highlight [[{{ $h.URL }}][{{ $h.ID }}]]{{ if $h.Tags }} {{ orgtags $h.Tags }}{{ end }} {{ $h.Text }} {{ if $h.Note }}*** Note {{ $h.Note }} {{ end -}} -{{ end }}` +{{ end }}{{ end }}` mainTemplate = `#+title: {{ .Title }} #+author: {{ .Author }} diff --git a/tools/go-org-readwise/internal/readwise/readwise.go b/tools/go-org-readwise/internal/readwise/readwise.go @@ -49,7 +49,6 @@ func fetchExport(ctx context.Context, client *http.Client, apikey string, update params = append(params, fmt.Sprintf("pageCursor=%d", *nextPageCursor)) } endpoint = endpoint + strings.Join(params, "&&") - fmt.Println(endpoint) req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil) if err != nil { return export, err diff --git a/tools/go-org-readwise/main.go b/tools/go-org-readwise/main.go @@ -3,7 +3,6 @@ package main import ( "context" "flag" - "fmt" "log" "os" "path/filepath" @@ -36,17 +35,11 @@ func main() { if err != nil { log.Fatalf("Error reading readwise state file from %s: %v", stateFile, err) } - fmt.Println(*targetFolder) - fmt.Println("updateAfter", updateAfter) ctx := context.Background() results, err := readwise.FetchFromAPI(ctx, apikey, updateAfter) if err != nil { log.Fatalf("Error while fetching results: %v", err) } - // if err := os.WriteFile(stateFile, []byte(time.Now().Format(readwise.FormatUpdatedAfter)), 0o666); err != nil { - // log.Fatalf("Error writing readwise state file in %s: %v", stateFile, err) - // } - fmt.Println("size", len(results)) if err := org.Sync(ctx, *targetFolder, results); err != nil { log.Fatalf("Error syncing readwise and org file in %s folder: %v", *targetFolder, err)