beng

Happy Birthday, bos!

Jan 3rd, 2015
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE OverloadedStrings #-}
  2.  
  3. import qualified Facebook as FB
  4.  
  5. import Control.Applicative
  6. import Control.Monad (mzero)
  7. import Control.Monad.IO.Class (liftIO)
  8. import qualified Data.Aeson as A
  9. import Data.Time
  10. import Data.Text
  11. import Formatting
  12. import Network.HTTP.Conduit (withManager)
  13.  
  14. data UserWithBirthday = UserWithBirthday
  15.   { birthday :: Text
  16.   } deriving Show
  17.  
  18. instance A.FromJSON UserWithBirthday where
  19.   parseJSON (A.Object v) = UserWithBirthday <$>
  20.                          v A..: "birthday"
  21.   parseJSON _ = mzero
  22.  
  23. -- Copy paste a valid Access Token (make sure to request the 'user_birthday' permission) from:                                                                                                                            
  24. -- https://developers.facebook.com/tools/explorer/                                                                                                                                                                        
  25. fbTokenData :: Text
  26. fbTokenData = "paste-your-token-here"
  27.  
  28. userAccessToken :: FB.UserAccessToken
  29. userAccessToken = FB.UserAccessToken (FB.Id "0") fbTokenData farInTheFuture
  30.   where
  31.     Just farInTheFuture = parseTime (error "farInTheFuture") "%Y" "3000"
  32.  
  33. parseFbBirthday :: Text -> UTCTime
  34. parseFbBirthday formatted = case parseTime (error "yearsOld") "%m/%d/%Y" (unpack formatted) of
  35.   Just t -> t
  36.   Nothing -> undefined
  37.  
  38. yearsOld :: UTCTime -> UTCTime -> Text
  39. yearsOld time curTime = sformat ("Happy " % ords % " birthday!") (round $ (diffUTCTime curTime time) / 31536000)
  40.  
  41. main :: IO ()
  42. main = do
  43.   curTime <- getCurrentTime
  44.   withManager $ \manager -> do
  45.     FB.runNoAuthFacebookT manager $ do
  46.       userWithBirthday <- FB.getObject "/v2.2/me" [ ("fields", "birthday") ] (Just userAccessToken)
  47.       liftIO $ print $ yearsOld (parseFbBirthday (birthday userWithBirthday)) curTime
Advertisement
Add Comment
Please, Sign In to add comment