From 1f419017336c02465eaae39736abb6ef23d13919 Mon Sep 17 00:00:00 2001 From: Sean Hinchee Date: Tue, 30 Jan 2018 03:10:22 +0000 Subject: [PATCH] added rss feed support --- TODO | 5 +++++ main.go | 6 +++-- mux.go | 8 ++++++- x/mux/commits.go | 58 +++++++++++++++++++++++++++++++++++++++++++----- x/mux/config.go | 4 ++-- 5 files changed, 70 insertions(+), 11 deletions(-) diff --git a/TODO b/TODO index a6a87d853cdbb77fb210e51b2b183574f0acf742..5c206d63875b1b0125c5b3b9e56dd679d61f7551 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,4 @@ +Features * g!sum acid(1) * g!man acid(1) or g!man 1 acid * rss feed subscription (tracking commits) @@ -5,3 +6,7 @@ * 9front/plan9 source searching * various cat-v fetches (harmful, page search [get lucky]) * Auto-propaganda processing for images (Black and White -> (9)-ify) + +-- +Problems +* Move feed caching to another file than the .cfg (seriously this is a fcking mess) diff --git a/main.go b/main.go index 9428104596d4925b5288ebf95cf1584c87053e4c..310590a95e9e375bae65e2140851b1d8a673211a 100644 --- a/main.go +++ b/main.go @@ -77,9 +77,11 @@ func main() { } // Init Mux daemons + // Problem child Listener is go mux.Listener() - go mux.CommMux() - go CommMux() + +// go mux.CommMux() +// go CommMux() go mux.Config.Init() Session.UpdateStatus(0, "with #cat-v") diff --git a/mux.go b/mux.go index de73c1b8f2ed5fa90bed83447ab3889196181d8e..fba7d140cc2c49a78c5ab5ae563d4bf1acacdaf0 100644 --- a/mux.go +++ b/mux.go @@ -19,10 +19,16 @@ func init() { Router.Route("help", "Display this message.", Router.Help) Router.Route("fortune", "Display fortunes. Bonus files are (theo troll rsc bullshit terry rob).", Router.Fortunes) + + Router.Route("subscribe", "Subscribe to an RSS feed.", Router.Subscribe) + + Router.Route("list", "List RSS feeds Glenda is subscribed to by id.", Router.List) + + Router.Route("last", "Show the last commit for a given RSS feed by id.", Router.Last) Router.Route("dump", "Dump config to file.", Router.Dump) Router.Route("about", "General information about the bot.", Router.About) + } - diff --git a/x/mux/commits.go b/x/mux/commits.go index 4d0b92e8284f42e7923f7720bfaf61bd107e8116..1876f50295b07249c8efcb4fd6c6ef48bbeb5ce1 100644 --- a/x/mux/commits.go +++ b/x/mux/commits.go @@ -5,6 +5,8 @@ import ( "github.com/SlyMarbo/rss" "fmt" "time" + "strings" + "strconv" ) /* internal functions */ @@ -13,24 +15,52 @@ import ( func Listener() { for { for _, feed := range Config.Feeds { + fmt.Print("Printing feed: ", feed, "\n") err := feed.Update() if err != nil { fmt.Println("Error in updating RSS feed, see: x/mux/commits.go") fmt.Printf("%s\n\n", err) } - time.Sleep(1000 * time.Second) + time.Sleep(10 * time.Minute) } + time.Sleep(10 * time.Minute) } } /* Commands for Mux */ -// Print last commit or -func (m *Mux) Commits(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { - resp := "```\n" +// Print last commit for a given feed (by subscribed id int [See: List(...)]) +func (m *Mux) Last(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { + resp := ".\n" + + id, _ := strconv.Atoi(ctx.Fields[len(ctx.Fields) -1]) + if id >= 0 && id < len(Config.Feeds) { + resp += "**" + Config.Feeds[id].Title + ": **" + "\n" + resp += Config.Feeds[id].Items[ len(Config.Feeds[id].Items) -1].Date.String() + "\n\n" + resp += "`" + Config.Feeds[id].Items[ len(Config.Feeds[id].Items) -1].Title + "`" + "\n" + //resp += Config.Feeds[id].Items[ len(Config.Feeds[id].Items) -1].Summary + "\n" + //resp += Config.Feeds[id].Items[ len(Config.Feeds[id].Items) -1].Content + "\n" + resp += "\n" + Config.Feeds[id].Items[ len(Config.Feeds[id].Items) -1].Link + "\n" + } else { + resp += "Denied fetch. Invalid stream id, see: list command" + } + + resp += "\n" + + ds.ChannelMessageSend(dm.ChannelID, resp) + + return +} +// List all subscribed feeds +// Print last commit for a given feed +func (m *Mux) List(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { + resp := "```\n" + for p, v := range Config.Feeds { + resp += strconv.Itoa(p) + ": " + v.Title + "\n" + } resp += "```\n" @@ -42,18 +72,34 @@ func (m *Mux) Commits(ds *discordgo.Session, dm *discordgo.Message, ctx *Context // Subscribe to a feed func (m *Mux) Subscribe(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { //http://code.9front.org/hg/plan9front/rss-log - // TODO -- check if feed is already subscribed to resp := "```\n" + // URL to feed should be last item + url := ctx.Fields[len(ctx.Fields) -1] + fmt.Println("Proposed subscribe for: ", url) + + for _, v := range Config.Feeds { + if strings.Contains(url, v.Link) { + resp += "Denied! Feed already subscribed to." + resp += "```\n" + ds.ChannelMessageSend(dm.ChannelID, resp) + return + } + } - feed, err := rss.Fetch("") + feed, err := rss.Fetch(url) if err != nil { fmt.Println("Error in reading RSS feed, see: x/mux/commits.go") fmt.Printf("%s\n\n", err) + resp += "Denied! Could not parse feed." + resp += "```\n" + ds.ChannelMessageSend(dm.ChannelID, resp) + return } // Might not be thread safe Config.Feeds = append(Config.Feeds, *feed) + resp += "Subscribed." resp += "```\n" ds.ChannelMessageSend(dm.ChannelID, resp) diff --git a/x/mux/config.go b/x/mux/config.go index 9958dac98afd248b7bf64352c688e6657344cb81..8de34900ea09c2171368d929bb40d4ba2bd33743 100644 --- a/x/mux/config.go +++ b/x/mux/config.go @@ -45,7 +45,7 @@ func (c *Configuration) Write() (rerr error) { rerr = err } } - + return } @@ -53,6 +53,7 @@ func (c *Configuration) Write() (rerr error) { func (c *Configuration) Read() (rerr error) { RD: f, err := os.Open("./cfg/glenda.cfg") + defer f.Close() if err != nil { if strings.Contains(err.Error(), "no such file or directory") { // danger: this can go infinite @@ -71,7 +72,6 @@ func (c *Configuration) Read() (rerr error) { fmt.Printf("%s\n", err) rerr = err Config.Write() - goto RD } }