module Network.URL.Archiver where
import Data.List (isPrefixOf)
import Control.Monad (when)
import Data.Maybe (fromJust)
import Network.Browser (browse, formToRequest, request, Form(..))
import Network.HTTP (getRequest, rspBody, simpleHTTP, RequestMethod(POST))
import Network.URI (isURI, parseURI, uriPath)
checkArchive :: String
-> String
-> IO ()
checkArchive email url = when (isURI url) (webciteArchive email url >> alexaArchive url)
webciteArchive :: String -> String -> IO ()
webciteArchive email url = when (not $ "http://www.webcitation.org" `isPrefixOf` url) $
void $ openURL ("http://www.webcitation.org/archive?url=" ++ url ++ "&email=" ++ email)
where openURL = simpleHTTP . getRequest
void = (>> return ())
alexaArchive :: String -> IO ()
alexaArchive url = when (not $ "http://www.archive.org" `isPrefixOf` url) $
do let archiveform = Form POST
(fromJust $ parseURI "http://www.alexa.com/help/crawlrequest")
[("url", url), ("submit", "")]
(uri, resp) <- browse $ request $ formToRequest archiveform
when (uriPath uri /= "/help/crawlthanks") $
print $ "Request failed! Alexa changed webpages? Response:" ++ rspBody resp