Skip to content

Commit

Permalink
Add Roads and Kingdom plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
dewey committed Aug 31, 2018
1 parent 5207ead commit 363e0eb
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 4 deletions.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/dewey/feedbridge/api"
"github.com/dewey/feedbridge/config"
"github.com/dewey/feedbridge/plugin"
"github.com/dewey/feedbridge/plugins/roadsandkingdoms"
"github.com/dewey/feedbridge/plugins/scmp"
"github.com/dewey/feedbridge/runner"

Expand Down Expand Up @@ -56,6 +57,7 @@ func main() {

pluginRepo := plugin.NewMemRepo()
pluginRepo.Install(scmp.NewPlugin(l, c))
pluginRepo.Install(roadsandkingdoms.NewPlugin(l, c))

storageRepo, err := store.NewStoreBackend(cfg)
if err != nil {
Expand Down
93 changes: 93 additions & 0 deletions plugins/roadsandkingdoms/roadsandkingdoms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package roadsandkingdoms

import (
"io/ioutil"
"net/http"

pm "github.com/dewey/feedbridge/plugin"
"github.com/go-kit/kit/log"
"github.com/gorilla/feeds"
readability "github.com/mauidude/go-readability"
"github.com/mmcdole/gofeed"
)

// Plugin defines a new plugin
type plugin struct {
l log.Logger
c *http.Client
f *feeds.Feed
}

// NewPlugin initializes a new plugin
func NewPlugin(l log.Logger, c *http.Client) *plugin {
return &plugin{
l: log.With(l, "plugin", "roadsandkingdoms"),
c: c,
f: &feeds.Feed{
Title: "Roads & Kingdoms",
Link: &feeds.Link{Href: "https://roadsandkingdoms.com"},
Description: "Journalism and travel, together at last.",
Author: &feeds.Author{Name: "Roads & Kingdoms"},
},
}
}

func (p *plugin) Info() pm.PluginMetadata {
return pm.PluginMetadata{
TechnicalName: "roadsandkingdoms",
Name: p.f.Title,
Description: "Proving a full content feed not just snippets.",
Author: "Philipp",
AuthorURL: "https://github.com/dewey",
Image: "https://i.imgur.com/ABzvg51.png",
SourceURL: "https://roadsandkingdoms.com",
}
}

func (p *plugin) Run() (*feeds.Feed, error) {
fp := gofeed.NewParser()
feed, err := fp.ParseURL("https://roadsandkingdoms.com/feed/")
if err != nil {
return nil, err
}

var feedItems []*feeds.Item
for _, fi := range feed.Items {
resp, err := p.c.Get(fi.Link)
if err != nil {
continue
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
continue
}
doc, err := readability.NewDocument(string(b))
if err != nil {
continue
}

content := doc.Content()
item := &feeds.Item{
Author: &feeds.Author{
Name: fi.Author.Name,
Email: fi.Author.Email,
},
Title: fi.Title,
Link: &feeds.Link{
Href: fi.Link,
},
Id: fi.GUID,
Description: content,
}
if fi.PublishedParsed != nil {
item.Created = *fi.PublishedParsed
}
if fi.UpdatedParsed != nil {
item.Updated = *fi.UpdatedParsed
}
feedItems = append(feedItems, item)
}
p.f.Items = feedItems
return p.f, nil
}
1 change: 0 additions & 1 deletion plugins/scmp/scmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func (p *plugin) Info() pm.PluginMetadata {
}
}

// Run runs the main checker function of the plugin
func (p *plugin) Run() (*feeds.Feed, error) {
it := []string{
"https://www.scmp.com/topics/infographics-asia",
Expand Down
16 changes: 13 additions & 3 deletions runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,19 @@ func (r *Runner) runPlugin(cp plugin.Plugin) (scrape.Statistic, error) {
if err != nil {
return scrape.Statistic{}, err
}
f.Sort(func(a, b *feeds.Item) bool {
return a.Created.After(b.Created)
})

var hasCreatedTimestamp bool
for _, fi := range f.Items {
if fi.Created.IsZero() {
hasCreatedTimestamp = false
}
}
if hasCreatedTimestamp {
f.Sort(func(a, b *feeds.Item) bool {
return a.Created.After(b.Created)
})
}

rss, err := f.ToRss()
if err != nil {
return scrape.Statistic{}, err
Expand Down

0 comments on commit 363e0eb

Please sign in to comment.