{-# LANGUAGE OverloadedStrings #-} {-| Module : JSONTypes Description : Contains all the types used for data extracted from JSON responses Self explanatory -} module JSONTypes where import Data.Aeson import Data.Aeson.Types import Data.Maybe (fromMaybe) import qualified Data.Text as T data JSONResponse d = JSONResponse { respStatus :: T.Text , respMessage :: T.Text , respData :: Maybe d } deriving (Eq, Show) instance (FromJSON d) => FromJSON (JSONResponse d) where parseJSON (Object v) = JSONResponse <$> v .: "status" <*> v .: "status_message" <*> v .:? "data" parseJSON invalid = prependFailure "parsing JSONResponse failed, " (typeMismatch "Object" invalid) data JSONListMovies = JSONListMovies { moviesCount :: Int , moviesLimit :: Int , moviesPage :: Int , moviesMovies :: [JSONMovie] } deriving (Eq, Show) instance FromJSON JSONListMovies where parseJSON (Object v) = JSONListMovies <$> v .: "movie_count" <*> v .: "limit" <*> v .: "page_number" <*> v .:? "movies" .!= [] parseJSON invalid = prependFailure "parsing JSONListMovies failed, " (typeMismatch "Object" invalid) data JSONMovie = JSONMovie { movieId :: Int , movieUrl :: T.Text , imdbCode :: T.Text , movieTitle :: T.Text , movieTitleLong :: T.Text , movieYear :: Int , movieRating :: Double , movieRuntime :: Int , movieGenres :: [T.Text] , movieSummary :: T.Text , movieLanguage :: T.Text , movieState :: T.Text , movieTorrents :: [JSONTorrent] } deriving (Eq, Show) instance FromJSON JSONMovie where parseJSON (Object v) = JSONMovie <$> v .: "id" <*> v .: "url" <*> v .: "imdb_code" <*> v .: "title" <*> v .: "title_long" <*> v .: "year" <*> v .: "rating" <*> v .: "runtime" <*> v .: "genres" <*> v .: "summary" <*> v .: "language" <*> v .: "state" <*> v .: "torrents" parseJSON invalid = prependFailure "parsing JSONMovie failed, " (typeMismatch "Object" invalid) data JSONTorrent = JSONTorrent { torrentUrl :: T.Text , torrentHash :: T.Text , torrentQuality :: T.Text , torrentType :: T.Text , torrentSeeds :: Int , torrentPeers :: Int , torrentSize :: T.Text , torrentBytes :: Int , torrentUploaded :: T.Text , torrentUploadedUnix :: Int -- TODO: better date type? } deriving (Eq, Show) instance FromJSON JSONTorrent where parseJSON (Object v) = JSONTorrent <$> v .: "url" <*> v .: "hash" <*> v .: "quality" <*> v .: "type" <*> v .: "seeds" <*> v .: "peers" <*> v .: "size" <*> v .: "size_bytes" <*> v .: "date_uploaded" <*> v .: "date_uploaded_unix" parseJSON invalid = prependFailure "parsing JSONTorrent failed, " (typeMismatch "Object" invalid)