Last active
August 29, 2015 14:02
-
-
Save abhillman/1637da51f5c60dd9c7ff to your computer and use it in GitHub Desktop.
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
{- | |
Module: urban.hs | |
Description: Grabs example sentences for a random term from Urban Dictionary | |
Copyright: June 18, 2014 | |
License: MIT | |
Maintainer: [email protected] | |
Stability: experimental | |
Portability: portable | |
Requests and follows the 302 redirect at http://www.urbandictionary.com/random.php, | |
thereby requesting a random Urban Dictionary Page. Parses the HTML using HXT and | |
searches for HTML elements with the class name "example". The text for each example | |
element is parsed, then lstrip and rstrip'ed to remove newline characters. Finally, | |
a list containing those example texts are returned. | |
-} | |
import Network.Browser | |
import Network.HTTP | |
import Text.XML.HXT.Core | |
import Data.List | |
import Data.String.Utils -- from MissingH | |
main :: IO [String] | |
main = do | |
(_, rsp) <- Network.Browser.browse $ do | |
setAllowRedirects True | |
request $ getRequest "http://www.urbandictionary.com/random.php" | |
let htmlString = rspBody rsp | |
let htmlDoc = readString [withParseHTML yes, withWarnings no] htmlString | |
examples <- runX $ htmlDoc //> hasAttrValue "class" (isInfixOf "example") >>> getChildren >>> getText | |
return $ map (lstrip.rstrip) examples |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment