Skip to content

Commit

Permalink
moved the rss2 parser into its own repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Billy Ernest committed Apr 27, 2019
1 parent 344f694 commit a97f77b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 144 deletions.
Binary file added images/0d5f15c9-6271-46b0-ae72-6f41a66168c6.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "rssfeed",
"name": "RSSFeed",
"description": "This plugin serves as an rss subscription service for Mattermost.",
"version": "0.0.2",
"version": "0.0.3",
"server": {
"executables": {
"linux-amd64": "server/dist/plugin-linux-amd64",
Expand Down
2 changes: 1 addition & 1 deletion server/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ var manifest = struct {
Version string
}{
Id: "rssfeed",
Version: "0.0.2",
Version: "0.0.3",
}
91 changes: 25 additions & 66 deletions server/plugin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"github.com/lunny/html2md"
"github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model"
Expand All @@ -10,6 +11,7 @@ import (
"strconv"
"sync"
"time"
"github.com/wbernest/rss-v2-parser"
)

const RSSFEED_ICON_URL = "https://mattermost.gridprotectionalliance.org/plugins/rssfeed/images/rss.png"
Expand Down Expand Up @@ -74,12 +76,14 @@ func (p *RSSFeedPlugin) setupHeartBeat() {
func (p *RSSFeedPlugin) processHeartBeat() error {
dictionaryOfSubscriptions, err := p.getSubscriptions()
if err != nil {
mlog.Error(err.Error())
return nil
return err
}

for _, value := range dictionaryOfSubscriptions.Subscriptions {
p.processSubscription(value)
err := p.processSubscription(value)
if err != nil {
p.API.LogError(err.Error())
}
}

return nil
Expand All @@ -92,86 +96,41 @@ func (p *RSSFeedPlugin) getHeartbeatTime() (int, error) {
if len(config.Heartbeat) > 0 {
heartbeatTime, err = strconv.Atoi(config.Heartbeat)
if err != nil {
mlog.Error(err.Error())
return 15, err
}

}

return heartbeatTime, nil
}

func (p *RSSFeedPlugin) processSubscription(subscription *Subscription) error {
if len(subscription.URL) == 0 {
return nil
return errors.New("no url supplied")
}

//p.API.LogInfo("Process subscription. url = " + subscription.URL)
//p.API.LogInfo("Process subscription. xml = " + subscription.XML)

// retrieve old xml feed from database
if len(subscription.XML) > 0 {
oldRssFeed, err := RssParseString(subscription.XML)
if err != nil {
p.API.LogError("Go Feed failed to parse old subscription.")
p.API.LogError(err.Error())

return err
}
//p.API.LogInfo(fmt.Sprintf("%v", oldRssFeed))

newRssFeed, newRssFeedString, err := RssParseURL(subscription.URL)
if err != nil {
p.API.LogError(err.Error())
return err
}

// check each item in new feed to see if they exist in old feed
// if they do not exist post the new item to the channel and update
// xml in the subscription object

postsMade := false
for _, item := range newRssFeed.ItemList {
exists := false
for _, oldItem := range oldRssFeed.ItemList {
if oldItem.Guid == item.Guid {
exists = true
}
}

// if the item does not exist post it to the correct channel
if !exists {
postsMade = true
post := item.Title + "\n" + item.Link + "\n" + html2md.Convert(item.Description) + "\n"
p.createBotPost(subscription.ChannelID, post, model.POST_DEFAULT)
}
}

if postsMade {
subscription.XML = newRssFeedString
p.updateSubscription(subscription)
}

} else {
//p.API.LogInfo("Gettings RSS for url = " + subscription.URL)

newRssFeed, newRssFeedXML, err := RssParseURL(subscription.URL)
if err != nil {
p.API.LogError("Go Feed failed to parse new subscription.")
p.API.LogError(err.Error())
// get new rss feed string from url
newRssFeed, newRssFeedString, err := rssv2parser.RssParseURL(subscription.URL)
if err != nil {
return err
}

return err
}
// retrieve old xml feed from database
oldRssFeed, err := rssv2parser.RssParseString(subscription.XML)
if err != nil {
return err
}

//p.API.LogInfo(fmt.Sprintf("New RSS Feed Title %s\n Description %s\n", newRssFeed.Title, newRssFeed.Description))
//.API.LogInfo(fmt.Sprintf("New RSS Feed Items %v", newRssFeed.ItemList))
items := rssv2parser.CompareItems(oldRssFeed, newRssFeed)

for _, item := range newRssFeed.ItemList {
post := item.Title + "\n" + item.Link + "\n" + html2md.Convert(item.Description) + "\n"
p.createBotPost(subscription.ChannelID, post, model.POST_DEFAULT)
}
for _, item := range items {
post := item.Title + "\n" + item.Link + "\n" + html2md.Convert(item.Description) + "\n"
p.createBotPost(subscription.ChannelID, post, model.POST_DEFAULT)
}

subscription.XML = newRssFeedXML
if len(items) > 0 {
subscription.XML = newRssFeedString
p.updateSubscription(subscription)
}

Expand Down
76 changes: 0 additions & 76 deletions server/rssparser.go

This file was deleted.

0 comments on commit a97f77b

Please sign in to comment.