kino/src/JSONTypes.hs

111 lines
2.7 KiB
Haskell
Raw Normal View History

2021-09-05 19:30:29 +02:00
{-# LANGUAGE OverloadedStrings #-}
{-|
Module : JSONTypes
Description : Contains all the types used for data extracted from JSON responses
Self explanatory
-}
2021-09-05 19:30:29 +02:00
module JSONTypes where
import Data.Aeson
import Data.Aeson.Types
2021-09-06 21:02:39 +02:00
import qualified Data.Text as T
2021-09-05 19:30:29 +02:00
data JSONResponse d = JSONResponse
{ respStatus :: T.Text
, respMessage :: T.Text
, respData :: Maybe d
} deriving (Eq, Show)
2021-09-05 19:30:29 +02:00
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
, pagenumber :: Int
2021-09-05 19:30:29 +02:00
, movies :: [JSONMovie]
} deriving (Eq, Show)
2021-09-05 19:30:29 +02:00
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)
2021-09-05 19:30:29 +02:00
instance FromJSON JSONMovie where
parseJSON (Object v) = JSONMovie
<$> v .: "id"
<*> v .: "url"
<*> v .: "imdb_code"
<*> v .: "title"
2021-09-06 21:02:39 +02:00
<*> v .: "title_long"
2021-09-05 19:30:29 +02:00
<*> 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)
2021-09-05 19:30:29 +02:00
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)