diff --git a/cmd/omnivore/main.go b/cmd/omnivore/main.go new file mode 100644 index 0000000..09cf4b5 --- /dev/null +++ b/cmd/omnivore/main.go @@ -0,0 +1,86 @@ +package main + +import ( + "fmt" + "log" + "os" + "strings" + + "github.com/rubiojr/omnivore-go" + "github.com/urfave/cli/v2" +) + +func main() { + app := &cli.App{ + Name: "omnivore", + Usage: "Omnivore API client", + Action: func(*cli.Context) error { + return nil + }, + Flags: []cli.Flag{}, + Commands: []*cli.Command{ + { + Name: "list", + Usage: "List available articles", + Action: func(cCtx *cli.Context) error { + listSaved(cCtx) + return nil + }, + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "long", + }, + }, + }, + }, + } + + if err := app.Run(os.Args); err != nil { + log.Fatal(err) + } +} + +func listSaved(ctx *cli.Context) { + longFormat := ctx.Bool("long") + client := omnivore.NewClient(omnivore.Opts{Token: getAPIToken()}) + // https://docs.omnivore.app/using/search.html + a, err := client.Search(omnivore.SearchOpts{Query: "in:all sort:saved"}) + if err != nil { + log.Fatalf("Failed to search: %v", err) + } + for _, searchItem := range a { + if longFormat { + formatLong(&searchItem) + } else { + formatShort(&searchItem) + } + } + fmt.Println("Total items:", len(a)) +} + +func formatLong(searchItem *omnivore.SearchItem) { + fmt.Println("* " + searchItem.Title) + fmt.Println(" Labels:", labelsToString(searchItem.Labels)) + fmt.Println(" Folder:", searchItem.Folder) + fmt.Println(" URL:", searchItem.Url) + fmt.Println(" Published at:", searchItem.PublishedAt.Format("2006-01-02")) + fmt.Println(" Saved at:", searchItem.SavedAt.Format("2006-01-02")) +} + +func formatShort(searchItem *omnivore.SearchItem) { + fmt.Printf("* [%s] %s %s (%s)\n", searchItem.SavedAt.Format("2006-01-02"), searchItem.Title, labelsToString(searchItem.Labels), searchItem.Url) +} + +func labelsToString(labels []omnivore.Label) string { + l := []string{} + for _, label := range labels { + l = append(l, label.Name) + } + if len(l) == 0 { + return "" + } + return fmt.Sprintf("[%s]", strings.Join(l, ", ")) +} +func getAPIToken() string { + return os.Getenv("OMNIVORE_API_TOKEN") +} diff --git a/go.mod b/go.mod index cd7cbb1..fb83a22 100644 --- a/go.mod +++ b/go.mod @@ -6,10 +6,14 @@ require ( github.com/google/uuid v1.6.0 github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 github.com/stretchr/testify v1.9.0 + github.com/urfave/cli/v2 v2.27.4 ) require ( + github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index e66a6d2..9f07121 100644 --- a/go.sum +++ b/go.sum @@ -1,13 +1,21 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4= +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= 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/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 h1:17JxqqJY66GmZVHkmAsGEkcIu0oCe3AM420QDgGwZx0= github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466/go.mod h1:9dIRpgIY7hVhoqfe0/FcYp0bpInZaT7dc3BYOprrIUE= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/urfave/cli/v2 v2.27.4 h1:o1owoI+02Eb+K107p27wEX9Bb8eqIoZCfLXloLUSWJ8= +github.com/urfave/cli/v2 v2.27.4/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ= +github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4= +github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=