kino/src/JSONTypes.hs

104 lines
2.6 KiB
Haskell
Raw Normal View History

2021-09-05 19:30:29 +02:00
{-# LANGUAGE OverloadedStrings #-}
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
{ resp_status :: T.Text
, resp_message :: T.Text
, response_data :: Maybe d
2021-09-05 19:30:29 +02:00
} 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
2021-09-06 21:02:39 +02:00
, movie_title_long :: T.Text
2021-09-05 19:30:29 +02:00
, 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"
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
{ 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)