- get_opts =
- case cookie_jar of
- Nothing -> []
- Just cookies -> [ CurlCookieJar cookies ]
-
- curl_opts = default_curl_opts ++ get_opts
-
-
-log_in :: FilePath -> String -> String -> IO Bool
-log_in cookie_jar username password =
- withCurlDo $ do
- -- Create a curl instance.
- curl <- initialize
-
- -- Perform the request, and get back a CurlResponse object.
- -- The cast is needed to specify how we would like our headers
- -- and body returned (Strings).
- resp <- do_curl_ curl login_url curl_opts :: IO CurlResponse
-
- -- Pull out the response code as a CurlCode.
- let code = respCurlCode resp
-
- case code of
- CurlOK -> return True
- error_code -> do
- hPutStrLn stderr ("HTTP Error: " ++ (show error_code))
- -- If an error occurred, we want to dump as much information as
- -- possible. If this becomes a problem, we can use respGetInfo to
- -- query the response object for more information
- return False
+ post_submit :: (C.ByteString, C.ByteString)
+ post_submit = (submit_field, C.pack "Log+In")
+
+ post_username :: (C.ByteString, C.ByteString)
+ post_username = (username_field, C.pack username)
+
+ post_password :: (C.ByteString, C.ByteString)
+ post_password = (password_field, C.pack password)
+
+ post_data :: [(C.ByteString, C.ByteString)]
+ post_data = [post_username, post_password, post_submit]
+
+
+
+-- | Save the image at 'url'. Saves to a temporary file, and
+-- returns the path to that file if successful. Otherwise,
+-- returns 'Nothing'.
+--
+-- We need to be able to parse the filename out of the URL
+-- so that when we stick our image in the document, the reader
+-- knows that type (jpg, png, etc.) it is.
+save_image :: URL -> IO (Maybe FilePath)
+save_image url = do
+ it_exists <- doesFileExist url
+ if it_exists then do
+ -- It's local, just use it.
+ return $ Just url
+ else do
+ let fn = filename url
+ case fn of
+ Nothing -> return Nothing
+ Just file -> do
+ temp_dir <- getTemporaryDirectory
+ (out_path, out_handle) <- openBinaryTempFile temp_dir file
+ -- We don't need to be logged in to get the images, so use an
+ -- empty cookie jar.
+ result <- get_page C.cj_empty url
+ case result of
+ Left err -> do
+ hPutStrLn stderr $ "Failed to retrieve image. " ++
+ "Server response:\n" ++ (show err)
+ return Nothing
+ Right bs -> do
+ L.hPut out_handle bs
+ return $ Just out_path
+
+
+
+
+-- | Map absolute image URLs to local system file paths where the
+-- image referenced by the URL is stored.
+type ImageMap = Map.Map URL FilePath
+
+download_image_urls :: [URL] -> IO ImageMap
+download_image_urls image_urls = do
+ files <- parallel $ map save_image image_urls
+ let pairs = zip image_urls files
+ return $ foldl my_insert empty_map pairs