module LinkedIn.ArtDecoCardAlt where import Control.Monad.Except.Trans import Prelude import Control.Monad.Writer (Writer, tell) import Data.Either (Either(..), hush, note) import Data.Foldable (class Foldable, foldMap, foldlDefault, foldrDefault) import Data.Generic.Rep (class Generic) import Data.List (List) import Data.List.Types (NonEmptyList) import Data.Maybe (Maybe(..)) import Data.Show.Generic (genericShow) import Data.Traversable (class Traversable, sequence, traverseDefault) import Effect (Effect) import Effect.Class.Console (log, logShow) import LinkedIn (DetachedNode) import LinkedIn.ArtDeco (ArtDecoPvsEntity, parseArtDecoPvsEntity) import LinkedIn.ArtDeco as AD import LinkedIn.Types (Parser, QueryError(..), QueryRunner) import LinkedIn.Utils (queryAll, queryOne, queryOneAndParse) import Web.DOM (Node) import Web.DOM.Node (nodeName) data ArtDecoCenterHeaderAlt a = ArtDecoCenterHeaderAlt { bold :: a, normal :: Maybe a } derive instance Generic (ArtDecoCenterHeaderAlt a) _ derive instance Eq a => Eq (ArtDecoCenterHeaderAlt a) instance Show a => Show (ArtDecoCenterHeaderAlt a) where show = genericShow instance Functor ArtDecoCenterHeaderAlt where map f (ArtDecoCenterHeaderAlt {bold, normal}) = ArtDecoCenterHeaderAlt ({bold: f bold, normal: map f normal}) instance Foldable ArtDecoCenterHeaderAlt where foldMap f (ArtDecoCenterHeaderAlt {bold, normal}) = mempty <> (f bold) <> (foldMap f normal) foldl = \x -> foldlDefault x foldr = \x -> foldrDefault x instance Traversable ArtDecoCenterHeaderAlt where sequence (ArtDecoCenterHeaderAlt {bold, normal}) = ado b <- bold n <- sequence normal in ArtDecoCenterHeaderAlt {bold: b, normal: n} traverse = \x -> traverseDefault x data ArtDecoCardAltElement a = ArtDecoCardAltElement { pvs_entity :: ArtDecoCenterHeaderAlt a } derive instance Generic (ArtDecoCardAltElement a) _ derive instance Eq a => Eq (ArtDecoCardAltElement a) instance Show (ArtDecoCardAltElement Node) where show (ArtDecoCardAltElement { pvs_entity: ArtDecoCenterHeaderAlt {bold, normal} }) = "ArtDecoCardAltElement(Node " <> "ArtDecoCenterHeaderAlt(Node " <> nodeName bold <> "," <> show (map nodeName normal) <> ")" <> ")" else instance Show a => Show (ArtDecoCardAltElement a) where show = genericShow instance Functor ArtDecoCardAltElement where map f (ArtDecoCardAltElement {pvs_entity}) = ArtDecoCardAltElement ({pvs_entity: map f pvs_entity}) instance Foldable ArtDecoCardAltElement where foldMap f (ArtDecoCardAltElement {pvs_entity}) = mempty <> (foldMap f pvs_entity) foldl = \x -> foldlDefault x foldr = \x -> foldrDefault x instance Traversable ArtDecoCardAltElement where sequence (ArtDecoCardAltElement {pvs_entity}) = ado p <- sequence pvs_entity in ArtDecoCardAltElement {pvs_entity: p} traverse = \x -> traverseDefault x queryArtDecoCenterHeaderAlt ∷ Node → ExceptT QueryError Effect (ArtDecoCenterHeaderAlt Node) queryArtDecoCenterHeaderAlt n = do bold <- runOne ":scope div.t-bold > span[aria-hidden=true]" n normal <- ignoreNotFound $ runOne ":scope span.t-normal:not(t-black--light) > span[aria-hidden=true]" n pure $ ArtDecoCenterHeaderAlt {bold, normal: normal} queryArtDecoCardAlt ∷ Node → ExceptT QueryError Effect (ArtDecoCardAltElement Node) queryArtDecoCardAlt n = do pvs <- runOne ":scope div.pvs-entity--padded" n header <- queryArtDecoCenterHeaderAlt pvs pure $ ArtDecoCardAltElement { pvs_entity: header } runQuery ∷ ∀ a. ExceptT QueryError Effect a → Effect (Either QueryError a) runQuery = runExceptT ignoreNotFound ∷ ∀ a f. Functor f ⇒ ExceptT QueryError f a → ExceptT QueryError f (Maybe a) ignoreNotFound = mapExceptT (map ignoreNotFound') where ignoreNotFound' = case _ of (Left (QNodeNotFoundError _ )) -> Right Nothing (Left q) -> Left q (Right n') -> Right (Just n') runOne ∷ String → Node → ExceptT QueryError Effect Node runOne selector node = ExceptT $ do maybeNode <- queryOne selector node pure $ note (QNodeNotFoundError selector) maybeNode runAll ∷ String → Node → ExceptT QueryError Effect (NonEmptyList Node) runAll selector node = ExceptT $ do maybeNodes <- queryAll selector node pure $ note (QNodeListNotFoundError selector) maybeNodes