aboutsummaryrefslogtreecommitdiff
path: root/internal/feed
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--internal/feed/cache.go4
-rw-r--r--internal/feed/cache_v1.go51
-rw-r--r--internal/feed/state.go2
3 files changed, 54 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 {