Skip to content

Commit

Permalink
fix UpdateFeed xml parsing; fix ErrorCount magic number
Browse files Browse the repository at this point in the history
Change-Id: Ib7780ab7a82fe0ee0d31e11ba88d65d0fae09499
  • Loading branch information
yinchenyang committed Dec 13, 2019
1 parent 42228e8 commit 6b23c26
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
7 changes: 4 additions & 3 deletions bot/controller.go
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"github.com/indes/flowerss-bot/bot/fsm"
"github.com/indes/flowerss-bot/config"
"github.com/indes/flowerss-bot/model"
tb "gopkg.in/tucnak/telebot.v2"
"html/template"
Expand Down Expand Up @@ -92,7 +93,7 @@ func toggleCtrlButtons(c *tb.Callback, action string) {
Text: "暂停更新",
}

if source.ErrorCount >= 100 {
if source.ErrorCount >= config.ErrorThreshold {
toggleEnabledKey.Text = "重启更新"
}

Expand Down Expand Up @@ -366,7 +367,7 @@ func setFeedItemBtnCtr(c *tb.Callback) {
Text: "暂停更新",
}

if source.ErrorCount >= 100 {
if source.ErrorCount >= config.ErrorThreshold {
toggleEnabledKey.Text = "重启更新"
}

Expand Down Expand Up @@ -768,7 +769,7 @@ func textCtr(m *tb.Message) {
Text: "暂停更新",
}

if source.ErrorCount >= 100 {
if source.ErrorCount >= config.ErrorThreshold {
toggleEnabledKey.Text = "重启更新"
}

Expand Down
44 changes: 24 additions & 20 deletions model/source.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/SlyMarbo/rss"
"github.com/indes/flowerss-bot/config"
"github.com/jinzhu/gorm"
"io/ioutil"
"log"
"net/http"
Expand Down Expand Up @@ -44,34 +45,37 @@ func GetSourceByUrl(url string) (*Source, error) {
return &source, nil
}

func fetchFunc(url string) (resp *http.Response, err error) {
resp, err = http.Get(url)
if err != nil {
return nil, err
}
var data []byte
if data, err = ioutil.ReadAll(resp.Body); err != nil {
_ = resp.Body.Close()
return nil, err
}
_ = resp.Body.Close()
resp.Body = ioutil.NopCloser(strings.NewReader(strings.Map(func(r rune) rune {
if unicode.IsPrint(r) {
return r
}
return -1
}, string(data))))
return
}

func FindOrNewSourceByUrl(url string) (*Source, error) {
var source Source
db := getConnect()
defer db.Close()

if err := db.Where("link=?", url).Find(&source).Error; err != nil {
if err.Error() == "record not found" {
if err == gorm.ErrRecordNotFound {
source.Link = url

// parsing task
feed, err := rss.FetchByFunc(func(url string) (resp *http.Response, err error) {
resp, err = http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data []byte
if data, err = ioutil.ReadAll(resp.Body); err != nil {
return nil, err
}
resp.Body = ioutil.NopCloser(strings.NewReader(strings.Map(func(r rune) rune {
if unicode.IsPrint(r) {
return r
}
return -1
}, string(data))))
return
}, url)
feed, err := rss.FetchByFunc(fetchFunc, url)

if err != nil {
return nil, fmt.Errorf("Feed 抓取错误 %v", err)
Expand Down Expand Up @@ -124,7 +128,7 @@ func (s *Source) IsSubscribed() bool {

func (s *Source) GetNewContents() ([]Content, error) {
var newContents []Content
feed, err := rss.Fetch(s.Link)
feed, err := rss.FetchByFunc(fetchFunc, s.Link)
if err != nil {
log.Println("Unable to make request: ", err, " ", s.Link)
s.AddErrorCount()
Expand Down
9 changes: 6 additions & 3 deletions model/subscribe.go
@@ -1,6 +1,9 @@
package model

import "errors"
import (
"errors"
"github.com/indes/flowerss-bot/config"
)

type Subscribe struct {
ID uint `gorm:"primary_key;AUTO_INCREMENT"`
Expand Down Expand Up @@ -190,10 +193,10 @@ func (s *Subscribe) ToggleTelegraph() error {
}

func (s *Source) ToggleEnabled() error {
if s.ErrorCount >= 100 {
if s.ErrorCount >= config.ErrorThreshold {
s.ErrorCount = 0
} else {
s.ErrorCount = 100
s.ErrorCount = config.ErrorThreshold
}

///TODO a hack for save source changes
Expand Down

0 comments on commit 6b23c26

Please sign in to comment.