module Rascal.API where
import Control.Applicative ((<$>))
import Text.Printf (printf)
import Control.Exception (handle)
import Data.Aeson (parseJSON, FromJSON)
import Network.Curl.Aeson (curlAeson, noData, CurlAesonException, errorMsg, curlCode)
import Network.Curl.Opts (CurlOption(CurlUserAgent))
import Network.Curl.Code (CurlCode(CurlOK))
import Rascal.Constants
import Rascal.Types
getListing :: String -> String -> Int -> Maybe String -> IO NamedListing
getListing select subreddit cnt aftr =
let apiurl = "http://www.reddit.com/r/" ++ subreddit ++ "/%s.json?count="
++ show cnt ++ maybe "" ("&after=" ++) aftr
in NamedListing (subreddit ++ " -- " ++ select) cnt <$>
getThing apiurl select emptyListing
getThing :: FromJSON a => String -> String -> a -> IO a
getThing apiurl sort emptyThing =
let sort' = if sort `notElem` map snd availableSorts
then snd . head $ availableSorts
else sort
apiurl' = printf apiurl sort'
in handle (handleCurlAesonException emptyThing) $ do
l <- curlAeson parseJSON "GET" apiurl' [CurlUserAgent userAgent] noData
return $! l
handleCurlAesonException :: a -> CurlAesonException -> IO a
handleCurlAesonException x e = do
putStrLn $ red ++ "Caught exception: " ++ reset ++ errorMsg e
putStrLn $ if curlCode e == CurlOK
then "(Might indicate a non-existing subreddit)"
else "cURL code: " ++ (drop 4 . show . curlCode) e
return x
getComments :: String -> String -> String -> IO Comments
getComments subreddit article csort =
let apiurl = "http://www.reddit.com/r/" ++ subreddit ++
"/comments/" ++ article ++ ".json?sort=%s"
in getThing apiurl csort emptyComments