Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #24 from yichya/fix-xml-parsing-and-magic-number
Browse files Browse the repository at this point in the history
fix UpdateFeed xml parsing; fix ErrorCount magic number
  • Loading branch information
indes authored Dec 13, 2019
2 parents 42228e8 + 6b23c26 commit 45576ba
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 45576ba

Please sign in to comment.