From d0e391fb165ab23bb08dc02871747e0131c1e723 Mon Sep 17 00:00:00 2001 From: Dragos Boca Date: Tue, 25 Aug 2015 13:37:23 +0300 Subject: define flag 'with_conduit' --- src/Plugins/Monitors/Weather.hs | 36 +++++++++++++++++++++++++++++++--- xmobar.cabal | 43 ++++++++++++++++++++++++----------------- 2 files changed, 58 insertions(+), 21 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 "" +#else +getData station = do + let request = getRequest (stationUrl station) + CE.catch (simpleHTTP request >>= getResponseBody) errHandler + where errHandler :: CE.IOException -> IO String + errHandler _ = return "" +#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 diff --git a/xmobar.cabal b/xmobar.cabal index 9be6452..f8e8057 100644 --- a/xmobar.cabal +++ b/xmobar.cabal @@ -78,6 +78,10 @@ flag with_uvmeter description: UVMeter only useful to australians. default: False +flag with_conduit + description: Use http-conduits for getting weather data + default: False + executable xmobar hs-source-dirs: src main-is: Main.hs @@ -105,24 +109,27 @@ executable xmobar extra-libraries: Xrandr Xrender build-depends: - base == 4.*, - containers, - regex-compat, - process, - old-locale, - bytestring, - directory, - unix, - time, - filepath, - transformers, - http-conduit, - http-types, - X11 >= 1.6.1, - mtl >= 2.1 && < 2.3, - parsec == 3.1.*, - HTTP >= 4000.2.4, - stm >= 2.3 && < 2.5 + base == 4.*, + containers, + regex-compat, + process, + old-locale, + bytestring, + directory, + unix, + time, + filepath, + transformers, + X11 >= 1.6.1, + mtl >= 2.1 && < 2.3, + parsec == 3.1.*, + HTTP >= 4000.2.4, + stm >= 2.3 && < 2.5 + + if flag(with_conduit) + -- use http-conduit instead of simple-http + build-depends: http-conduit, http-types + cpp-options: -DHTTP_CONDUIT if flag(with_threaded) -- -threaded is a workaround for 100% CPU busy loop -- cgit v1.2.3