feed: Interfacing with RSS (v 0.9x, 2.x, 1.0) + Atom feeds.

[ bsd3, library, text ] [ Propose Tags ] [ Report a vulnerability ]

Interfacing with RSS (v 0.9x, 2.x, 1.0) + Atom feeds.

To help working with the multiple feed formats we've ended up with, this set of modules provides parsers, pretty printers and some utility code for querying and just generally working with a concrete representation of feeds in Haskell.

See here for an example of how to create an Atom feed: https://github.com/haskell-party/feed/blob/master/tests/Example/CreateAtom.hs

For basic reading and editing of feeds, consult the documentation of the Text.Feed.* hierarchy.


[Skip to Readme]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Versions [RSS] 0.3.1, 0.3.2, 0.3.2.1, 0.3.4, 0.3.5, 0.3.6, 0.3.7, 0.3.8, 0.3.9, 0.3.9.1, 0.3.9.2, 0.3.9.3, 0.3.9.4, 0.3.9.5, 0.3.9.6, 0.3.9.7, 0.3.10.0, 0.3.10.1, 0.3.10.2, 0.3.10.3, 0.3.10.4, 0.3.11.0, 0.3.11.1, 0.3.12.0, 1.0.0.0, 1.0.1.0, 1.1.0.0, 1.2.0.0, 1.2.0.1, 1.3.0.0, 1.3.0.1, 1.3.2.0, 1.3.2.1
Change log CHANGELOG.md
Dependencies base (>=4 && <4.20), base-compat (>=0.9 && <0.14), bytestring (>=0.9 && <0.13), old-locale (>=1.0 && <1.1), old-time (>=1 && <1.2), safe (>=0.3 && <0.4), text (<1.3 || >=2.0 && <2.2), time (<1.13), time-locale-compat (>=0.1 && <0.2), utf8-string (<1.1), xml-conduit (>=1.3 && <1.10), xml-types (>=0.3.6 && <0.4) [details]
Tested with ghc ==7.6.3, ghc ==7.8.4, ghc ==7.10.3, ghc ==8.0.2, ghc ==8.2.2, ghc ==8.4.4, ghc ==8.6.5, ghc ==8.8.4, ghc ==8.10.7, ghc ==9.0.2, ghc ==9.2.8, ghc ==9.4.8, ghc ==9.6.4, ghc ==9.8.1
License BSD-3-Clause
Author Sigbjorn Finne <sof@forkIO.com>
Maintainer Adam Bergmark <adam@bergmark.nl>
Revised Revision 4 made by Bodigrim at 2024-02-27T19:34:31Z
Category Text
Home page https://github.com/haskell-party/feed
Bug tracker https://github.com/haskell-party/feed/issues
Source repo head: git clone https://github.com/haskell-party/feed.git
Uploaded by AdamBergmark at 2022-03-19T23:15:48Z
Distributions Arch:1.3.2.1, Debian:1.3.0.1, Fedora:1.3.2.1, FreeBSD:0.3.10.0, LTSHaskell:1.3.2.1, NixOS:1.3.2.1, openSUSE:1.3.2.1
Reverse Dependencies 24 direct, 26 indirect [details]
Downloads 47730 total (111 in the last 30 days)
Rating 2.25 (votes: 2) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2022-03-20 [all 1 reports]

Readme for feed-1.3.2.1

[back to package description]

Feed

feed Build Status

Goal

Interfacing with RSS (v 0.9x, 2.x, 1.0) + Atom feeds.

  • Parsers
  • Constructors
  • Rendering
  • Querying

To help working with the multiple feed formats we've ended up with this set of modules providing parsers, printers and some utility code for querying and just generally working with a concrete representation of feeds in Haskell.

For basic reading and editing of feeds, consult the documentation of the Text.Feed.* hierarchy.

Usage

Building an Atom feed is similar to building an RSS feed, but we'll arbitrarily pick Atom here:

We'd like to generate the XML for a minimal working example. Constructing our base Feed can use the smart constructor called nullFeed:

This is a pattern the library maintains for smart constructors. If you want the minimum viable 'X', use the 'nullX' constructor.

