Documentation ¶
Overview ¶
Package feed2json converts Atom and RSS feeds to JSON feeds.
Index ¶
- func Convert(from, to *bytes.Buffer) (err error)
- func FeedURLFromContext(ctx context.Context) (u *url.URL, valid bool)
- func Handler(x URLExtractor, v URLValidator, c *http.Client, l Logger, ms ...Middleware) http.Handler
- func SetFeedURLContext(ctx context.Context, u *url.URL, valid bool) context.Context
- type Logger
- type Middleware
- type URLExtractor
- type URLValidator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Convert ¶
Convert takes an XML feed from one buffer and turns it into a JSON feed in the other buffer.
Example ¶
package main import ( "bytes" "encoding/json" "fmt" "github.com/carlmjohnson/feed2json" ) func main() { var from, to bytes.Buffer from.WriteString(` <?xml version="1.0"?> <rss version="2.0"> <channel> <title>Liftoff News</title> <link>http://liftoff.msfc.nasa.gov/</link> <description>Liftoff to Space Exploration.</description> <language>en-us</language> <pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate> <lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</lastBuildDate> <docs>http://blogs.law.harvard.edu/tech/rss</docs> <generator>Weblog Editor 2.0</generator> <managingEditor>editor@example.com</managingEditor> <webMaster>webmaster@example.com</webMaster> <item> <title>Star City</title> <link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link> <description>How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's <a href="http://howe.iki.rssi.ru/GCTC/gctc_e.htm">Star City</a>.</description> <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate> <guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid> </item> </channel> </rss> `) if err := feed2json.Convert(&from, &to); err == nil { from.Reset() json.Indent(&from, to.Bytes(), "", " ") fmt.Println(from.String()) } }
Output: { "version": "https://jsonfeed.org/version/1", "title": "Liftoff News", "home_page_url": "http://liftoff.msfc.nasa.gov/", "description": "Liftoff to Space Exploration.", "author": {}, "items": [ { "id": "http://liftoff.msfc.nasa.gov/2003/06/03.html#item573", "url": "http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp", "title": "Star City", "content_html": "How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's <a href=\"http://howe.iki.rssi.ru/GCTC/gctc_e.htm\">Star City</a>.", "date_published": "2003-06-03T09:39:21Z" } ] }
func FeedURLFromContext ¶ added in v0.0.4
FeedURLFromContext allows middleware to intercept the URLs and their validity in Handler.
func Handler ¶
func Handler(x URLExtractor, v URLValidator, c *http.Client, l Logger, ms ...Middleware) http.Handler
Handler is an http.Handler that extracts and validates a URL for a request, sets the URL and its validity with SetFeedURLContext. Responses from Handler are then wrapped by the user provided middleware, if any. Finally, the innermost handler requests valid URLs with the provided http.Client by unwrapping FeedURLFromContext.
c if nil defaults to http.DefaultClient. l if nil defaults to log.Printf.
Types ¶
type Logger ¶
type Logger = func(format string, v ...interface{})
Logger is a user provided callback that matches the fmt/log.Printf calling conventions.
type Middleware ¶
Middleware wraps an http.Handler in a http.Handler.
type URLExtractor ¶
URLExtractor is a user provided callback that determines a URL for an XML feed based on a request
func ExtractURLFromParam ¶
func ExtractURLFromParam(name string) URLExtractor
ExtractURLFromParam is a URLExtractor that extracts a URL from the query param specified by name.
func StaticURLInjector ¶ added in v0.0.6
func StaticURLInjector(staticurl string) URLExtractor
StaticURLInjector is a URLExtractor that always injects the same URL, the provided string.
type URLValidator ¶
URLValidator is a user provided callback that determines whether the URL for an XML feed is valid for Handler.
func ValidateHost ¶
func ValidateHost(names ...string) URLValidator
ValidateHost is a URLValidator that approves of URLs where the hostname is in the names list.