-
Notifications
You must be signed in to change notification settings - Fork 1
/
HaskellIntegration.hs
34 lines (25 loc) · 1.13 KB
/
HaskellIntegration.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
-- HTTP, XML, RSS, and parsing in Haskell
import Text.XML.Light.Lexer (XmlSource)
import Text.XML.Light.Input (parseXML)
import Text.XML.Light.Types (Content, Element, Element (elName), QName (QName), QName (qName))
import Text.XML.Light.Proc (onlyElems, elChildren, findElements)
import System.IO (openFile, hPutStrLn, hClose, IOMode (WriteMode), hGetLine, hFlush)
import Network.HTTP (simpleHTTP, getRequest, getResponseBody)
-- data Message a = Message {payload :: a}
type URL = String
readFeed :: URL -> IO String
readFeed url = simpleHTTP (getRequest url) >>= getResponseBody
parseFeed :: XmlSource a => a -> [String]
parseFeed = map show . getItems where
getItems xml = findElements (QName "item" Nothing Nothing) $ elemNamed "rss" $ parseXML xml
elemNamed :: String -> [Content] -> Element
elemNamed name = head . filter ((==name).qName.elName) . onlyElems
storeFeed :: String -> [String] -> IO ()
storeFeed f xs = do
outFile <- openFile f WriteMode
hPutStrLn outFile $ concatMap (++"\n") xs
hClose outFile
main = do
feed <- readFeed "http://lambda-the-ultimate.org/rss.xml"
let items = parseFeed feed
storeFeed "out" items