From 780ca0015ffb772c2a7c004a393f9298c2d07fdd Mon Sep 17 00:00:00 2001 From: indes Date: Tue, 29 Oct 2019 19:20:50 +0800 Subject: [PATCH] feat(bot): export feeds --- ChangeLog.md => CHANGELOG.md | 0 bot/bot.go | 30 ++++++++++++++++++++++++++++-- bot/opml.go | 18 ++++++++++++++++++ model/source.go | 1 + 4 files changed, 47 insertions(+), 2 deletions(-) rename ChangeLog.md => CHANGELOG.md (100%) diff --git a/ChangeLog.md b/CHANGELOG.md similarity index 100% rename from ChangeLog.md rename to CHANGELOG.md diff --git a/bot/bot.go b/bot/bot.go index a40971d4..2f325829 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -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) { @@ -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) diff --git a/bot/opml.go b/bot/opml.go index 7749c740..0e0324ed 100644 --- a/bot/opml.go +++ b/bot/opml.go @@ -3,6 +3,7 @@ package bot import ( "crypto/tls" "encoding/xml" + "github.com/indes/flowerss-bot/model" "io/ioutil" "net/http" "net/url" @@ -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() +} diff --git a/model/source.go b/model/source.go index 43b8bd17..bb956185 100644 --- a/model/source.go +++ b/model/source.go @@ -136,6 +136,7 @@ func GetSourcesByUserID(userID int64) ([]Source, error) { return sources, nil } + func (s *Source) AddErrorCount() { s.ErrorCount++ s.Save()