Permalink
Browse files

Remove json dependency

  • Loading branch information...
1 parent 53d1e44 commit 62b7b6324e96cba442eef39fc3925369ac247824 @jaspervdj committed May 20, 2012
Showing with 33 additions and 18 deletions.
  1. +0 −1 number-six.cabal
  2. +33 −17 src/NumberSix/Handlers/Tumblr.hs
View
@@ -81,7 +81,6 @@ Library
curl >= 1.3 && < 1.4,
dataenc >= 0.13 && < 0.15,
deepseq >= 1.0 && < 1.4,
- json >= 0.4 && < 0.6,
mtl >= 2 && < 2.1,
network >= 2.3 && < 2.4,
old-locale >= 1.0 && < 1.1,
@@ -6,11 +6,12 @@ module NumberSix.Handlers.Tumblr
--------------------------------------------------------------------------------
-import Control.Applicative ((<$>))
-import Control.Monad(forM)
+import Control.Applicative ((<$>), (<*>))
+import Control.Monad (mzero)
+import Data.Aeson (FromJSON (..), Value (..), (.:))
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as B
-import Text.JSON
+import qualified Data.ByteString.Char8 as BC
--------------------------------------------------------------------------------
@@ -22,6 +23,26 @@ import NumberSix.Util.Http
--------------------------------------------------------------------------------
+data Tumblr = Tumblr [Post] deriving (Show)
+
+
+--------------------------------------------------------------------------------
+instance FromJSON Tumblr where
+ parseJSON (Object o) = Tumblr <$> o .: "posts"
+ parseJSON _ = mzero
+
+
+--------------------------------------------------------------------------------
+data Post = Post ByteString ByteString deriving (Show)
+
+
+--------------------------------------------------------------------------------
+instance FromJSON Post where
+ parseJSON (Object o) = Post <$> o .: "url" <*> o .: "slug"
+ parseJSON _ = mzero
+
+
+--------------------------------------------------------------------------------
-- | Tumblr gives a string containing the JSON value, but assign it to a
-- variable in a JavaScript like approach, defining the variable
-- tumblr_api_read to use further in other JS scripts on the caling page.
@@ -41,30 +62,25 @@ cleanTumblrJSON str
-- obtain the last tumble, just pass 1 as the count.
randomTumble :: ByteString -> Int -> Irc ByteString
randomTumble query count = do
- result <- decode . B.unpack . cleanTumblrJSON <$> httpGet url
+ result <- parseJsonEither . cleanTumblrJSON <$> httpGet url
case result of
- Ok (JSObject root) ->
- let Ok dest = do
- JSArray posts <- valFromObj "posts" root
- forM posts $ \(JSObject post) -> do
- url' <- valFromObj "url" post
- slug <- valFromObj "slug" post
- return (B.pack (fromJSString slug),
- B.pack (fromJSString url'))
- in randomElement dest >>= uncurry textAndUrl
- _ -> return $ "Something went wrong when fetching " <> url
+ Right (Tumblr posts) -> do
+ Post url' slug <- randomElement posts
+ textAndUrl slug url'
+ Left e ->
+ return $ "Something went wrong: " <> BC.pack e
where
url = "http://" <> query <> ".tumblr.com/api/read/json?num=" <>
B.pack (show count)
--------------------------------------------------------------------------------
tumblr :: ByteString -> Irc ByteString
-tumblr query =
- let command : user = B.words query
+tumblr query =
+ let (command : user) = B.words query
in case command of
"last" -> randomTumble (head user) 1
- _ -> randomTumble query 50
+ _ -> randomTumble query 50
--------------------------------------------------------------------------------

0 comments on commit 62b7b63

Please sign in to comment.