Skip to content

Instantly share code, notes, and snippets.

@abhillman
Last active August 29, 2015 14:02
Show Gist options
  • Save abhillman/1637da51f5c60dd9c7ff to your computer and use it in GitHub Desktop.
Save abhillman/1637da51f5c60dd9c7ff to your computer and use it in GitHub Desktop.
{-
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