-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from skx/feat/rss
Added 'rss' sub-command because it is easier than sed/awk
- Loading branch information
Showing
5 changed files
with
210 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/mmcdole/gofeed" | ||
) | ||
|
||
// Structure for our options and state. | ||
type rssCommand struct { | ||
// format contains the format-string to display for entries. | ||
// We recognize "$link" and "$title". More might be added in the future. | ||
format string | ||
} | ||
|
||
// Arguments adds per-command args to the object. | ||
func (r *rssCommand) Arguments(f *flag.FlagSet) { | ||
f.StringVar(&r.format, "format", "$link", "Specify the format-string to display for entries") | ||
} | ||
|
||
// Info returns the name of this subcommand. | ||
func (r *rssCommand) Info() (string, string) { | ||
return "rss", `Show details from an RSS feed. | ||
Details: | ||
This command fetches the specified URLs as RSS feeds, and shows | ||
their contents in a simple fashion. By default only the entry URLs | ||
are shown, but a format-string may be used to specify the output. | ||
For example to show the link and title of entries: | ||
$ sysbox rss -format='$link $title' http://,.. | ||
Suggestions for additional fields/details to be displayed are | ||
welcome via issue-reports. | ||
Usage: | ||
$ sysbox rss url1 url2 .. urlN | ||
Note: | ||
Care must be taken to escape, or quote, the '$' character which | ||
is used in the format-string. | ||
` | ||
} | ||
|
||
// Process each specified feed. | ||
func (r *rssCommand) processFeed(url string) error { | ||
|
||
fp := gofeed.NewParser() | ||
feed, err := fp.ParseURL(url) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, ent := range feed.Items { | ||
txt := os.Expand( | ||
r.format, | ||
func(s string) string { | ||
switch s { | ||
case "content": | ||
return ent.Content | ||
case "link": | ||
return ent.Link | ||
case "title": | ||
return ent.Title | ||
default: | ||
return s | ||
} | ||
}, | ||
) | ||
|
||
fmt.Println(txt) | ||
} | ||
return err | ||
} | ||
|
||
// Execute is invoked if the user specifies `rss` as the subcommand. | ||
func (r *rssCommand) Execute(args []string) int { | ||
|
||
for _, u := range args { | ||
|
||
err := r.processFeed(u) | ||
if err != nil { | ||
fmt.Printf("Failed to process %s: %s\n", u, err) | ||
} | ||
} | ||
|
||
return 0 | ||
} |
Oops, something went wrong.