parent
c0c88502c4
commit
84d5906d9b
30
rss/feed.go
30
rss/feed.go
|
|
@ -6,12 +6,13 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/mmcdole/gofeed"
|
||||
)
|
||||
|
||||
const NSFeeds = "NSFeeds"
|
||||
const nsFeeds = "nsFeeds"
|
||||
|
||||
type Feed struct {
|
||||
Key string
|
||||
|
|
@ -36,12 +37,12 @@ func (f *Feed) Decode(b []byte) error {
|
|||
return config.Decode(b, f)
|
||||
}
|
||||
|
||||
func (f *Feed) load() error {
|
||||
func (f *Feed) Load() error {
|
||||
if f.Key == "" {
|
||||
return errors.New("cannot load nil feed")
|
||||
}
|
||||
db := config.Values().DB
|
||||
b, err := db.Get(f.Key, NSFeeds)
|
||||
b, err := db.Get(f.Key, nsFeeds)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -50,7 +51,7 @@ func (f *Feed) load() error {
|
|||
|
||||
func (f *Feed) pull() error {
|
||||
if f.URL == "" {
|
||||
if err := f.load(); err != nil {
|
||||
if err := f.Load(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
@ -81,7 +82,7 @@ func (f *Feed) pull() error {
|
|||
log.Println("Skipping bad titled item")
|
||||
continue
|
||||
}
|
||||
if err := item.save(); err != nil {
|
||||
if err := item.save(f.Key); err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
|
@ -98,7 +99,24 @@ func (f *Feed) save() error {
|
|||
return err
|
||||
}
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ func TestRSSFeedSaveLoad(t *testing.T) {
|
|||
}
|
||||
|
||||
g := newFeed("key")
|
||||
if err := g.load(); err != nil {
|
||||
if err := g.Load(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
|
@ -88,7 +88,7 @@ func TestRSSFeedSaveLoad(t *testing.T) {
|
|||
}
|
||||
|
||||
h := newFeed("key2")
|
||||
if err := h.load(); err == nil {
|
||||
if err := h.Load(); err == nil {
|
||||
t.Fatal("can load nil feed")
|
||||
}
|
||||
}
|
||||
|
|
@ -116,14 +116,14 @@ func TestRSSFeedPull(t *testing.T) {
|
|||
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 {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(keys) < 1 {
|
||||
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])
|
||||
}
|
||||
|
||||
|
|
@ -133,7 +133,7 @@ func TestRSSFeedPull(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
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 {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
@ -141,10 +141,22 @@ func TestRSSFeedPull(t *testing.T) {
|
|||
t.Fatalf("%v vs %v", keys, keysB)
|
||||
}
|
||||
|
||||
i := &Item{
|
||||
Link: keys[0],
|
||||
itemKeys, err := f.List(5)
|
||||
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)
|
||||
}
|
||||
if *i == (Item{}) {
|
||||
|
|
|
|||
16
rss/item.go
16
rss/item.go
|
|
@ -1,7 +1,6 @@
|
|||
package rss
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"local/rssmon3/config"
|
||||
|
|
@ -68,21 +67,22 @@ func (i *Item) Decode(b []byte) error {
|
|||
return config.Decode(b, i)
|
||||
}
|
||||
|
||||
func (i *Item) save() error {
|
||||
func (i *Item) save(ns1 string, ns ...string) error {
|
||||
db := config.Values().DB
|
||||
b, err := i.Encode()
|
||||
if err != nil {
|
||||
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 {
|
||||
if i.Link == "" {
|
||||
return errors.New("cannot load nil item")
|
||||
}
|
||||
func (i *Item) ID() string {
|
||||
return fmt.Sprintf("%s:%s", i.TS.Format("2006-01-02-15-04"), i.Link)
|
||||
}
|
||||
|
||||
func (i *Item) Load(key, ns1 string, ns ...string) error {
|
||||
db := config.Values().DB
|
||||
b, err := db.Get(i.Link, nsItems)
|
||||
b, err := db.Get(key, append([]string{nsItems, ns1}, ns...)...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -54,29 +54,24 @@ func TestRSSItemNewEncodeDecode(t *testing.T) {
|
|||
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)
|
||||
}
|
||||
|
||||
itemC := &Item{
|
||||
Link: item.Link,
|
||||
}
|
||||
if err := itemC.load(); err != nil {
|
||||
if err := itemC.Load(item.ID(), "key"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fmt.Sprintf("%v", item) != fmt.Sprintf("%v", itemC) {
|
||||
t.Fatalf("%v != %v", item, itemC)
|
||||
}
|
||||
|
||||
itemD := &Item{}
|
||||
if err := itemD.load(); err == nil {
|
||||
if err := itemC.Load(item.ID()+"not", "key"); err == nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
itemE := &Item{
|
||||
Link: "nothing",
|
||||
}
|
||||
if err := itemE.load(); err == nil {
|
||||
if err := itemC.Load(item.ID(), "key"+"not"); err == nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ import (
|
|||
"log"
|
||||
)
|
||||
|
||||
const nsFeeds = "nsFeeds"
|
||||
|
||||
type RSS struct {
|
||||
items chan *monitor.Item
|
||||
config.Stoppable
|
||||
|
|
|
|||
|
|
@ -44,14 +44,14 @@ func TestRSSNewRunUpdate(t *testing.T) {
|
|||
}
|
||||
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 {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(keys) != 1 {
|
||||
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])
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
40a59e2956fb03b937a63a870d72f67979c1fce2
|
||||
0d46529517022e32f10ee1e18ee21760de34fecf
|
||||
|
|
@ -1,17 +1,12 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"local/router"
|
||||
"local/rssmon3/config"
|
||||
"local/storage"
|
||||
"log"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
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) {
|
||||
foo := s.notFound
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
foo = s.getFeedByTag
|
||||
if r.Method != "GET" {
|
||||
s.notFound(w, r)
|
||||
return
|
||||
}
|
||||
foo(w, r)
|
||||
}
|
||||
|
||||
func (s *Server) getFeedByTag(w http.ResponseWriter, r *http.Request) {
|
||||
tag := regexp.MustCompile("^.*\\/").ReplaceAllString(r.URL.Path, "")
|
||||
log.Println(tag)
|
||||
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))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue