diff options
-rw-r--r-- | internal/feed/cache.go | 4 | ||||
-rw-r--r-- | internal/feed/cache_v1.go | 51 | ||||
-rw-r--r-- | internal/feed/state.go | 2 | ||||
-rw-r--r-- | tools/print-cache/print-cache.go | 36 |
4 files changed, 90 insertions, 3 deletions
diff --git a/internal/feed/cache.go b/internal/feed/cache.go index 5674de4..45cefd0 100644 --- a/internal/feed/cache.go +++ b/internal/feed/cache.go @@ -20,6 +20,8 @@ const ( type Cache interface { findItem(*Feed) CachedFeed Version() Version + Info() string + SpecificInfo(interface{}) string transformToCurrent() (Cache, error) } @@ -75,7 +77,7 @@ func newCache() (Cache, error) { return cacheForVersion(currentVersion) } -func loadCache(fileName string) (Cache, error) { +func LoadCache(fileName string) (Cache, error) { f, err := os.Open(fileName) if err != nil { if errors.Is(err, os.ErrNotExist) { diff --git a/internal/feed/cache_v1.go b/internal/feed/cache_v1.go index 02890e1..b5d6b3e 100644 --- a/internal/feed/cache_v1.go +++ b/internal/feed/cache_v1.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "fmt" "strconv" + "strings" "time" "github.com/google/uuid" @@ -25,6 +26,11 @@ func (id feedId) String() string { return strconv.FormatUint(uint64(id), 16) } +func idFromString(s string) feedId { + id, _ := strconv.ParseUint(s, 16, 64) + return feedId(id) +} + type v1Cache struct { Ids map[feedDescriptor]feedId NextId uint64 @@ -58,12 +64,15 @@ type cachedItem struct { func (item cachedItem) String() string { return fmt.Sprintf(`{ + ID: %s Title: %q Guid: %q Link: %q Date: %s Hash: %s -}`, item.Title, item.Guid, item.Link, util.TimeFormat(item.Date), item.Hash) +}`, + base64.RawURLEncoding.EncodeToString(item.ID[:]), + item.Title, item.Guid, item.Link, util.TimeFormat(item.Date), item.Hash) } func (cf *cachedFeed) Checked(withFailure bool) { @@ -97,6 +106,46 @@ func (cache *v1Cache) Version() Version { return v1Version } +func (cache *v1Cache) Info() string { + b := strings.Builder{} + for descr, id := range cache.Ids { + b.WriteString(fmt.Sprintf("%3s: %s (%s)\n", id.String(), descr.Name, descr.Url)) + } + return b.String() +} + +func (cache *v1Cache) SpecificInfo(i interface{}) string { + id := idFromString(i.(string)) + + b := strings.Builder{} + feed := cache.Feeds[id] + + for descr, fId := range cache.Ids { + if id == fId { + b.WriteString(descr.Name) + b.WriteString(" -- ") + b.WriteString(descr.Url) + b.WriteByte('\n') + break + } + } + + b.WriteString(fmt.Sprintf(` +Last Check: %s +Num Failures: %d +Num Items: %d +`, + util.TimeFormat(feed.LastCheck), + feed.NumFailures, + len(feed.Items))) + + for _, item := range feed.Items { + b.WriteString("\n--------------------\n") + b.WriteString(item.String()) + } + return b.String() +} + func newV1Cache() *v1Cache { cache := v1Cache{ Ids: map[feedDescriptor]feedId{}, diff --git a/internal/feed/state.go b/internal/feed/state.go index 5831ff4..dae0917 100644 --- a/internal/feed/state.go +++ b/internal/feed/state.go @@ -48,7 +48,7 @@ func (state *State) LoadCache(fileName string, forceNew bool) error { if forceNew { cache, err = newCache() } else { - cache, err = loadCache(fileName) + cache, err = LoadCache(fileName) } if err != nil { diff --git a/tools/print-cache/print-cache.go b/tools/print-cache/print-cache.go new file mode 100644 index 0000000..d20963a --- /dev/null +++ b/tools/print-cache/print-cache.go @@ -0,0 +1,36 @@ +package main + +import ( + "flag" + "fmt" + "log" + + "github.com/Necoro/feed2imap-go/internal/feed" +) + +// flags +var ( + cacheFile string = "feed.cache" + feedId string = "" +) + +func init() { + flag.StringVar(&cacheFile, "c", cacheFile, "cache file") + flag.StringVar(&feedId, "i", feedId, "id of the feed") +} + +func main() { + flag.Parse() + + cache, err := feed.LoadCache(cacheFile) + if err != nil { + log.Fatal(err) + } + + fmt.Printf("Cache version %d\n", cache.Version()) + if feedId != "" { + fmt.Print(cache.SpecificInfo(feedId)) + } else { + fmt.Print(cache.Info()) + } +} |