diff options
author | Dragos Boca <dragos.boca@oracle.com> | 2015-08-25 13:37:23 +0300 |
---|---|---|
committer | Dragos Boca <dragos.boca@oracle.com> | 2015-08-25 13:37:23 +0300 |
commit | d0e391fb165ab23bb08dc02871747e0131c1e723 (patch) | |
tree | 3fd2ea797a518cd3f0aa5f3c218fb11977c35da4 /src/Plugins | |
parent | 7000477c014ed6d40507f4472f098736b270840b (diff) | |
download | xmobar-d0e391fb165ab23bb08dc02871747e0131c1e723.tar.gz xmobar-d0e391fb165ab23bb08dc02871747e0131c1e723.tar.bz2 |
define flag 'with_conduit'
Diffstat (limited to 'src/Plugins')
-rw-r--r-- | src/Plugins/Monitors/Weather.hs | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/src/Plugins/Monitors/Weather.hs b/src/Plugins/Monitors/Weather.hs index 5f5fb6f..d458279 100644 --- a/src/Plugins/Monitors/Weather.hs +++ b/src/Plugins/Monitors/Weather.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} ----------------------------------------------------------------------------- -- | -- Module : Plugins.Monitors.Weather @@ -18,12 +19,14 @@ import Plugins.Monitors.Common import qualified Control.Exception as CE +#ifdef HTTP_CONDUIT import Network.HTTP.Conduit import Network.HTTP.Types.Status import Network.HTTP.Types.Method - ---import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy.Char8 as B +#else +import Network.HTTP +#endif import Text.ParserCombinators.Parsec @@ -177,7 +180,8 @@ defUrl = "http://weather.noaa.gov/pub/data/observations/metar/decoded/" stationUrl :: String -> String stationUrl station = defUrl ++ station ++ ".TXT" -getData:: String -> IO String +getData :: String -> IO String +#ifdef HTTP_CONDUIT getData station = CE.catch (do manager <- newManager tlsManagerSettings request <- parseUrl $ stationUrl station @@ -186,6 +190,13 @@ getData station = CE.catch (do ) errHandler where errHandler :: CE.SomeException -> IO String errHandler _ = return "<Could not retrieve data>" +#else +getData station = do + let request = getRequest (stationUrl station) + CE.catch (simpleHTTP request >>= getResponseBody) errHandler + where errHandler :: CE.IOException -> IO String + errHandler _ = return "<Could not retrieve data>" +#endif formatWeather :: [WeatherInfo] -> Monitor String formatWeather [WI st ss y m d h (WindInfo wc wa wm wk) v sk tC tF dC dF r p] = @@ -201,6 +212,7 @@ runWeather str = formatWeather i weatherReady :: [String] -> Monitor Bool +#ifdef HTTP_CONDUIT weatherReady str = do initRequest <- parseUrl $ stationUrl $ head str let request = initRequest{method = methodHead} @@ -214,3 +226,21 @@ weatherReady str = do | statusIsServerError status = False | statusIsClientError status = False | otherwise = True +#else +weatherReady str = do + let station = head str + request = headRequest (stationUrl station) + io $ CE.catch (simpleHTTP request >>= checkResult) errHandler + where errHandler :: CE.IOException -> IO Bool + errHandler _ = return False + checkResult result = + case result of + Left _ -> return False + Right response -> + case rspCode response of + -- Permission or network errors are failures; anything + -- else is recoverable. + (4, _, _) -> return False + (5, _, _) -> return False + (_, _, _) -> return True +#endif |