mirror of
https://github.com/nkanaev/yarr.git
synced 2025-05-25 05:29:20 +00:00
basic feed fetcher
This commit is contained in:
parent
be1804c7d0
commit
9cea82005b
@ -174,6 +174,40 @@ func FeedListHandler(rw http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func convertItems(items []*gofeed.Item, feed storage.Feed) []storage.Item {
|
||||||
|
result := make([]storage.Item, len(items))
|
||||||
|
for _, item := range items {
|
||||||
|
imageURL := ""
|
||||||
|
if item.Image != nil {
|
||||||
|
imageURL = item.Image.URL
|
||||||
|
}
|
||||||
|
author := ""
|
||||||
|
if item.Author != nil {
|
||||||
|
author = item.Author.Name
|
||||||
|
}
|
||||||
|
result = append(result, storage.Item{
|
||||||
|
Id: item.GUID,
|
||||||
|
FeedId: feed.Id,
|
||||||
|
Title: item.Title,
|
||||||
|
Link: item.Link,
|
||||||
|
Description: item.Description,
|
||||||
|
Content: item.Content,
|
||||||
|
Author: author,
|
||||||
|
Date: item.PublishedParsed,
|
||||||
|
DateUpdated: item.UpdatedParsed,
|
||||||
|
Status: storage.UNREAD,
|
||||||
|
Image: imageURL,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func listItems(f storage.Feed) []storage.Item {
|
||||||
|
fp := gofeed.NewParser()
|
||||||
|
feed, _ := fp.ParseURL(f.FeedLink)
|
||||||
|
return convertItems(feed.Items, f)
|
||||||
|
}
|
||||||
|
|
||||||
func createFeed(s *storage.Storage, url string, folderId *int64) error {
|
func createFeed(s *storage.Storage, url string, folderId *int64) error {
|
||||||
fp := gofeed.NewParser()
|
fp := gofeed.NewParser()
|
||||||
feed, err := fp.ParseURL(url)
|
feed, err := fp.ParseURL(url)
|
||||||
@ -184,7 +218,7 @@ func createFeed(s *storage.Storage, url string, folderId *int64) error {
|
|||||||
if len(feedLink) == 0 {
|
if len(feedLink) == 0 {
|
||||||
feedLink = url
|
feedLink = url
|
||||||
}
|
}
|
||||||
entry := s.CreateFeed(
|
s.CreateFeed(
|
||||||
feed.Title,
|
feed.Title,
|
||||||
feed.Description,
|
feed.Description,
|
||||||
feed.Link,
|
feed.Link,
|
||||||
@ -192,8 +226,6 @@ func createFeed(s *storage.Storage, url string, folderId *int64) error {
|
|||||||
"",
|
"",
|
||||||
folderId,
|
folderId,
|
||||||
)
|
)
|
||||||
|
|
||||||
fmt.Println("here we go", entry)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"github.com/nkanaev/yarr/storage"
|
"github.com/nkanaev/yarr/storage"
|
||||||
"log"
|
"log"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Route struct {
|
type Route struct {
|
||||||
@ -18,6 +19,41 @@ type Route struct {
|
|||||||
type Handler struct {
|
type Handler struct {
|
||||||
db *storage.Storage
|
db *storage.Storage
|
||||||
fetchRunning bool
|
fetchRunning bool
|
||||||
|
feedQueue chan storage.Feed
|
||||||
|
counter chan int
|
||||||
|
queueSize int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) startJobs() {
|
||||||
|
go func() {
|
||||||
|
fmt.Println("one")
|
||||||
|
for {
|
||||||
|
feed := <-h.feedQueue
|
||||||
|
fmt.Println("got feed", feed)
|
||||||
|
items := listItems(feed)
|
||||||
|
h.db.CreateItems(items)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
go func() {
|
||||||
|
fmt.Println("two")
|
||||||
|
for {
|
||||||
|
val := <-h.counter
|
||||||
|
h.queueSize += val
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
h.fetchAllFeeds()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) fetchFeed(feed storage.Feed) {
|
||||||
|
h.queueSize += 1
|
||||||
|
h.feedQueue <- feed
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) fetchAllFeeds() {
|
||||||
|
for _, feed := range h.db.ListFeeds() {
|
||||||
|
fmt.Println("here", feed)
|
||||||
|
h.fetchFeed(feed)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func p(path string, handler func(http.ResponseWriter, *http.Request)) Route {
|
func p(path string, handler func(http.ResponseWriter, *http.Request)) Route {
|
||||||
@ -103,7 +139,12 @@ func writeJSON(rw http.ResponseWriter, data interface{}) {
|
|||||||
|
|
||||||
func New() *http.Server {
|
func New() *http.Server {
|
||||||
db, _ := storage.New()
|
db, _ := storage.New()
|
||||||
h := Handler{db: db}
|
h := Handler{
|
||||||
|
db: db,
|
||||||
|
feedQueue: make(chan storage.Feed),
|
||||||
|
counter: make(chan int),
|
||||||
|
}
|
||||||
s := &http.Server{Addr: "127.0.0.1:8000", Handler: h}
|
s := &http.Server{Addr: "127.0.0.1:8000", Handler: h}
|
||||||
|
h.startJobs()
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package storage
|
package storage
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type ItemStatus int
|
type ItemStatus int
|
||||||
|
|
||||||
@ -18,8 +21,8 @@ type Item struct {
|
|||||||
Description string
|
Description string
|
||||||
Content string
|
Content string
|
||||||
Author string
|
Author string
|
||||||
Date int64
|
Date *time.Time
|
||||||
DateUpdated int64
|
DateUpdated *time.Time
|
||||||
Status ItemStatus
|
Status ItemStatus
|
||||||
Image string
|
Image string
|
||||||
}
|
}
|
||||||
|
@ -35,8 +35,8 @@ create table if not exists items (
|
|||||||
description text,
|
description text,
|
||||||
content text,
|
content text,
|
||||||
author text,
|
author text,
|
||||||
date integer,
|
date datetime,
|
||||||
date_updated integer,
|
date_updated datetime,
|
||||||
status integer,
|
status integer,
|
||||||
image text
|
image text
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user