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

Commit

Permalink
feat(bot): export feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
indes committed Oct 29, 2019
1 parent f5e9280 commit 780ca00
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
File renamed without changes.
30 changes: 28 additions & 2 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,33 @@ func makeHandle() {
})

B.Handle("/export", func(m *tb.Message) {
sourceList, err := model.GetSourcesByUserID(m.Chat.ID)

_, _ = B.Send(m.Chat, fmt.Sprintf("export"))
if err != nil {
log.Println(err.Error())
_, _ = B.Send(m.Chat, fmt.Sprintf("导出失败"))
return
}

if len(sourceList) == 0 {
_, _ = B.Send(m.Chat, fmt.Sprintf("订阅列表为空"))
return
}

opmlStr, err := ToOPML(sourceList)

if err != nil {
_, _ = B.Send(m.Chat, fmt.Sprintf("导出失败"))
return
}
opmlFile := &tb.Document{File: tb.FromReader(strings.NewReader(opmlStr))}
opmlFile.FileName = "subscriptions.opml"
_, err = B.Send(m.Chat, opmlFile)

if err != nil {
_, _ = B.Send(m.Chat, fmt.Sprintf("导出失败"))
log.Println("[export]", err)
}
})

B.Handle("/sub", func(m *tb.Message) {
Expand Down Expand Up @@ -445,7 +470,8 @@ func makeHandle() {
/list 查看当前订阅源
/set 设置订阅
/help 帮助
/import 导入OPML文件
/import 导入 OPML 文件
/export 导出 OPML 文件
详细使用方法请看:https://github.com/indes/flowerss-bot
`
_, _ = B.Send(m.Chat, message)
Expand Down
18 changes: 18 additions & 0 deletions bot/opml.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bot
import (
"crypto/tls"
"encoding/xml"
"github.com/indes/flowerss-bot/model"
"io/ioutil"
"net/http"
"net/url"
Expand Down Expand Up @@ -114,3 +115,20 @@ func (o OPML) XML() (string, error) {
b, err := xml.MarshalIndent(o, "", "\t")
return xml.Header + string(b), err
}

func ToOPML(sources []model.Source) (string, error) {
O := OPML{}
O.XMLName.Local = "opml"
O.Version = "2.0"
O.XMLName.Space = ""
O.Head.Title = "subscriptions in flowerss"
O.Head.DateCreated = time.Now().Format(time.RFC1123)
for _, s := range sources {
outline := Outline{}
outline.Text = s.Title
outline.Type = "rss"
outline.XMLURL = s.Link
O.Body.Outlines = append(O.Body.Outlines, outline)
}
return O.XML()
}
1 change: 1 addition & 0 deletions model/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func GetSourcesByUserID(userID int64) ([]Source, error) {

return sources, nil
}

func (s *Source) AddErrorCount() {
s.ErrorCount++
s.Save()
Expand Down

0 comments on commit 780ca00

Please sign in to comment.