mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-12 03:05:14 +00:00
Feeds Categories (#53)
* add further accessor methods to feeds * add category to feeds
This commit is contained in:
@@ -25,14 +25,16 @@ func (e *Feeds) fetchAll() {
|
||||
return
|
||||
}
|
||||
for _, feed := range fs {
|
||||
err = e.fetch(feed.Url)
|
||||
err = e.fetch(feed)
|
||||
if err != nil {
|
||||
log.Errorf("Error saving post: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Feeds) fetch(url string) error {
|
||||
func (e *Feeds) fetch(f *feeds.Feed) error {
|
||||
url := f.Url
|
||||
|
||||
log.Infof("Fetching address %v", url)
|
||||
fd, err := rss.Fetch(url)
|
||||
if err != nil {
|
||||
@@ -42,17 +44,25 @@ func (e *Feeds) fetch(url string) error {
|
||||
|
||||
for _, item := range fd.Items {
|
||||
id := fmt.Sprintf("%x", md5.Sum([]byte(item.ID)))
|
||||
|
||||
err = e.entries.Create(feeds.Entry{
|
||||
Id: id,
|
||||
Url: item.Link,
|
||||
Title: item.Title,
|
||||
Domain: domain,
|
||||
Content: item.Summary,
|
||||
Date: item.Date.Unix(),
|
||||
Id: id,
|
||||
Url: item.Link,
|
||||
Title: item.Title,
|
||||
Domain: domain,
|
||||
Content: item.Summary,
|
||||
Date: item.Date.Unix(),
|
||||
Category: f.Category,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error saving item: %v", err)
|
||||
}
|
||||
|
||||
var tags []string
|
||||
if len(f.Category) > 0 {
|
||||
tags = append(tags, f.Category)
|
||||
}
|
||||
|
||||
// @todo make this optional
|
||||
_, err := e.postsService.Save(context.TODO(), &posts.SaveRequest{
|
||||
Id: id,
|
||||
@@ -63,11 +73,13 @@ func (e *Feeds) fetch(url string) error {
|
||||
"domain": domain,
|
||||
"link": item.Link,
|
||||
},
|
||||
Tags: tags,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -72,19 +72,24 @@ func (e *Feeds) crawl() {
|
||||
|
||||
func (e *Feeds) Add(ctx context.Context, req *feeds.AddRequest, rsp *feeds.AddResponse) error {
|
||||
log.Info("Received Feeds.New request")
|
||||
e.feeds.Create(feeds.Feed{
|
||||
Name: req.Name,
|
||||
Url: req.Url,
|
||||
})
|
||||
|
||||
f := feeds.Feed{
|
||||
Name: req.Name,
|
||||
Url: req.Url,
|
||||
Category: req.Category,
|
||||
}
|
||||
|
||||
// create the feed
|
||||
e.feeds.Create(f)
|
||||
|
||||
// schedule immediate fetch
|
||||
go e.fetch(&f)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Feeds) Entries(ctx context.Context, req *feeds.EntriesRequest, rsp *feeds.EntriesResponse) error {
|
||||
log.Info("Received Feeds.New request")
|
||||
err := e.fetch(req.Url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Info("Received Feeds.Entries request")
|
||||
return e.entries.Read(e.entriesURLIndex.ToQuery(req.Url), &rsp.Entries)
|
||||
}
|
||||
|
||||
@@ -108,3 +113,24 @@ func (e *Feeds) Remove(ctx context.Context, req *feeds.RemoveRequest, rsp *feeds
|
||||
e.feeds.Delete(model.QueryEquals("name", req.Name))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Feeds) List(ctx context.Context, req *feeds.ListRequest, rsp *feeds.ListResponse) error {
|
||||
var feeds []*feeds.Feed
|
||||
|
||||
err := e.feeds.Read(model.QueryAll(), &feeds)
|
||||
if err != nil {
|
||||
return errors.InternalServerError("feeds.list", "failed to read list of feeds: %v", err)
|
||||
}
|
||||
|
||||
rsp.Feeds = feeds
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Feeds) Remove(ctx context.Context, req *feeds.RemoveRequest, rsp *feeds.RemoveResponse) error {
|
||||
if len(req.Name) == 0 {
|
||||
return errors.BadRequest("feeds.remove", "blank name provided")
|
||||
}
|
||||
|
||||
e.feeds.Delete(model.QueryEquals("name", req.Name))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -26,7 +26,9 @@ type Feed struct {
|
||||
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
||||
// rss feed url
|
||||
// eg. http://a16z.com/feed/
|
||||
Url string `protobuf:"bytes,2,opt,name=url,proto3" json:"url,omitempty"`
|
||||
Url string `protobuf:"bytes,2,opt,name=url,proto3" json:"url,omitempty"`
|
||||
// category of the feed
|
||||
Category string `protobuf:"bytes,3,opt,name=category,proto3" json:"category,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
@@ -71,6 +73,13 @@ func (m *Feed) GetUrl() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *Feed) GetCategory() string {
|
||||
if m != nil {
|
||||
return m.Category
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type Entry struct {
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
Domain string `protobuf:"bytes,2,opt,name=domain,proto3" json:"domain,omitempty"`
|
||||
@@ -78,6 +87,7 @@ type Entry struct {
|
||||
Title string `protobuf:"bytes,4,opt,name=title,proto3" json:"title,omitempty"`
|
||||
Content string `protobuf:"bytes,5,opt,name=content,proto3" json:"content,omitempty"`
|
||||
Date int64 `protobuf:"varint,6,opt,name=date,proto3" json:"date,omitempty"`
|
||||
Category string `protobuf:"bytes,7,opt,name=category,proto3" json:"category,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
@@ -150,13 +160,22 @@ func (m *Entry) GetDate() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (m *Entry) GetCategory() string {
|
||||
if m != nil {
|
||||
return m.Category
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type AddRequest struct {
|
||||
// rss feed name
|
||||
// eg. a16z
|
||||
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
||||
// rss feed url
|
||||
// eg. http://a16z.com/feed/
|
||||
Url string `protobuf:"bytes,2,opt,name=url,proto3" json:"url,omitempty"`
|
||||
Url string `protobuf:"bytes,2,opt,name=url,proto3" json:"url,omitempty"`
|
||||
// category to add
|
||||
Category string `protobuf:"bytes,3,opt,name=category,proto3" json:"category,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
@@ -201,6 +220,13 @@ func (m *AddRequest) GetUrl() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *AddRequest) GetCategory() string {
|
||||
if m != nil {
|
||||
return m.Category
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type AddResponse struct {
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
@@ -470,28 +496,29 @@ func init() {
|
||||
func init() { proto.RegisterFile("proto/feeds.proto", fileDescriptor_dd517c38176c13bf) }
|
||||
|
||||
var fileDescriptor_dd517c38176c13bf = []byte{
|
||||
// 363 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x52, 0xcb, 0x4e, 0x2a, 0x41,
|
||||
0x10, 0xbd, 0xc3, 0x3c, 0xc8, 0xad, 0x01, 0x2e, 0xd4, 0x05, 0xd2, 0x99, 0x15, 0xb6, 0x89, 0x61,
|
||||
0x61, 0x30, 0xe0, 0xc2, 0xa8, 0x2b, 0x4c, 0x74, 0xe5, 0x6a, 0x96, 0xee, 0xd0, 0x2e, 0x93, 0x49,
|
||||
0x60, 0x06, 0xe9, 0xc6, 0xc4, 0x0f, 0xf0, 0x3b, 0xfd, 0x15, 0xd3, 0xaf, 0xe1, 0xb1, 0x30, 0xee,
|
||||
0xea, 0x9c, 0xaa, 0x73, 0xba, 0xfa, 0x74, 0x43, 0x6f, 0xbd, 0xa9, 0x54, 0x75, 0xf1, 0x4a, 0x24,
|
||||
0xe4, 0xc4, 0xd4, 0x18, 0x1b, 0xc0, 0xcf, 0x21, 0x7a, 0x20, 0x12, 0x88, 0x10, 0x95, 0x8b, 0x15,
|
||||
0xb1, 0x60, 0x14, 0x8c, 0xff, 0xe6, 0xa6, 0xc6, 0x2e, 0x84, 0xdb, 0xcd, 0x92, 0x35, 0x0c, 0xa5,
|
||||
0x4b, 0xfe, 0x19, 0x40, 0x7c, 0x5f, 0xaa, 0xcd, 0x07, 0x76, 0xa0, 0x51, 0x08, 0x37, 0xdd, 0x28,
|
||||
0x04, 0x0e, 0x21, 0x11, 0xd5, 0x6a, 0x51, 0x94, 0x6e, 0xdc, 0x21, 0xef, 0x11, 0xd6, 0x1e, 0xd8,
|
||||
0x87, 0x58, 0x15, 0x6a, 0x49, 0x2c, 0x32, 0x9c, 0x05, 0xc8, 0xa0, 0xf9, 0x52, 0x95, 0x8a, 0x4a,
|
||||
0xc5, 0x62, 0xc3, 0x7b, 0xa8, 0x37, 0x13, 0x0b, 0x45, 0x2c, 0x19, 0x05, 0xe3, 0x30, 0x37, 0x35,
|
||||
0x9f, 0x01, 0xcc, 0x85, 0xc8, 0xe9, 0x6d, 0x4b, 0x52, 0xfd, 0x72, 0xf7, 0x36, 0xa4, 0x46, 0x23,
|
||||
0xd7, 0x55, 0x29, 0x89, 0x73, 0xe8, 0xe8, 0x9b, 0x14, 0x24, 0xbd, 0x8d, 0x93, 0x04, 0x3b, 0xc9,
|
||||
0x35, 0xfc, 0xab, 0x67, 0xac, 0x0c, 0xcf, 0xa0, 0x49, 0x96, 0x62, 0xc1, 0x28, 0x1c, 0xa7, 0xb3,
|
||||
0xd6, 0xc4, 0xa6, 0x6a, 0x62, 0xc9, 0x7d, 0x53, 0x9f, 0xf6, 0x58, 0x48, 0xe5, 0xbc, 0xf9, 0x14,
|
||||
0x5a, 0x16, 0x3a, 0x9b, 0x13, 0xb0, 0xf9, 0x3b, 0x93, 0xd4, 0x99, 0xe8, 0xa7, 0xc8, 0xdd, 0xcb,
|
||||
0x9c, 0x42, 0x3b, 0xa7, 0x55, 0xf5, 0x4e, 0x3f, 0x5c, 0x93, 0x77, 0xa1, 0xe3, 0x87, 0xac, 0xf3,
|
||||
0xec, 0x2b, 0x80, 0x58, 0xdb, 0x48, 0x9c, 0x40, 0x38, 0x17, 0x02, 0x7b, 0xce, 0x7b, 0x17, 0x58,
|
||||
0x86, 0xfb, 0x94, 0xcb, 0xe3, 0x0f, 0x5e, 0x41, 0x62, 0xbd, 0xb0, 0xef, 0xfa, 0x07, 0xe7, 0x67,
|
||||
0x83, 0x23, 0xb6, 0x16, 0xde, 0x40, 0xd3, 0xc5, 0x84, 0x83, 0xbd, 0x34, 0x76, 0xd1, 0x66, 0xc3,
|
||||
0x63, 0xba, 0xd6, 0x4e, 0x21, 0xd2, 0xc1, 0xa0, 0x5f, 0x69, 0x2f, 0xb4, 0xec, 0xff, 0x01, 0xe7,
|
||||
0x25, 0x77, 0xed, 0xa7, 0xd4, 0x7c, 0xe1, 0x5b, 0xd3, 0x7d, 0x4e, 0x0c, 0xb8, 0xfc, 0x0e, 0x00,
|
||||
0x00, 0xff, 0xff, 0x30, 0x85, 0x1a, 0xbb, 0xe4, 0x02, 0x00, 0x00,
|
||||
// 384 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x93, 0xbb, 0x4e, 0xf3, 0x40,
|
||||
0x10, 0x85, 0x7f, 0xc7, 0xb7, 0x9f, 0x71, 0x12, 0x92, 0x21, 0x89, 0x56, 0xae, 0x82, 0x91, 0x50,
|
||||
0xaa, 0xa0, 0x84, 0x02, 0x01, 0x55, 0x90, 0x40, 0x14, 0x88, 0xc2, 0x25, 0x9d, 0xc9, 0x0e, 0xc8,
|
||||
0x52, 0x62, 0x07, 0x7b, 0x83, 0x94, 0xc7, 0xe1, 0xc9, 0x78, 0x15, 0xe4, 0xbd, 0x38, 0x97, 0x82,
|
||||
0x8a, 0x6e, 0xce, 0xd9, 0x9d, 0x6f, 0xc7, 0x67, 0x64, 0xe8, 0xae, 0x8a, 0x5c, 0xe4, 0x17, 0x6f,
|
||||
0x44, 0xbc, 0x1c, 0xcb, 0x1a, 0x5d, 0x29, 0xa2, 0x47, 0x70, 0x1e, 0x88, 0x38, 0x22, 0x38, 0x59,
|
||||
0xb2, 0x24, 0x66, 0x0d, 0xad, 0xd1, 0x51, 0x2c, 0x6b, 0xec, 0x80, 0xbd, 0x2e, 0x16, 0xac, 0x21,
|
||||
0xad, 0xaa, 0xc4, 0x10, 0xfe, 0xcf, 0x13, 0x41, 0xef, 0x79, 0xb1, 0x61, 0xb6, 0xb4, 0x6b, 0x1d,
|
||||
0x7d, 0x59, 0xe0, 0xde, 0x67, 0xa2, 0xd8, 0x60, 0x1b, 0x1a, 0x29, 0xd7, 0xa4, 0x46, 0xca, 0x71,
|
||||
0x00, 0x1e, 0xcf, 0x97, 0x49, 0x9a, 0x69, 0x94, 0x56, 0x86, 0x6f, 0x6f, 0xf9, 0x3d, 0x70, 0x45,
|
||||
0x2a, 0x16, 0xc4, 0x1c, 0xe9, 0x29, 0x81, 0x0c, 0xfc, 0x79, 0x9e, 0x09, 0xca, 0x04, 0x73, 0xa5,
|
||||
0x6f, 0x64, 0x35, 0x35, 0x4f, 0x04, 0x31, 0x6f, 0x68, 0x8d, 0xec, 0x58, 0xd6, 0x7b, 0x33, 0xfa,
|
||||
0x07, 0x33, 0x3e, 0x03, 0xcc, 0x38, 0x8f, 0xe9, 0x63, 0x4d, 0xa5, 0xf8, 0x83, 0x6f, 0x6e, 0x41,
|
||||
0x20, 0x79, 0xe5, 0x2a, 0xcf, 0x4a, 0x8a, 0x22, 0x68, 0x57, 0x09, 0xa4, 0x54, 0x9a, 0x27, 0x34,
|
||||
0xce, 0xaa, 0x71, 0xd1, 0x35, 0x1c, 0xd7, 0x77, 0x54, 0x1b, 0x9e, 0x83, 0x4f, 0xca, 0x62, 0xd6,
|
||||
0xd0, 0x1e, 0x05, 0xd3, 0xe6, 0x58, 0x6d, 0x4a, 0xc6, 0x19, 0x9b, 0xc3, 0xea, 0xb5, 0xa7, 0xb4,
|
||||
0x14, 0x9a, 0x1d, 0x4d, 0xa0, 0xa9, 0xa4, 0xc6, 0x9c, 0x82, 0xda, 0xa9, 0x86, 0x04, 0x1a, 0x52,
|
||||
0xad, 0x37, 0xd6, 0xdb, 0x3e, 0x83, 0x56, 0x4c, 0xcb, 0xfc, 0x93, 0x7e, 0x89, 0x20, 0xea, 0x40,
|
||||
0xdb, 0x5c, 0x52, 0xe4, 0xe9, 0xb7, 0x05, 0x6e, 0x85, 0x29, 0x71, 0x0c, 0xf6, 0x8c, 0x73, 0xec,
|
||||
0x6a, 0xf6, 0x36, 0xcc, 0x10, 0x77, 0x2d, 0x9d, 0xc7, 0x3f, 0xbc, 0x02, 0x4f, 0xb1, 0xb0, 0xa7,
|
||||
0xcf, 0xf7, 0xde, 0x0f, 0xfb, 0x07, 0x6e, 0xdd, 0x78, 0x03, 0xbe, 0x8e, 0x09, 0xfb, 0x3b, 0x69,
|
||||
0x6c, 0xa3, 0x0d, 0x07, 0x87, 0x76, 0xdd, 0x3b, 0x01, 0xa7, 0x0a, 0x06, 0xcd, 0x48, 0x3b, 0xa1,
|
||||
0x85, 0x27, 0x7b, 0x9e, 0x69, 0xb9, 0x6b, 0xbd, 0x04, 0xf2, 0xb7, 0xb8, 0x95, 0xa7, 0xaf, 0x9e,
|
||||
0x14, 0x97, 0x3f, 0x01, 0x00, 0x00, 0xff, 0xff, 0xe4, 0x42, 0x5d, 0x93, 0x38, 0x03, 0x00, 0x00,
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ message Feed {
|
||||
// rss feed url
|
||||
// eg. http://a16z.com/feed/
|
||||
string url = 2;
|
||||
// category of the feed
|
||||
string category = 3;
|
||||
}
|
||||
|
||||
message Entry {
|
||||
@@ -27,6 +29,7 @@ message Entry {
|
||||
string title = 4;
|
||||
string content = 5;
|
||||
int64 date = 6;
|
||||
string category = 7;
|
||||
}
|
||||
|
||||
message AddRequest {
|
||||
@@ -36,6 +39,8 @@ message AddRequest {
|
||||
// rss feed url
|
||||
// eg. http://a16z.com/feed/
|
||||
string url = 2;
|
||||
// category to add
|
||||
string category = 3;
|
||||
}
|
||||
|
||||
message AddResponse {
|
||||
|
||||
Reference in New Issue
Block a user