{-# LANGUAGE OverloadedStrings #-} module JSONTypes where import Data.Aeson import Data.Aeson.Types import Data.Text as T import Data.Aeson.Lens (key, nth) import qualified Data.ByteString.Lazy.Internal as BL data JSONResponse d = JSONResponse { resp_status :: T.Text , resp_message :: T.Text , response_data :: d } deriving (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 { movies_count :: Int , movies_limit :: Int , page_number :: Int , movies :: [JSONMovie] } deriving (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 { movie_id :: Int , movie_url :: T.Text , imdb_code :: T.Text , movie_title :: T.Text , movie_year :: Int , movie_rating :: Double , movie_runtime :: Int , movie_genres :: [T.Text] , movie_summary :: T.Text , movie_language :: T.Text , movie_state :: T.Text , movie_torrents :: [JSONTorrent] } deriving (Show) instance FromJSON JSONMovie where parseJSON (Object v) = JSONMovie <$> v .: "id" <*> v .: "url" <*> v .: "imdb_code" <*> v .: "title" <*> 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 { torrent_url :: T.Text , torrent_hash :: T.Text , torrent_quality :: T.Text , torrent_type :: T.Text , torrent_seeds :: Int , torrent_peers :: Int , torrent_size :: T.Text , torrent_bytes :: Int , torrent_uploaded :: T.Text , torrent_uploaded_unix :: Int -- TODO: better date type? } deriving (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)