Browse Source

Parse more fields from a movie record

tags/v0.2.2.0
Peter J. Jones 6 years ago
parent
commit
22302e889c
3 changed files with 65 additions and 10 deletions
  1. 2
    0
      Network/API/TheMovieDB.hs
  2. 60
    10
      Network/API/TheMovieDB/Types.hs
  3. 3
    0
      themoviedb.cabal

+ 2
- 0
Network/API/TheMovieDB.hs View File

@@ -12,6 +12,8 @@ module Network.API.TheMovieDB
( APIKey
, APIError
, SearchTerm
, ReleaseDate(..)
, Genre(..)
, Movie(..)
, searchErr
, search

+ 60
- 10
Network/API/TheMovieDB/Types.hs View File

@@ -13,25 +13,75 @@ contained in the LICENSE file.
module Network.API.TheMovieDB.Types
( APIKey
, APIError(..)
, ReleaseDate(..)
, Genre(..)
, Movie(..)
) where

-- Imports.
import Data.Aeson
import Data.Aeson.Types (typeMismatch)
import Control.Applicative
import System.Locale
import Data.Time
import Data.Text (unpack)

-- | Type for the API Key issued by TheMovieDB.
type APIKey = String

data APIError = NetworkError String
| ParseError String
deriving (Eq, Show)
-- | Possible errors returned by the API.
data APIError
= NetworkError String -- ^ Network or HTTP error.
| ParseError String -- ^ Invalid or error response from the API.
deriving (Eq, Show)

data Movie = Movie
{ movieID :: Int
, movieTitle :: String
} deriving (Eq, Show)
-- | Type wrapper for Day to parse a movie's release date.
newtype ReleaseDate =
ReleaseDate {releaseDate :: Day}
deriving (Eq, Show)

instance FromJSON ReleaseDate where
parseJSON (String t) =
case parseTime defaultTimeLocale "%Y-%m-%d" (unpack t) of
Just d -> pure $ ReleaseDate d
_ -> fail "could not parse release_date"
parseJSON v = typeMismatch "ReleaseDate" v

-- | Metadata for a genre.
data Genre =
Genre
{ genreID :: Int -- ^ TheMovieDB unique ID.
, genreName :: String -- ^ The name of the genre.
} deriving (Eq, Show)

instance FromJSON Genre where
parseJSON (Object v) = Genre <$> v .: "id" <*> v .: "name"
parseJSON v = typeMismatch "Genre" v

-- | Metadata for a movie.
data Movie =
Movie
{ movieID :: Int -- ^ TheMovieDB unique ID.
, movieTitle :: String -- ^ The name/title of the movie.
, movieOverview :: String -- ^ Short plot summary.
, movieGenres :: [Genre] -- ^ List of genre names.
, moviePopularity :: Double -- ^ Popularity ranking.
, moviePosterPath :: String -- ^ FIXME:
, movieReleaseDate :: ReleaseDate -- ^ Movie release date.
} deriving (Eq, Show)

instance FromJSON Movie where
parseJSON (Object v) =
Movie <$> v .: "id"
<*> v .: "original_title"
parseJSON (Object v) = do
genresMaybe <- v .:? "genres"
genres <- case genresMaybe of
Just a -> parseJSON a
_ -> return []
Movie <$> v .: "id"
<*> v .: "title"
<*> v .:? "overview" .!= ""
<*> return genres
<*> v .:? "popularity" .!= 0.0
<*> v .:? "poster_path" .!= ""
<*> v .:? "release_date" .!= defaultDate
where defaultDate = ReleaseDate $ ModifiedJulianDay 0
parseJSON _ = empty

+ 3
- 0
themoviedb.cabal View File

@@ -23,9 +23,12 @@ library
Network.API.TheMovieDB.Types
build-depends:
base ==4.3.*,
old-locale ==1.0.*,
time ==1.2.*,
HTTP ==4000.2.*,
network ==2.3.*,
bytestring ==0.9.*,
text ==0.11.*,
aeson ==0.6.*

source-repository head

Loading…
Cancel
Save