module Rascal.Types where
import Control.Applicative (empty, (<$>), (<*>), (<|>))
import Data.Aeson
import Data.Vector (toList)
import Rascal.Utils (unescape)
data Link = Link {
title :: String,
author :: String,
score :: Int,
isSelf :: Bool,
link :: String,
uid :: String,
numComments :: Int,
selfHtml :: String,
selfText :: String
}
data Listing = Listing
{ links :: [Link]
, after :: Maybe String
}
emptyListing :: Listing
emptyListing = Listing [] Nothing
data NamedListing = NamedListing
{ name :: String
, count :: Int
, listing :: Listing
}
newtype Comments = Comments [CommentListing] deriving (Show)
emptyComments :: Comments
emptyComments = Comments []
newtype CommentListing = CommentListing [Comment] deriving (Show)
data Comment = Comment
{ _cauthor :: String
, _ups :: Int
, _downs :: Int
, __bodyHtml :: String
, _body :: String
, _children :: CommentListing
}
| OriginalArticle deriving (Show)
instance FromJSON Link where
parseJSON (Object o) = do
datum <- o .: "data"
etitle <- datum .: "title"
Link (unescape etitle)
<$> datum .: "author"
<*> datum .: "score"
<*> datum .: "is_self"
<*> datum .: "url"
<*> datum .: "name"
<*> datum .: "num_comments"
<*> datum .:? "selftext_html" .!= ""
<*> datum .: "selftext"
parseJSON _ = empty
instance FromJSON Listing where
parseJSON (Object o) = do
datum <- o .: "data"
Listing <$> datum .: "children"
<*> datum .: "after"
parseJSON _ = empty
instance FromJSON Comments where
parseJSON (Array a) = Comments <$> mapM parseJSON (toList a)
parseJSON _ = empty
instance FromJSON CommentListing where
parseJSON (Object o) = do
datum <- o .: "data"
a <- datum .: "children"
CommentListing <$> mapM parseJSON (toList a)
parseJSON _ = empty
instance FromJSON Comment where
parseJSON (Object o) = do
kind <- o .: "kind"
if (kind :: String) == "t1"
then do
datum <- o .: "data"
Comment <$> datum .: "author"
<*> datum .: "ups"
<*> datum .: "downs"
<*> datum .: "body_html"
<*> datum .: "body"
<*> (datum .: "replies" <|> return (CommentListing []))
else
return OriginalArticle
parseJSON _ = empty
data RuntimeConf = RuntimeConf
{ textWidth :: Int
, textHeight :: Int
, commentSort :: String
, linkSort :: String
, pageComments :: Bool
}