This repository has been archived by the owner on Feb 28, 2020. It is now read-only.
forked from fosskers/aura
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInternet.hs
56 lines (42 loc) · 1.73 KB
/
Internet.hs
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
-- A library that serves as an abstraction for dealing with the internet.
{-
Copyright 2012, 2013, 2014 Colin Woodbury <[email protected]>
This file is part of Aura.
Aura is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Aura is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Aura. If not, see <http://www.gnu.org/licenses/>.
-}
module Internet
( unpack
, urlContents
, urlEncodeVars
, saveUrlContents ) where
import qualified Data.ByteString.Lazy as L
import Data.ByteString.Lazy.Char8 (pack, unpack)
import Control.Exception (catch)
import System.FilePath (splitFileName, (</>))
import Network.HTTP (urlEncodeVars)
import System.IO (hClose, openFile, IOMode(WriteMode))
import Network.HTTP.Conduit
---
urlContents :: String -> IO L.ByteString
urlContents url = do
req <- parseUrl url
let req2 = req { responseTimeout = Nothing}
catch (fmap responseBody . withManager . httpLbs $ req2) handleException
handleException :: HttpException -> IO L.ByteString
handleException _ = return (pack "")
saveUrlContents :: FilePath -> String -> IO FilePath
saveUrlContents fpath url = do
handle <- openFile filePath WriteMode
content <- urlContents url
L.hPutStr handle content >> hClose handle >> return filePath
where filePath = fpath </> file
(_,file) = splitFileName url