Skip to content

Commit

Permalink
feat(subscribe): optimze performance
Browse files Browse the repository at this point in the history
  • Loading branch information
indes committed Jan 15, 2019
1 parent 37bdfc8 commit ac86744
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
12 changes: 9 additions & 3 deletions bot/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ import (
func registFeed(chat *telebot.Chat, url string) {
msg, _ := B.Send(chat, "处理中...")

source, _ := model.FindOrNewSourceByUrl(url)
err := model.RegistFeed(chat.ID, source.ID)
log.Printf("%d subscribe %s %s", chat.ID, source.Title, source.Link)
source, err := model.FindOrNewSourceByUrl(url)

if err != nil {
msg, _ = B.Edit(msg, fmt.Sprintf("%s,订阅失败", err))
return
}

err = model.RegistFeed(chat.ID, source.ID)
log.Printf("%d subscribe [%d]%s %s", chat.ID, source.ID, source.Title, source.Link)

if err == nil {
msg, _ = B.Edit(msg, fmt.Sprintf("<%s> 订阅成功", source.Title))
Expand Down
10 changes: 6 additions & 4 deletions model/source.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

import (
"errors"
"github.com/SlyMarbo/rss"
"log"
)
Expand All @@ -15,11 +16,13 @@ type Source struct {
}

func (s *Source) appendContents(items []*rss.Item) error {
db := getConnect()
defer db.Close()
for _, item := range items {
c, _ := getContentByFeedItem(s, item)
s.Content = append(s.Content, c)
}

db.Save(&s)
return nil
}

Expand Down Expand Up @@ -48,17 +51,16 @@ func FindOrNewSourceByUrl(url string) (*Source, error) {
// parsing task
feed, err := rss.Fetch(url)
if err != nil {
log.Println("Unable to make request: ", err)
return nil, err
return nil, errors.New("Feed 抓取错误")
}

source.Title = feed.Title
source.ErrorCount = 0

// Get contents and insert
items := feed.Items
source.appendContents(items)
db.Create(&source)
go source.appendContents(items)
return &source, nil
}
return nil, err
Expand Down

0 comments on commit ac86744

Please sign in to comment.