add temperature extraction

This commit is contained in:
Szymon Szczubkowski 2024-05-26 18:51:13 +02:00
parent d2fb17df88
commit b96ff0bfb1
2 changed files with 18 additions and 7 deletions

View File

@ -64,3 +64,18 @@ weatherDecode jsonBody =
case decode jsonBody :: Maybe WeatherResponse of case decode jsonBody :: Maybe WeatherResponse of
Just decoded -> return decoded Just decoded -> return decoded
Nothing -> error "Invalid JSON" Nothing -> error "Invalid JSON"
getTemperature :: WeatherResponse -> Float
getTemperature (WeatherResponse _ Nothing Nothing) = error "No data provided!"
getTemperature (WeatherResponse _ (Just curr) Nothing) = getTemperatureFromCurrent curr
getTemperature (WeatherResponse _ Nothing (Just fore)) = getTemperatureFromForecastDay $ getForecastDayFromForecast fore
getTemperature (WeatherResponse _ (Just _) (Just fore)) = getTemperatureFromForecastDay $ getForecastDayFromForecast fore
getForecastDayFromForecast :: Forecast -> ForecastDay
getForecastDayFromForecast (Forecast forecasts) = head forecasts
getTemperatureFromCurrent :: Current -> Float
getTemperatureFromCurrent (Current temp) = temp
getTemperatureFromForecastDay :: ForecastDay -> Float
getTemperatureFromForecastDay (ForecastDay (Day temp)) = temp

View File

@ -7,7 +7,6 @@ import Data.Time as T
import Plot import Plot
import qualified DataTypes as DT import qualified DataTypes as DT
data WhichDay = Yesterday | Today | Tomorrow data WhichDay = Yesterday | Today | Tomorrow
deriving (Show, Eq) deriving (Show, Eq)
@ -33,25 +32,22 @@ main = do
let yesterdayDate = formatDate $ addDays (-1) currentDate let yesterdayDate = formatDate $ addDays (-1) currentDate
let yesterdayRequest = apiRequestBuilder apiKey Yesterday city yesterdayDate let yesterdayRequest = apiRequestBuilder apiKey Yesterday city yesterdayDate
yesterdayResponse <- httpLBS yesterdayRequest yesterdayResponse <- httpLBS yesterdayRequest
print $ getResponseStatusCode yesterdayResponse
yesterdayDecoded <- DT.weatherDecode $ getResponseBody yesterdayResponse yesterdayDecoded <- DT.weatherDecode $ getResponseBody yesterdayResponse
print yesterdayDecoded print ("Yesterday's temperature: ", DT.getTemperature yesterdayDecoded)
--request for today's weather --request for today's weather
let todayDate = formatDate currentDate let todayDate = formatDate currentDate
let todayRequest = apiRequestBuilder apiKey Today city todayDate let todayRequest = apiRequestBuilder apiKey Today city todayDate
todayResponse <- httpLBS todayRequest todayResponse <- httpLBS todayRequest
print $ getResponseStatusCode todayResponse
todayDecoded <- DT.weatherDecode $ getResponseBody todayResponse todayDecoded <- DT.weatherDecode $ getResponseBody todayResponse
print todayDecoded print ("Today's temperature: ", DT.getTemperature todayDecoded)
--request for tomorrow's weather --request for tomorrow's weather
let tomorrowDate = formatDate $ addDays 1 currentDate let tomorrowDate = formatDate $ addDays 1 currentDate
let tomorrowRequest = apiRequestBuilder apiKey Tomorrow city tomorrowDate let tomorrowRequest = apiRequestBuilder apiKey Tomorrow city tomorrowDate
tomorrowResponse <- httpLBS tomorrowRequest tomorrowResponse <- httpLBS tomorrowRequest
print $ getResponseStatusCode tomorrowResponse
tomorrowDecoded <- DT.weatherDecode $ getResponseBody tomorrowResponse tomorrowDecoded <- DT.weatherDecode $ getResponseBody tomorrowResponse
print tomorrowDecoded print ("Tomorrow's temperature: ", DT.getTemperature tomorrowDecoded)
--apiKey is stored in an env variable, not something that should be pushed onto git --apiKey is stored in an env variable, not something that should be pushed onto git