Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ebc97e8699
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 34 lines (31 sloc) 1.263 kb
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 34
module UrlExtender (expandUrlEasy) where

import Network.HTTP
import System.IO
import Data.Maybe
import Network.URI
import Text.XML.Light

expandUrlEasy :: String -> IO (String)
expandUrlEasy url =
        do resp <- expandUrl url
           case resp of
                Left x -> return $ "Error connecting: " ++ show x
                Right x -> return $ url ++ " -> " ++ longurl
                        where
                                xml = fromJust $ parseXMLDoc x
                                items = findElements (QName "long-url" Nothing Nothing) xml
                                longurl = (strContent (head items))

expandUrl :: String -> IO (Either String String)
expandUrl url =
    do resp <- simpleHTTP request
       case resp of
            Left x -> return $ Left ("Error connecting: " ++ show x)
            Right r ->
                  case rspCode r of
                    (2,_,_) -> return $ Right (rspBody r)
                    _ -> return $ Left (show r)
    where
        request = Request {rqURI = uri,
                            rqMethod = GET,
                            rqHeaders = [],
                            rqBody = ""}
        uri = fromJust $ parseURI $ "http://api.longurl.org/v2/expand?url=" ++ (urlEncode url)
Something went wrong with that request. Please try again.