Adds recipe

This commit is contained in:
Miguel de la Cruz 2024-05-29 11:24:13 +02:00
parent 8d7decd3f7
commit c898be82de
3 changed files with 101 additions and 1 deletions

View file

@ -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))
}

17
templates/recipe.tmpl Normal file
View file

@ -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 }}

34
templates/templates.go Normal file
View file

@ -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
}