|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE OverloadedStrings #-} |
| 3 | +{-# LANGUAGE TypeOperators #-} |
| 4 | + |
| 5 | +module AOC.API ( |
| 6 | + mkAocClient, |
| 7 | +) where |
| 8 | + |
| 9 | +import AOC.Types |
| 10 | +import Control.Monad ((>=>)) |
| 11 | +import Data.ByteString qualified as BS |
| 12 | +import Data.Proxy (Proxy (..)) |
| 13 | +import Data.Text qualified as T |
| 14 | +import Data.Text.Encoding qualified as T |
| 15 | +import Data.Time.Clock (addUTCTime, getCurrentTime) |
| 16 | +import GHC.Conc (atomically, newTVar) |
| 17 | +import Network.HTTP.Client (Cookie (..), createCookieJar) |
| 18 | +import Network.HTTP.Client.TLS (newTlsManager) |
| 19 | +import Network.HTTP.Media ((//), (/:)) |
| 20 | +import Servant.API |
| 21 | +import Servant.Client hiding ((//), (/:)) |
| 22 | + |
| 23 | +mkAocClient :: String -> Int -> Int -> IO (IO T.Text, Submission -> IO Answer) |
| 24 | +mkAocClient session year day = do |
| 25 | + env <- mkAocEnv session |
| 26 | + let aocInput :<|> aocSubmit = aocClient year day |
| 27 | + f = flip runClientM env >=> pure . either (error . show) id |
| 28 | + aocInput' = f aocInput |
| 29 | + aocSubmit' = f . aocSubmit |
| 30 | + return (aocInput', aocSubmit') |
| 31 | + |
| 32 | +mkAocEnv :: String -> IO ClientEnv |
| 33 | +mkAocEnv session = do |
| 34 | + current <- getCurrentTime |
| 35 | + manager <- newTlsManager |
| 36 | + let |
| 37 | + base = BaseUrl Https "adventofcode.com" 443 "" |
| 38 | + env = mkClientEnv manager base |
| 39 | + year = 60 * 60 * 24 * 365 |
| 40 | + expiry = addUTCTime year current |
| 41 | + cookie = |
| 42 | + Cookie |
| 43 | + { cookie_name = "session", |
| 44 | + cookie_value = T.encodeUtf8 . T.pack $ session, |
| 45 | + cookie_expiry_time = expiry, |
| 46 | + cookie_domain = T.encodeUtf8 . T.pack $ baseUrlHost base, |
| 47 | + cookie_path = "/", |
| 48 | + cookie_creation_time = current, |
| 49 | + cookie_last_access_time = current, |
| 50 | + cookie_persistent = True, |
| 51 | + cookie_host_only = True, |
| 52 | + cookie_secure_only = True, |
| 53 | + cookie_http_only = True |
| 54 | + } |
| 55 | + cookies <- atomically $ newTVar (createCookieJar [cookie]) |
| 56 | + return $ env {cookieJar = Just cookies} |
| 57 | + |
| 58 | +aocClient :: Int -> Int -> (ClientM T.Text :<|> (Submission -> ClientM Answer)) |
| 59 | +aocClient = client (Proxy @API) |
| 60 | + |
| 61 | +type API = |
| 62 | + Capture "year" Int :> |
| 63 | + ("day" :> Capture "day" Int :> |
| 64 | + ( |
| 65 | + -- GET /:year/day/:day/input |
| 66 | + ("input" :> Get '[RawPlainText] T.Text) :<|> |
| 67 | + -- POST /:year/day/:day/answer level=<1|2>&answer=_ |
| 68 | + ("answer" :> ReqBody '[FormUrlEncoded] Submission :> Post '[HTML] Answer) |
| 69 | + ) |
| 70 | + ) |
| 71 | + |
| 72 | +-- This is silly: https://github.com/haskell-servant/servant/issues/1002 |
| 73 | +data RawPlainText |
| 74 | + |
| 75 | +instance Accept RawPlainText where |
| 76 | + contentType _ = "text" // "plain" |
| 77 | + |
| 78 | +instance MimeUnrender RawPlainText T.Text where |
| 79 | + mimeUnrender _ = Right . T.decodeUtf8 . BS.toStrict |
| 80 | + |
| 81 | +-- TODO implement marshalling from HTML to 'Day', 'Year', and 'Answer' |
| 82 | +-- types which extracts yearly calendars, day prompts, and |
| 83 | +-- pre-existing correct answer submissions via scraping the received |
| 84 | +-- HTML. |
| 85 | + |
| 86 | +data HTML |
| 87 | + |
| 88 | +instance Accept HTML where |
| 89 | + contentType _ = "text" // "html" /: ("charset", "utf-8") |
| 90 | + |
| 91 | +data Answer = Correct | Low | High | Empty |
| 92 | + |
| 93 | +instance MimeUnrender HTML Answer where |
| 94 | + mimeUnrender _ bs |
| 95 | + | "correct" `BS.isInfixOf` bs' = Right Correct |
| 96 | + | "low" `BS.isInfixOf` bs' = Right Low |
| 97 | + | "high" `BS.isInfixOf` bs' = Right High |
| 98 | + | "provide an answer" `BS.isInfixOf` bs' = Right Empty |
| 99 | + | otherwise = Left "Unknown answer response" |
| 100 | + where |
| 101 | + bs' = BS.toStrict bs |
0 commit comments