{-# LANGUAGE OverloadedStrings #-}

module Main where

import Prelude.Compat hiding (take)
import Data.Maybe
import Data.Text
import Data.XML.Types as XML
import qualified Data.Text.Lazy as Lazy
import Text.Feed.Types

import Text.XML (def, rsPretty)
import qualified Text.Atom.Feed as Atom
import qualified Text.Feed.Export as Export (textFeedWith)

myFeed :: Atom.Feed
myFeed = Atom.nullFeed
    "http://example.com/atom.xml"
    (Atom.TextString "Example Website")
    "2017-08-01"

Now we can export the feed to Text.

renderFeed :: Atom.Feed -> Lazy.Text
renderFeed = fromJust . Export.textFeedWith def{rsPretty = True} . AtomFeed

We can now render our feed:

-- |
-- $setup
-- >>> import qualified Data.Text.Lazy.IO as Lazy
--
-- >>> Lazy.putStr $ renderFeed myFeed
-- <?xml version="1.0" encoding="UTF-8"?>
-- <feed xmlns="http://www.w3.org/2005/Atom">
--     <title type="text">
--         Example Website
--     </title>
--     <id>
--         http://example.com/atom.xml
--     </id>
--     <updated>
--         2017-08-01
--     </updated>
-- </feed>

The TextContent sum type allows us to specify which type of text we're providing.

data TextContent
  = TextString Text
  | HTMLString Text
  | XHTMLString XML.Element
  deriving (Show)

A feed isn't very useful without some content though, so we'll need to build up an Entry.

data Post
  = Post
  { _postedOn :: Text
  , _url :: Text
  , _content :: Text
  }

examplePosts :: [Post]
examplePosts =
  [ Post "2000-02-02T18:30:00Z" "http://example.com/2" "Bar."
  , Post "2000-01-01T18:30:00Z" "http://example.com/1" "Foo."
  ]

Our Post data type will need to be converted into an Entry in order to use it in the top level Feed. The required fields for an entry are an url "id" from which an entry's presence can be validated, a title for the entry, and a posting date. In this example we'll also add authors, link, and the entries actual content, since we have all of this available in the Post provided.

toEntry :: Post -> Atom.Entry
toEntry (Post date url content) =
  (Atom.nullEntry
     url -- The ID field. Must be a link to validate.
     (Atom.TextString (take 20 content)) -- Title
     date)
  { Atom.entryAuthors = [Atom.nullPerson {Atom.personName = "J. Smith"}]
  , Atom.entryLinks = [Atom.nullLink url]
  , Atom.entryContent = Just (Atom.HTMLContent content)
  }

From the base feed we created earlier, we can add further details (Link and Entry content) as well as map our toEntry function over the posts we'd like to include in the feed.

feed :: Atom.Feed
feed =
  myFeed { Atom.feedEntries = fmap toEntry examplePosts
         , Atom.feedLinks = [Atom.nullLink "http://example.com/"]
         }
-- |
-- >>> Lazy.putStr $ renderFeed feed
-- <?xml version="1.0" encoding="UTF-8"?>
-- <feed xmlns="http://www.w3.org/2005/Atom">
--     <title type="text">
--         Example Website
--     </title>
--     <id>
--         http://example.com/atom.xml
--     </id>
--     <updated>
--         2017-08-01
--     </updated>
--     <link href="http://example.com/"/>
--     <entry>
--         <id>
--             http://example.com/2
--         </id>
--         <title type="text">
--             Bar.
--         </title>
--         <updated>
--             2000-02-02T18:30:00Z
--         </updated>
--         <author>
--             <name>
--                 J. Smith
--             </name>
--         </author>
--         <content type="html">
--             Bar.
--         </content>
--         <link href="http://example.com/2"/>
--     </entry>
--     <entry>
--         <id>
--             http://example.com/1
--         </id>
--         <title type="text">
--             Foo.
--         </title>
--         <updated>
--             2000-01-01T18:30:00Z
--         </updated>
--         <author>
--             <name>
--                 J. Smith
--             </name>
--         </author>
--         <content type="html">
--             Foo.
--         </content>
--         <link href="http://example.com/1"/>
--     </entry>
-- </feed>

See here for this content as an uninterrupted running example.

-- Dummy main needed to compile this file with markdown-unlit
main :: IO ()
main = return ()