advancing

Former-commit-id: 0ea003b2683a6174d3d304e4bd32dd01540b87df
master
bel 2019-06-22 13:48:27 -06:00
parent c0c88502c4
commit 84d5906d9b
8 changed files with 62 additions and 79 deletions

View File

@ -6,12 +6,13 @@ import (
"log" "log"
"net/http" "net/http"
"regexp" "regexp"
"sort"
"time" "time"
"github.com/mmcdole/gofeed" "github.com/mmcdole/gofeed"
) )
const NSFeeds = "NSFeeds" const nsFeeds = "nsFeeds"
type Feed struct { type Feed struct {
Key string Key string
@ -36,12 +37,12 @@ func (f *Feed) Decode(b []byte) error {
return config.Decode(b, f) return config.Decode(b, f)
} }
func (f *Feed) load() error { func (f *Feed) Load() error {
if f.Key == "" { if f.Key == "" {
return errors.New("cannot load nil feed") return errors.New("cannot load nil feed")
} }
db := config.Values().DB db := config.Values().DB
b, err := db.Get(f.Key, NSFeeds) b, err := db.Get(f.Key, nsFeeds)
if err != nil { if err != nil {
return err return err
} }
@ -50,7 +51,7 @@ func (f *Feed) load() error {
func (f *Feed) pull() error { func (f *Feed) pull() error {
if f.URL == "" { if f.URL == "" {
if err := f.load(); err != nil { if err := f.Load(); err != nil {
return err return err
} }
} }
@ -81,7 +82,7 @@ func (f *Feed) pull() error {
log.Println("Skipping bad titled item") log.Println("Skipping bad titled item")
continue continue
} }
if err := item.save(); err != nil { if err := item.save(f.Key); err != nil {
log.Println(err) log.Println(err)
continue continue
} }
@ -98,7 +99,24 @@ func (f *Feed) save() error {
return err return err
} }
db := config.Values().DB db := config.Values().DB
return db.Set(f.Key, b, NSFeeds) return db.Set(f.Key, b, nsFeeds)
}
func (f *Feed) List(limit int) ([]string, error) {
keys, err := config.Values().DB.List([]string{nsItems, f.Key})
if err != nil {
return nil, err
}
sorted := sort.StringSlice(keys)
sorted.Sort()
if len(sorted) > limit {
sorted = sorted[len(sorted)-limit:]
}
for i := 0; i < len(sorted)/2; i++ {
j := len(sorted) - 1 - i
sorted.Swap(i, j)
}
return sorted, nil
} }
func latestTSPtr(times ...*time.Time) time.Time { func latestTSPtr(times ...*time.Time) time.Time {

View File

@ -77,7 +77,7 @@ func TestRSSFeedSaveLoad(t *testing.T) {
} }
g := newFeed("key") g := newFeed("key")
if err := g.load(); err != nil { if err := g.Load(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -88,7 +88,7 @@ func TestRSSFeedSaveLoad(t *testing.T) {
} }
h := newFeed("key2") h := newFeed("key2")
if err := h.load(); err == nil { if err := h.Load(); err == nil {
t.Fatal("can load nil feed") t.Fatal("can load nil feed")
} }
} }
@ -116,14 +116,14 @@ func TestRSSFeedPull(t *testing.T) {
t.Errorf("updated is wrong: %v", f) t.Errorf("updated is wrong: %v", f)
} }
keys, err := config.Values().DB.List([]string{nsItems}) keys, err := config.Values().DB.List([]string{nsItems, f.Key})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if len(keys) < 1 { if len(keys) < 1 {
t.Fatal(len(keys)) t.Fatal(len(keys))
} }
if keys[0] != "https://roosterteeth.com/episode/rooster-teeth-podcast-2018-rooster-teeth-podcast-500" { if keys[0] != "2018-07-10-19-00:https://roosterteeth.com/episode/rooster-teeth-podcast-2018-rooster-teeth-podcast-500" {
t.Fatal(keys[0]) t.Fatal(keys[0])
} }
@ -133,7 +133,7 @@ func TestRSSFeedPull(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
log.SetOutput(os.Stderr) log.SetOutput(os.Stderr)
keysB, err := config.Values().DB.List([]string{nsItems}) keysB, err := config.Values().DB.List([]string{nsItems, f.Key})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -141,10 +141,22 @@ func TestRSSFeedPull(t *testing.T) {
t.Fatalf("%v vs %v", keys, keysB) t.Fatalf("%v vs %v", keys, keysB)
} }
i := &Item{ itemKeys, err := f.List(5)
Link: keys[0], if err != nil {
t.Fatal(err)
} }
if err := i.load(); err != nil { if len(itemKeys) < 1 {
t.Fatal(len(itemKeys))
}
if len(itemKeys) > 5 {
t.Fatal(len(itemKeys))
}
if itemKeys[0] != "2018-07-10-19-00:https://roosterteeth.com/episode/rooster-teeth-podcast-2018-rooster-teeth-podcast-500" {
t.Fatal(itemKeys[0])
}
i := &Item{}
if err := i.Load(itemKeys[0], f.Key); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if *i == (Item{}) { if *i == (Item{}) {

View File

@ -1,7 +1,6 @@
package rss package rss
import ( import (
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"local/rssmon3/config" "local/rssmon3/config"
@ -68,21 +67,22 @@ func (i *Item) Decode(b []byte) error {
return config.Decode(b, i) return config.Decode(b, i)
} }
func (i *Item) save() error { func (i *Item) save(ns1 string, ns ...string) error {
db := config.Values().DB db := config.Values().DB
b, err := i.Encode() b, err := i.Encode()
if err != nil { if err != nil {
return err return err
} }
return db.Set(i.Link, b, nsItems) return db.Set(i.ID(), b, append([]string{nsItems, ns1}, ns...)...)
} }
func (i *Item) load() error { func (i *Item) ID() string {
if i.Link == "" { return fmt.Sprintf("%s:%s", i.TS.Format("2006-01-02-15-04"), i.Link)
return errors.New("cannot load nil item") }
}
func (i *Item) Load(key, ns1 string, ns ...string) error {
db := config.Values().DB db := config.Values().DB
b, err := db.Get(i.Link, nsItems) b, err := db.Get(key, append([]string{nsItems, ns1}, ns...)...)
if err != nil { if err != nil {
return err return err
} }

View File

@ -54,29 +54,24 @@ func TestRSSItemNewEncodeDecode(t *testing.T) {
t.Errorf("decode found %v, want %v", itemB.Content, item.Content) t.Errorf("decode found %v, want %v", itemB.Content, item.Content)
} }
if err := item.save(); err != nil { if err := item.save("key"); err != nil {
t.Fatal(err) t.Fatal(err)
} }
itemC := &Item{ itemC := &Item{
Link: item.Link, Link: item.Link,
} }
if err := itemC.load(); err != nil { if err := itemC.Load(item.ID(), "key"); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if fmt.Sprintf("%v", item) != fmt.Sprintf("%v", itemC) { if fmt.Sprintf("%v", item) != fmt.Sprintf("%v", itemC) {
t.Fatalf("%v != %v", item, itemC) t.Fatalf("%v != %v", item, itemC)
} }
itemD := &Item{} if err := itemC.Load(item.ID()+"not", "key"); err == nil {
if err := itemD.load(); err == nil {
t.Fatal(err) t.Fatal(err)
} }
if err := itemC.Load(item.ID(), "key"+"not"); err == nil {
itemE := &Item{
Link: "nothing",
}
if err := itemE.load(); err == nil {
t.Fatal(err) t.Fatal(err)
} }
} }

View File

@ -6,8 +6,6 @@ import (
"log" "log"
) )
const nsFeeds = "nsFeeds"
type RSS struct { type RSS struct {
items chan *monitor.Item items chan *monitor.Item
config.Stoppable config.Stoppable

View File

@ -44,14 +44,14 @@ func TestRSSNewRunUpdate(t *testing.T) {
} }
log.SetOutput(os.Stderr) log.SetOutput(os.Stderr)
keys, err := config.Values().DB.List([]string{nsItems}) keys, err := config.Values().DB.List([]string{nsItems, f.Key})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if len(keys) != 1 { if len(keys) != 1 {
t.Fatal(keys) t.Fatal(keys)
} }
if keys[0] != "https://roosterteeth.com/episode/rooster-teeth-podcast-2018-rooster-teeth-podcast-500" { if keys[0] != "2018-07-10-19-00:https://roosterteeth.com/episode/rooster-teeth-podcast-2018-rooster-teeth-podcast-500" {
t.Fatal(keys[0]) t.Fatal(keys[0])
} }
} }

View File

@ -1 +1 @@
40a59e2956fb03b937a63a870d72f67979c1fce2 0d46529517022e32f10ee1e18ee21760de34fecf

View File

@ -1,17 +1,12 @@
package server package server
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"io"
"local/router" "local/router"
"local/rssmon3/config"
"local/storage"
"log" "log"
"net/http" "net/http"
"regexp" "regexp"
"strings"
) )
func (s *Server) Routes() error { func (s *Server) Routes() error {
@ -47,46 +42,11 @@ func (s *Server) error(w http.ResponseWriter, r *http.Request, err error) {
} }
func (s *Server) tag(w http.ResponseWriter, r *http.Request) { func (s *Server) tag(w http.ResponseWriter, r *http.Request) {
foo := s.notFound if r.Method != "GET" {
switch r.Method { s.notFound(w, r)
case "GET": return
foo = s.getFeedByTag
} }
foo(w, r)
}
func (s *Server) getFeedByTag(w http.ResponseWriter, r *http.Request) {
tag := regexp.MustCompile("^.*\\/").ReplaceAllString(r.URL.Path, "") tag := regexp.MustCompile("^.*\\/").ReplaceAllString(r.URL.Path, "")
log.Println(tag) log.Println(tag)
s.error(w, r, errors.New("not impl")) s.error(w, r, errors.New("not impl"))
} }
func (s *Server) auctions(w http.ResponseWriter, r *http.Request) {
foo := http.NotFound
r.ParseForm()
switch strings.ToLower(r.Method) {
case "get":
foo = s.get
}
foo(w, r)
}
func (s *Server) get(w http.ResponseWriter, r *http.Request) {
db := config.Values().DB
if len(strings.Split(r.URL.Path, "/")) < 3 {
s.notFound(w, r)
return
}
key := strings.Split(r.URL.Path, "/")[2]
log.Println("GET", key)
value, err := db.Get(key)
if err == storage.ErrNotFound {
s.notFound(w, r)
return
}
if err != nil {
s.error(w, r, err)
return
}
io.Copy(w, bytes.NewBuffer(value))
}