1 {-# LANGUAGE DoAndIfThenElse #-}
6 import qualified Data.ByteString as B (hPut)
19 import Network.Curl.Download (openURI)
20 import System.Directory (doesFileExist, getTemporaryDirectory)
21 import System.IO (hClose, hPutStrLn, stderr, stdout)
22 import System.IO.Temp (openBinaryTempFile, openTempFile)
24 import LWN.URI (filename)
26 login_url :: URLString
27 login_url = "https://lwn.net/login"
29 username_field :: String
30 username_field = "Username"
32 password_field :: String
33 password_field = "Password"
35 submit_field :: String
36 submit_field = "submit"
39 default_curl_opts :: [CurlOption]
41 [ -- The Global cache is not thread-friendly.
42 CurlDNSUseGlobalCache False,
44 -- And we don't want to use a DNS cache anyway.
45 CurlDNSCacheTimeout 0,
48 CurlFollowLocation True,
50 -- Give it a little time...
57 make_cookie_jar :: IO FilePath
59 temp_dir <- getTemporaryDirectory
60 let file_name_template = "lwn-epub-cookies.txt"
61 (out_path, out_handle) <- openTempFile temp_dir file_name_template
62 hClose out_handle -- We just want to create it for now.
65 get_page :: Maybe FilePath -> URLString -> IO (Maybe String)
66 get_page cookie_file url =
68 hPutStrLn stdout ("Getting page: " ++ url ++ "...")
70 -- Create a curl instance.
73 -- Perform the request, and get back a CurlResponse object.
74 -- The cast is needed to specify how we would like our headers
75 -- and body returned (Strings).
76 putStrLn "Curl options:"
79 resp <- do_curl_ curl url curl_opts :: IO CurlResponse
81 -- Pull out the response code as a CurlCode.
82 let code = respCurlCode resp
85 CurlOK -> return $ Just (respBody resp)
87 hPutStrLn stderr ("HTTP Error: " ++ (show error_code))
88 -- If an error occurred, we want to dump as much information as
89 -- possible. If this becomes a problem, we can use respGetInfo to
90 -- query the response object for more information
96 Just cookies -> [ CurlCookieFile cookies ]
98 curl_opts = default_curl_opts ++ get_opts
101 log_in :: FilePath -> String -> String -> IO Bool
102 log_in cookie_jar username password =
104 hPutStrLn stdout ("Logging " ++ username ++ " in...")
106 -- Create a curl instance.
109 -- Perform the request, and get back a CurlResponse object.
110 -- The cast is needed to specify how we would like our headers
111 -- and body returned (Strings).
112 resp <- do_curl_ curl login_url curl_opts :: IO CurlResponse
114 -- Pull out the response code as a CurlCode.
115 let code = respCurlCode resp
118 CurlOK -> return True
120 hPutStrLn stderr ("HTTP Error: " ++ (show error_code))
121 -- If an error occurred, we want to dump as much information as
122 -- possible. If this becomes a problem, we can use respGetInfo to
123 -- query the response object for more information
126 post_submit :: String
127 post_submit = submit_field ++ "=Log+In"
129 post_username :: String
130 post_username = username_field ++ "=" ++ username
132 post_password :: String
133 post_password = password_field ++ "=" ++ password
135 post_data :: [String]
136 post_data = [post_username, post_password, post_submit]
138 post_opts :: [CurlOption]
140 [ CurlCookieSession True,
141 CurlCookieJar cookie_jar,
143 CurlPostFields post_data ]
145 curl_opts :: [CurlOption]
146 curl_opts = default_curl_opts ++ post_opts
149 -- | Save the image at 'url'. Saves to a temporary file, and
150 -- returns the path to that file if successful. Otherwise,
151 -- returns 'Nothing'.
153 -- We need to be able to parse the filename out of the URL
154 -- so that when we stick our image in the document, the reader
155 -- knows that type (jpg, png, etc.) it is.
156 save_image :: URLString -> IO (Maybe FilePath)
158 it_exists <- doesFileExist url
160 -- It's local, just use it.
163 let fn = filename url
165 Nothing -> return Nothing
167 temp_dir <- getTemporaryDirectory
168 (out_path, out_handle) <- openBinaryTempFile temp_dir file
169 result <- openURI url
172 hPutStrLn stderr ("HTTP Error: " ++ err)
176 return $ Just out_path