taggy-lens: Lenses for the taggy html/xml parser

[ bsd3, library, text, web ] [ Propose Tags ]

Lenses, folds, traversals and prisms for taggy.

This greatly simplifies your life when dealing with the ugly world of Real Life HTML. Here's an example. It lists the date, author and package name of all the recent package uploads entries from the hackage page of the same name. Note that it uses wreq to fetch the Hackage page.

module Main (main) where

import Control.Lens (to, only,(^?),ix, toListOf)
import Data.ByteString.Lazy (ByteString)
import Data.Text (Text)
import Data.Text.Encoding.Error (lenientDecode)
import Data.Text.Lazy.Encoding (decodeUtf8With)
import Network.HTTP.Client (Response)
import Network.Wreq (responseBody, get)
import Text.Taggy (Node)
import Text.Taggy.Lens (html, elements, children, contents,allNamed)

data Upload =
  Upload Text -- ^ date
         Text -- ^ author
         Text -- ^ package name
  deriving (Show)

table :: [Node] -> Maybe Upload
table row = do
  date    <- row ^? ix 0 . contents
  author  <- row ^? ix 1 . contents
  package <- row ^? ix 2 . elements . contents
  return $ Upload date author package

recentPackages :: Response ByteString -> [Maybe Upload]
recentPackages = toListOf
               $ responseBody . to (decodeUtf8With lenientDecode)
               . html . allNamed (only "tr") . children . to table

main :: IO ()
main = get "https://hackage.haskell.org/packages/recent" >>= print `fmap` recentPackages
Versions [faq] 0.1, 0.1.1, 0.1.2
Dependencies base (>=4.5 && <5), lens (>=4), taggy (>=0.1), text, unordered-containers [details]
License BSD-3-Clause
Copyright 2014 Alp Mestanogullari, Vikram Verma
Author Alp Mestanogullari, Vikram Verma
Maintainer alpmestan@gmail.com
Revised Revision 3 made by AlpMestanogullari at 2014-07-08T08:05:17Z
Category Text, Web
Home page http://github.com/alpmestan/taggy-lens
Uploaded by AlpMestanogullari at 2014-07-08T07:49:12Z
Distributions NixOS:0.1.2
Downloads 2891 total (5 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Successful builds reported [all 1 reports]

Modules

[Index]

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'.

Maintainer's Corner

For package maintainers and hackage trustees