generated from alecodes/base-template
feat: add simple reconsiliation capabilities
This commit is contained in:
parent
5d00b7c336
commit
28fa3ed3cc
5 changed files with 451 additions and 8 deletions
162
examples/readeck/main.go
Normal file
162
examples/readeck/main.go
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
|
||||
synchronizator "git.alecodes.page/alecodes/synchronizator/pkg"
|
||||
)
|
||||
|
||||
type ReadwiseDocument struct {
|
||||
Id string `json:"id"`
|
||||
Url string `json:"url"`
|
||||
Title string `json:"title"`
|
||||
Location string `json:"location"`
|
||||
SourceUrl string `json:"source_url"`
|
||||
Tags map[string]ReadwiseTag `json:"tags"`
|
||||
}
|
||||
|
||||
type ReadwiseTag struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Created int `json:"created"`
|
||||
}
|
||||
|
||||
type ReadwiseHighlight struct {
|
||||
UserBookID int `json:"user_book_id"`
|
||||
Title string `json:"title"`
|
||||
Author string `json:"author"`
|
||||
Source string `json:"source"`
|
||||
UniqueURL string `json:"unique_url"`
|
||||
BookTags []HighlightTag `json:"book_tags"`
|
||||
Category string `json:"category"`
|
||||
DocumentNote *string `json:"document_note"`
|
||||
ReadwiseURL string `json:"readwise_url"`
|
||||
SourceURL string `json:"source_url"`
|
||||
Highlights []HighlightItem `json:"highlights"`
|
||||
}
|
||||
|
||||
type HighlightTag struct {
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type HighlightItem struct {
|
||||
ID int `json:"id"`
|
||||
Text string `json:"text"`
|
||||
Location int `json:"location"`
|
||||
LocationType string `json:"location_type"`
|
||||
Note string `json:"note"`
|
||||
Color string `json:"color"`
|
||||
HighlightedAt string `json:"highlighted_at"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
ExternalID string `json:"external_id"`
|
||||
EndLocation *int `json:"end_location"`
|
||||
URL string `json:"url"`
|
||||
BookID int `json:"book_id"`
|
||||
Tags []HighlightTag `json:"tags"`
|
||||
IsFavorite bool `json:"is_favorite"`
|
||||
IsDiscard bool `json:"is_discard"`
|
||||
ReadwiseURL string `json:"readwise_url"`
|
||||
}
|
||||
|
||||
type ReadeckBookmark struct {
|
||||
Id string `json:"id"`
|
||||
Url string `json:"url"`
|
||||
Title string `json:"title"`
|
||||
Labels []string `json:"labels"`
|
||||
IsArchived bool `json:"is_archived"`
|
||||
}
|
||||
|
||||
func readwiseToReadeck(document *ReadwiseDocument) (*synchronizator.Node, error) {
|
||||
bookmark := &ReadeckBookmark{
|
||||
Url: document.SourceUrl,
|
||||
Title: document.Title,
|
||||
}
|
||||
|
||||
for _, tag := range document.Tags {
|
||||
bookmark.Labels = append(bookmark.Labels, tag.Name)
|
||||
}
|
||||
|
||||
metadata, err := json.Marshal(bookmark)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
node := synchronizator.NewNode(bookmark.Title, "BOOKMARK", metadata, nil)
|
||||
|
||||
return node, nil
|
||||
}
|
||||
|
||||
func drop_data(conn *sql.DB) error {
|
||||
sql := `
|
||||
DELETE FROM nodes WHERE name = 'READECK';
|
||||
`
|
||||
_, err := conn.Exec(sql)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func main() {
|
||||
start := time.Now()
|
||||
|
||||
defer func() {
|
||||
elapsed := time.Now().Sub(start)
|
||||
fmt.Printf("\n\nExecution time took: %s", elapsed)
|
||||
}()
|
||||
|
||||
connection, err := sql.Open("sqlite", "readwise.sql")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
defer connection.Close()
|
||||
|
||||
opts := synchronizator.DefaultOptions
|
||||
// opts.Log_level = synchronizator.DEBUG
|
||||
|
||||
err = drop_data(connection)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
sync, err := synchronizator.New(connection, opts)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
readeck, err := sync.NewPlatform("READECK", nil, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
collection, err := readeck.GetDefaultCollection()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
readwiseDocuments, err := sync.GetCollection(3)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
err = synchronizator.ReconciliateCollections(readwiseDocuments, collection, readwiseToReadeck)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -24,7 +24,7 @@ type ReadwiseCursor struct {
|
|||
|
||||
type ReadwiseApiResponse[T, S any] struct {
|
||||
Results []T `json:"results"`
|
||||
Detail string `json:detail`
|
||||
Detail string `json:"detail"`
|
||||
Count uint64 `json:"count"`
|
||||
NextPageCursor S `json:"nextPageCursor"`
|
||||
}
|
||||
|
|
@ -40,8 +40,8 @@ type ReadwiseDocument struct {
|
|||
// Author string `json:"author"`
|
||||
// Source string `json:"source"`
|
||||
// Category string `json:"category"`
|
||||
Location string `json:"location"`
|
||||
// Tags map[string]string `json:"tags"`
|
||||
Location string `json:"location"`
|
||||
Tags map[string]ReadwiseTag `json:"tags"`
|
||||
// SiteName string `json:"site_name"`
|
||||
// CreatedAt string `json:"created_at"`
|
||||
// UpdatedAt string `json:"updated_at"`
|
||||
|
|
@ -53,6 +53,12 @@ type ReadwiseDocument struct {
|
|||
// LastMovedAt string `json:"last_moved_at"`
|
||||
}
|
||||
|
||||
type ReadwiseTag struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Created int `json:"created"`
|
||||
}
|
||||
|
||||
type ReadwiseHighlight struct {
|
||||
UserBookID int `json:"user_book_id"`
|
||||
Title string `json:"title"`
|
||||
|
|
@ -68,8 +74,8 @@ type ReadwiseHighlight struct {
|
|||
}
|
||||
|
||||
type HighlightTag struct {
|
||||
Id int `json:id`
|
||||
Name string `json:name`
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type HighlightItem struct {
|
||||
|
|
|
|||
Reference in a new issue