{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-}
module Json.TranscriptTypes where
import Control.Applicative
import Control.Monad (forM_, join, mzero)
import Data.Aeson (FromJSON (..), ToJSON (..),
Value (..), eitherDecode,
object, pairs, (.:), (.:?),
(.=))
import Data.Aeson.AutoType.Alternative
import Data.ByteString.Lazy
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.Monoid
import Data.Text (Text)
import qualified GHC.Generics
import System.Environment (getArgs)
import System.Exit (exitFailure, exitSuccess)
import System.IO (hPutStrLn, stderr)
o .:?? val = fmap join (o .:? val)
data LatestTopics = LatestTopics {
latestTopicsTopics :: [(Maybe Value)],
latestTopicsRevision :: Text
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON LatestTopics where
parseJSON (Object v) = LatestTopics <$> v .: "topics" <*> v .: "revision"
parseJSON _ = mzero
data Topics = Topics {
topicsLatestTopics :: LatestTopics
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON Topics where
parseJSON (Object v) = Topics <$> v .: "latest"
parseJSON _ = mzero
data Tasks = Tasks {
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON Tasks where
parseJSON (Object v) = return Tasks
parseJSON _ = mzero
data Progress = Progress {
progressStatus :: Text,
progressJobId :: Text,
progressTasks :: Tasks
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON Progress where
parseJSON (Object v) = Progress <$> v .: "status" <*> v .: "jobId" <*> v .: "tasks"
parseJSON _ = mzero
data Job = Job {
jobProgress :: Progress
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON Job where
parseJSON (Object v) = Job <$> v .: "progress"
parseJSON _ = mzero
data T = T {
tUnknown :: [Text]
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON T where
parseJSON (Object v) = T <$> v .: "unknown"
parseJSON _ = mzero
data WordsKeyElt = WordsKeyElt {
wordsKeyEltRelevance :: Text,
wordsKeyEltT :: T,
wordsKeyEltName :: Text
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON WordsKeyElt where
parseJSON (Object v) = WordsKeyElt <$> v .: "relevance" <*> v .: "t" <*> v .: "name"
parseJSON _ = mzero
data LatestKeywords = LatestKeywords {
latestKeywordsGroups :: [(Maybe Value)],
latestKeywordsWordsKey :: [WordsKeyElt],
latestKeywordsRevision :: Text
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON LatestKeywords where
parseJSON (Object v) = LatestKeywords <$> v .: "groups" <*> v .: "words" <*> v .: "revision"
parseJSON _ = mzero
data Keywords = Keywords {
keywordsLatestKeywords :: LatestKeywords
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON Keywords where
parseJSON (Object v) = Keywords <$> v .: "latest"
parseJSON _ = mzero
data Length = Length {
lengthDescriptive :: Text,
lengthMilliseconds :: Double
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON Length where
parseJSON (Object v) = Length <$> v .: "descriptive" <*> v .: "milliseconds"
parseJSON _ = mzero
data Metadata = Metadata {
metadataLength :: Length,
metadataContentType :: Text
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON Metadata where
parseJSON (Object v) = Metadata <$> v .: "length" <*> v .: "contentType"
parseJSON _ = mzero
data WordsTranscriptsElt = WordsTranscriptsElt {
wordsTranscriptsEltW :: Text,
wordsTranscriptsEltM :: (Maybe (Text:|:[(Maybe Value)])),
wordsTranscriptsEltP :: Double,
wordsTranscriptsEltE :: Double,
wordsTranscriptsEltC :: Double,
wordsTranscriptsEltS :: Double
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON WordsTranscriptsElt where
parseJSON (Object v) = WordsTranscriptsElt <$> v .: "w" <*> v .:?? "m" <*> v .: "p" <*> v .: "e" <*> v .: "c" <*> v .: "s"
parseJSON _ = mzero
data LatestTranscripts = LatestTranscripts {
latestTranscriptsEngine :: Text,
latestTranscriptsConfidence :: Double,
latestTranscriptsName :: Text,
latestTranscriptsWordsTranscripts :: [WordsTranscriptsElt],
latestTranscriptsRevision :: Text
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON LatestTranscripts where
parseJSON (Object v) = LatestTranscripts <$> v .: "engine" <*> v .: "confidence" <*> v .: "name" <*> v .: "words" <*> v .: "revision"
parseJSON _ = mzero
data Transcripts = Transcripts {
transcriptsLatestTranscripts :: LatestTranscripts
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON Transcripts where
parseJSON (Object v) = Transcripts <$> v .: "latest"
parseJSON _ = mzero
data Media = Media {
mediaStatus :: Text,
mediaTopics :: Topics,
mediaMediaId :: Text,
mediaDateCreated :: Text,
mediaJob :: Job,
mediaKeywords :: Keywords,
mediaMetadata :: Metadata,
mediaTranscripts :: Transcripts,
mediaDateFinished :: Text
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON Media where
parseJSON (Object v) = Media <$> v .: "status" <*> v .: "topics" <*> v .: "mediaId" <*> v .: "dateCreated" <*> v .: "job" <*> v .: "keywords" <*> v .: "metadata" <*> v .: "transcripts" <*> v .: "dateFinished"
parseJSON _ = mzero
data Self = Self {
selfHref :: Text
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON Self where
parseJSON (Object v) = Self <$> v .: "href"
parseJSON _ = mzero
data Links = Links {
linksSelf :: Self
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON Links where
parseJSON (Object v) = Links <$> v .: "self"
parseJSON _ = mzero
data Transcript = Transcript {
topLevelMedia :: Media,
topLevelLinks :: Links
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON Transcript where
parseJSON (Object v) = Transcript <$> v .: "media" <*> v .: "_links"
parseJSON _ = mzero
parse :: ByteString -> Either String Transcript
parse = eitherDecode