From e62cb0bc57b027efa0d4550617e940ce434d0d05 Mon Sep 17 00:00:00 2001 From: Sean Hinchee Date: Thu, 8 Feb 2018 01:02:43 +0000 Subject: [PATCH] revised foods, hope it works --- x/mux/commits.go | 84 ++++++++++++++++++++++++++++-------------------- x/mux/config.go | 14 +++++++- 2 files changed, 63 insertions(+), 35 deletions(-) diff --git a/x/mux/commits.go b/x/mux/commits.go index 7c1c4ce1122e16e65833f027fac666e9c46340f1..f67ac2165cb1310f2e3a8410fb995ce78b753561 100644 --- a/x/mux/commits.go +++ b/x/mux/commits.go @@ -15,14 +15,27 @@ import ( func Listener() { for { for p, _ := range Config.Feeds { - fmt.Print("Updating feed: ", Config.Feeds[p].Title, "\n") - err := Config.Feeds[p].Update() + fmt.Print("Updating feed: ", Config.Feeds[p].Feed.Title, "\n") + err := Config.Feeds[p].Feed.Update() if err != nil { fmt.Println("Error in updating RSS feed, see: x/mux/commits.go") fmt.Printf("%s\n\n", err) } else { - Notify(p) + additem := true + for j, _ := range Config.Feeds[p].Recent { + if Config.Feeds[p].Recent[j] == Config.Feeds[p].Feed.Items[0].Title { + fmt.Println("Checking Recent ", j, " against ", Config.Feeds[p].Feed.Items[0].Title) + additem = false + } + } + if additem { + // x y z → x y z 0 → y z 0 + Config.Feeds[p].Recent = append(Config.Feeds[p].Recent, Config.Feeds[p].Feed.Items[0].Title) + Config.Feeds[p].Recent = Config.Feeds[p].Recent[1:] + break + Notify(p) + } } time.Sleep(1 * time.Minute) } @@ -32,32 +45,31 @@ func Listener() { // Notify subscribed channels to subscribed feeds func Notify(id int) { - str := Config.Feeds[id].UpdateURL + // maybe only do at init step? + str := Config.Feeds[id].Feed.UpdateURL feed, _ := rss.Fetch(str) - Config.Feeds[id] = *feed + Config.Feeds[id].Feed = *feed - if !Config.Feeds[id].Items[0].Read { - resp := ".\n" - - resp += "**" + Config.Feeds[id].Title + ": **" + "\n" - resp += Config.Feeds[id].Items[0].Date.String() + "\n\n" - resp += "`" + Config.Feeds[id].Items[0].Title + "`" + "\n" - resp += "\n" + Config.Feeds[id].Items[0].Link + "\n" - Config.Feeds[id].Items[0].Read = true - resp += "\n" - - // Loop through subbed chans and post notification message - fmt.Println("Looping through subs to notify...") - for _, v := range Config.Subs { - if v.SubID == id { - Session.ChannelMessageSend(v.ChanID, resp) - } - time.Sleep(10 * time.Millisecond) + resp := ".\n" + resp += "**" + Config.Feeds[id].Feed.Title + ": **" + "\n" + resp += Config.Feeds[id].Feed.Items[0].Date.String() + "\n\n" + resp += "`" + Config.Feeds[id].Feed.Items[0].Title + "`" + "\n" + resp += "\n" + Config.Feeds[id].Feed.Items[0].Link + "\n" + Config.Feeds[id].Feed.Items[0].Read = true + resp += "\n" + + // Loop through subbed chans and post notification message + fmt.Println("Looping through subs to notify...") + for _, v := range Config.Subs { + if v.SubID == id { + Session.ChannelMessageSend(v.ChanID, resp) } + time.Sleep(10 * time.Millisecond) } - fmt.Println("No new notifys for ", Config.Feeds[id].UpdateURL) - fmt.Println(Config.Feeds[id].Items[0]) - fmt.Println(Config.Feeds[id].Items[len(Config.Feeds[id].Items)-1]) + + fmt.Println("No new notifys for ", Config.Feeds[id].Feed.UpdateURL) + fmt.Println(Config.Feeds[id].Feed.Items[0]) + fmt.Println(Config.Feeds[id].Feed.Items[len(Config.Feeds[id].Feed.Items)-1]) } @@ -69,12 +81,12 @@ func (m *Mux) Last(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { 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[0].Date.String() + "\n\n" - resp += "`" + Config.Feeds[id].Items[0].Title + "`" + "\n" - resp += "\n" + Config.Feeds[id].Items[0].Link + "\n" - Config.Feeds[id].Items[0].Read = true - fmt.Println("Last-ing notification: ", Config.Feeds[id].Items[0]) + resp += "**" + Config.Feeds[id].Feed.Title + ": **" + "\n" + resp += Config.Feeds[id].Feed.Items[0].Date.String() + "\n\n" + resp += "`" + Config.Feeds[id].Feed.Items[0].Title + "`" + "\n" + resp += "\n" + Config.Feeds[id].Feed.Items[0].Link + "\n" + Config.Feeds[id].Feed.Items[0].Read = true + fmt.Println("Last-ing notification: ", Config.Feeds[id].Feed.Items[0]) } else { resp += "Denied fetch. Invalid stream id, see: list command" } @@ -91,7 +103,7 @@ 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 + ", " + v.Link + "\n" + resp += strconv.Itoa(p) + ": " + v.Feed.Title + ", " + v.Feed.Link + "\n" } resp += "```\n" @@ -111,7 +123,7 @@ func (m *Mux) Add(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { for _, v := range Config.Feeds { // this is bad matching, can't have two bitbucket url's? - if strings.Contains(url, v.UpdateURL) { + if strings.Contains(url, v.Feed.UpdateURL) { //fmt.Println(url) //fmt.Println(v.Link) resp += "Denied! Feed already subscribed to." @@ -133,7 +145,11 @@ func (m *Mux) Add(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { } // Might not be thread safe - Config.Feeds = append(Config.Feeds, *feed) + var tmpFeed Feed + tmpFeed.Feed = *feed + // Maybe make the size here a Config variable + tmpFeed.Recent = make([]string, 3) + Config.Feeds = append(Config.Feeds, tmpFeed) resp += "Added." resp += "```\n" diff --git a/x/mux/config.go b/x/mux/config.go index 2665161e8d6f47b825bb447e974664dcc99dcc1f..b1c2ac4a0deeaacb67483429d16b3aa1dfd027a9 100644 --- a/x/mux/config.go +++ b/x/mux/config.go @@ -17,9 +17,15 @@ type Subscription struct { SubID int } +// Stores a Feed and a list of the last n commits; n=3 +type Feed struct { + Feed rss.Feed + Recent []string +} + // Stores config for current state type Configuration struct { - Feeds []rss.Feed + Feeds []Feed Subs []Subscription } @@ -27,6 +33,12 @@ type Configuration struct { func (c *Configuration) Init(s *discordgo.Session) { c.Read() Session = s + for id, _ := range Config.Feeds { + // maybe only do at init step? + str := Config.Feeds[id].Feed.UpdateURL + feed, _ := rss.Fetch(str) + Config.Feeds[id].Feed = *feed + } go Listener() }