diff --git a/cmd/bookworm/bookworm.go b/cmd/bookworm/bookworm.go index d4f4486..cfb9172 100644 --- a/cmd/bookworm/bookworm.go +++ b/cmd/bookworm/bookworm.go @@ -6,10 +6,13 @@ import ( "fmt" "os" + "git.ctrlz.es/mgdelacroix/bookworm/templates" + miniflux "miniflux.app/client" ) type Config struct { + Title string `json:"title"` MinifluxURL string `json:"miniflux_url"` MinifluxUsername string `json:"miniflux_username"` MinifluxPassword string `json:"miniflux_password"` @@ -18,6 +21,10 @@ type Config struct { } func (c *Config) IsValid() error { + if c.Title == "" { + return fmt.Errorf("title is required") + } + if c.MinifluxURL == "" { return fmt.Errorf("miniflux_url is required") } @@ -97,7 +104,49 @@ func main() { if *listCategoriesFlag { listCategories(cfg, client) + return } - // download feeds (filter by category if specified) + var feeds miniflux.Feeds + var fErr error + if cfg.MinifluxCategory != "" { + categories, catErr := client.Categories() + if catErr != nil { + checkErr(fmt.Errorf("can't get categories: %w", catErr)) + } + + var category *miniflux.Category + for _, c := range categories { + if c.Title == cfg.MinifluxCategory { + category = c + break + } + } + + if category == nil { + checkErr(fmt.Errorf("can't find category %q", cfg.MinifluxCategory)) + } + + feeds, fErr = client.CategoryFeeds(category.ID) + } else { + feeds, fErr = client.Feeds() + } + checkErr(fErr) + + data := struct { + Title string + OldestArticle int + MaxArticlesPerFeed int + Feeds miniflux.Feeds + }{ + Title: cfg.Title, + OldestArticle: 7, + MaxArticlesPerFeed: 100, + Feeds: feeds, + } + + s, tErr := templates.Execute(data) + checkErr(tErr) + + checkErr(os.WriteFile("out.recipe", s, 0755)) } diff --git a/templates/recipe.tmpl b/templates/recipe.tmpl new file mode 100644 index 0000000..6e11d76 --- /dev/null +++ b/templates/recipe.tmpl @@ -0,0 +1,17 @@ +{{- define "recipe" }} +#!/usr/bin/env python +from calibre.web.feeds.news import BasicNewsRecipe + +class AdvancedUserRecipe1709396452(BasicNewsRecipe): + title = '{{ .Title }}' + oldest_article = {{ .OldestArticle }} + max_articles_per_feed = {{ .MaxArticlesPerFeed }} + auto_cleanup = True + remove_empty_feeds = True + + feeds = [ +{{- range $index, $feed := .Feeds }} + ("{{ $feed.Title }}", "{{ $feed.FeedURL }}"), +{{- end }} + ] +{{- end }} \ No newline at end of file diff --git a/templates/templates.go b/templates/templates.go new file mode 100644 index 0000000..e7c5ae5 --- /dev/null +++ b/templates/templates.go @@ -0,0 +1,34 @@ +package templates + +import ( + "bytes" + "embed" + "fmt" + "text/template" +) + +//go:embed *.tmpl +var templates embed.FS + +func GetTemplates() (*template.Template, error) { + tmpl, err := template.New("").ParseFS(templates, "*.tmpl") + if err != nil { + return nil, fmt.Errorf("cannot parse templates: %w", err) + } + + return tmpl, nil +} + +func Execute(data any) ([]byte, error) { + buffer := bytes.NewBufferString("") + tmpl, err := GetTemplates() + if err != nil { + return nil, fmt.Errorf("cannot get templates: %w", err) + } + + if err := tmpl.ExecuteTemplate(buffer, "recipe", data); err != nil { + return nil, fmt.Errorf("cannot execute template: %w", err) + } + + return buffer.Bytes(), nil +}