Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE OverloadedStrings #-}
- import qualified Facebook as FB
- import Control.Applicative
- import Control.Monad (mzero)
- import Control.Monad.IO.Class (liftIO)
- import qualified Data.Aeson as A
- import Data.Time
- import Data.Text
- import Formatting
- import Network.HTTP.Conduit (withManager)
- data UserWithBirthday = UserWithBirthday
- { birthday :: Text
- } deriving Show
- instance A.FromJSON UserWithBirthday where
- parseJSON (A.Object v) = UserWithBirthday <$>
- v A..: "birthday"
- parseJSON _ = mzero
- -- Copy paste a valid Access Token (make sure to request the 'user_birthday' permission) from:
- -- https://developers.facebook.com/tools/explorer/
- fbTokenData :: Text
- fbTokenData = "paste-your-token-here"
- userAccessToken :: FB.UserAccessToken
- userAccessToken = FB.UserAccessToken (FB.Id "0") fbTokenData farInTheFuture
- where
- Just farInTheFuture = parseTime (error "farInTheFuture") "%Y" "3000"
- parseFbBirthday :: Text -> UTCTime
- parseFbBirthday formatted = case parseTime (error "yearsOld") "%m/%d/%Y" (unpack formatted) of
- Just t -> t
- Nothing -> undefined
- yearsOld :: UTCTime -> UTCTime -> Text
- yearsOld time curTime = sformat ("Happy " % ords % " birthday!") (round $ (diffUTCTime curTime time) / 31536000)
- main :: IO ()
- main = do
- curTime <- getCurrentTime
- withManager $ \manager -> do
- FB.runNoAuthFacebookT manager $ do
- userWithBirthday <- FB.getObject "/v2.2/me" [ ("fields", "birthday") ] (Just userAccessToken)
- liftIO $ print $ yearsOld (parseFbBirthday (birthday userWithBirthday)) curTime
Advertisement
Add Comment
Please, Sign In to add comment