{-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeInType #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE ViewPatterns #-} -- | -- Module : Advent.API -- Copyright : (c) Justin Le 2019 -- License : BSD3 -- -- Maintainer : justin@jle.im -- Stability : experimental -- Portability : non-portable -- -- Raw Servant API for Advent of Code. Can be useful for building mock -- servers, generating documentation and other servanty things, or -- low-level raw requests. -- -- If you use this to make requests directly, please use responsibly: do -- not make automated requests more than once per day and throttle all -- manual requestes. See notes in "Advent". -- -- @since 0.2.0.0 -- module Advent.API ( -- * Servant API AdventAPI , adventAPI , adventAPIClient , adventAPIPuzzleClient -- * Types , HTMLTags , FromTags(..) , Articles , Divs , Scripts , RawText -- * Internal , processHTML ) where import Advent.Types import Control.Applicative import Control.Monad import Control.Monad.State import Data.Bifunctor import Data.Char import Data.Finite import Data.Foldable import Data.List.NonEmpty (NonEmpty(..)) import Data.Map (Map) import Data.Maybe import Data.Ord import Data.Proxy import Data.Text (Text) import Data.Time hiding (Day) import GHC.TypeLits import Servant.API import Servant.Client import Text.HTML.TagSoup.Tree (TagTree(..)) import Text.Read (readMaybe) import qualified Data.ByteString.Lazy as BSL import qualified Data.List.NonEmpty as NE import qualified Data.Map as M import qualified Data.Text as T import qualified Data.Text.Encoding as T import qualified Network.HTTP.Media as M import qualified Text.HTML.TagSoup as H import qualified Text.HTML.TagSoup.Tree as H #if !MIN_VERSION_base(4,11,0) import Data.Semigroup ((<>)) #endif #if !MIN_VERSION_time(1,9,0) import Data.Time.LocalTime.Compat #endif -- | Raw "text/plain" MIME type data RawText instance Accept RawText where contentType _ = "text" M.// "plain" instance MimeUnrender RawText Text where mimeUnrender _ = first show . T.decodeUtf8' . BSL.toStrict -- | Interpret repsonse as a list of HTML 'T.Text' found in the given type of -- tag -- -- @since 0.2.3.0 data HTMLTags (tag :: Symbol) -- | Interpret a response as a list of HTML 'T.Text' found in @
@ tags. type Articles = HTMLTags "article" -- | Interpret a response as a list of HTML 'T.Text' found in @
@ tags. -- -- @since 0.2.3.0 type Divs = HTMLTags "div" -- | Interpret a response as a list of HTML 'T.Text' found in @