From 8d7decd3f75286e625f1aecf8b52f58319247135 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Tue, 28 May 2024 20:47:12 +0200 Subject: [PATCH] Adds list-categories command --- .gitignore | 1 + README.org | 3 +++ cmd/bookworm/bookworm.go | 53 ++++++++++++++++++++++++++++++++++++++++ go.mod | 2 ++ go.sum | 2 ++ 5 files changed, 61 insertions(+) create mode 100644 go.sum diff --git a/.gitignore b/.gitignore index ae3c172..524cf40 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /bin/ +/bookworm.json diff --git a/README.org b/README.org index 7862a4d..689c367 100644 --- a/README.org +++ b/README.org @@ -17,3 +17,6 @@ an ebook using calibre. script. - If possible, the script should send the ebook to our reader, if possible (say, through the kindle email, for example). +- Allow to download the OPML and use it as a local storage for the + feeds. In the future, maybe users will send their OPMLs and Miniflux + will not be necessary. diff --git a/cmd/bookworm/bookworm.go b/cmd/bookworm/bookworm.go index c4d0d9c..d4f4486 100644 --- a/cmd/bookworm/bookworm.go +++ b/cmd/bookworm/bookworm.go @@ -5,15 +5,27 @@ import ( "flag" "fmt" "os" + + miniflux "miniflux.app/client" ) type Config struct { MinifluxURL string `json:"miniflux_url"` + MinifluxUsername string `json:"miniflux_username"` + MinifluxPassword string `json:"miniflux_password"` MinifluxAPI string `json:"miniflux_api"` MinifluxCategory string `json:"miniflux_category"` } func (c *Config) IsValid() error { + if c.MinifluxURL == "" { + return fmt.Errorf("miniflux_url is required") + } + + if c.MinifluxAPI == "" && (c.MinifluxUsername == "" || c.MinifluxPassword == "") { + return fmt.Errorf("either miniflux_api or miniflux_username and miniflux_password need to be set") + } + return nil } @@ -31,6 +43,21 @@ func ReadConfig(name string) (*Config, error) { return &cfg, nil } +func createClient(cfg *Config) (*miniflux.Client, error) { + var client *miniflux.Client + if cfg.MinifluxAPI != "" { + client = miniflux.New(cfg.MinifluxURL, cfg.MinifluxAPI) + } else { + client = miniflux.New(cfg.MinifluxURL, cfg.MinifluxUsername, cfg.MinifluxPassword) + } + + if _, err := client.Me(); err != nil { + return nil, fmt.Errorf("cannot get \"me\" with newly created client: %w", err) + } + + return client, nil +} + func checkErr(err error) { if err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) @@ -38,8 +65,25 @@ func checkErr(err error) { } } +func listCategories(cfg *Config, client *miniflux.Client) error { + categories, err := client.Categories() + if err != nil { + return fmt.Errorf("cannot fetch categories: %w", err) + } + + if len(categories) != 0 { + fmt.Println("Category list") + } + for _, c := range categories { + fmt.Printf("- %s\n", c.Title) + } + + return nil +} + func main() { cfgFlag := flag.String("config", "bookworm.json", "path to the configuration file") + listCategoriesFlag := flag.Bool("list-categories", false, "lists the categories of the remote Miniflux instance") flag.Parse() cfg, err := ReadConfig(*cfgFlag) @@ -47,4 +91,13 @@ func main() { cErr := cfg.IsValid() checkErr(cErr) + + client, cliErr := createClient(cfg) + checkErr(cliErr) + + if *listCategoriesFlag { + listCategories(cfg, client) + } + + // download feeds (filter by category if specified) } diff --git a/go.mod b/go.mod index bcd3f32..cfaa3db 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module git.ctrlz.es/mgdelacroix/bookworm go 1.21.9 + +require miniflux.app v1.0.46 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..477e448 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +miniflux.app v1.0.46 h1:2/xrbXiEoQlj/bAZ8MT+fPN1+gmYDYuXVCOhvSskns4= +miniflux.app v1.0.46/go.mod h1:YtEJIO1vMCvZgyzDbds7II0W/H7sGpo3auFCQscuMrE=