ArtDecoCardAlt.purs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. module LinkedIn.ArtDecoCardAlt where
  2. import Control.Monad.Except.Trans
  3. import Prelude
  4. import Control.Monad.Writer (Writer, tell)
  5. import Data.Either (Either(..), hush, note)
  6. import Data.Foldable (class Foldable, foldMap, foldlDefault, foldrDefault)
  7. import Data.Generic.Rep (class Generic)
  8. import Data.List (List)
  9. import Data.List.Types (NonEmptyList)
  10. import Data.Maybe (Maybe(..))
  11. import Data.Show.Generic (genericShow)
  12. import Data.Traversable (class Traversable, sequence, traverseDefault)
  13. import Effect (Effect)
  14. import Effect.Class.Console (log, logShow)
  15. import LinkedIn (DetachedNode)
  16. import LinkedIn.ArtDeco (ArtDecoPvsEntity, parseArtDecoPvsEntity)
  17. import LinkedIn.ArtDeco as AD
  18. import LinkedIn.Types (Parser, QueryError(..), QueryRunner)
  19. import LinkedIn.Utils (queryAll, queryOne, queryOneAndParse)
  20. import Web.DOM (Node)
  21. import Web.DOM.Node (nodeName)
  22. data ArtDecoCenterHeaderAlt a = ArtDecoCenterHeaderAlt {
  23. bold :: a,
  24. normal :: Maybe a
  25. }
  26. derive instance Generic (ArtDecoCenterHeaderAlt a) _
  27. derive instance Eq a => Eq (ArtDecoCenterHeaderAlt a)
  28. instance Show a => Show (ArtDecoCenterHeaderAlt a) where
  29. show = genericShow
  30. instance Functor ArtDecoCenterHeaderAlt where
  31. map f (ArtDecoCenterHeaderAlt {bold, normal}) =
  32. ArtDecoCenterHeaderAlt ({bold: f bold, normal: map f normal})
  33. instance Foldable ArtDecoCenterHeaderAlt where
  34. foldMap f (ArtDecoCenterHeaderAlt {bold, normal}) = mempty <> (f bold) <> (foldMap f normal)
  35. foldl = \x -> foldlDefault x
  36. foldr = \x -> foldrDefault x
  37. instance Traversable ArtDecoCenterHeaderAlt where
  38. sequence (ArtDecoCenterHeaderAlt {bold, normal}) = ado
  39. b <- bold
  40. n <- sequence normal
  41. in ArtDecoCenterHeaderAlt {bold: b, normal: n}
  42. traverse = \x -> traverseDefault x
  43. data ArtDecoCardAltElement a = ArtDecoCardAltElement {
  44. pvs_entity :: ArtDecoCenterHeaderAlt a
  45. }
  46. derive instance Generic (ArtDecoCardAltElement a) _
  47. derive instance Eq a => Eq (ArtDecoCardAltElement a)
  48. instance Show (ArtDecoCardAltElement Node) where
  49. show (ArtDecoCardAltElement {
  50. pvs_entity: ArtDecoCenterHeaderAlt {bold, normal}
  51. }) = "ArtDecoCardAltElement(Node " <> "ArtDecoCenterHeaderAlt(Node " <> nodeName bold <> "," <> show (map nodeName normal) <> ")" <> ")"
  52. else instance Show a => Show (ArtDecoCardAltElement a) where
  53. show = genericShow
  54. instance Functor ArtDecoCardAltElement where
  55. map f (ArtDecoCardAltElement {pvs_entity}) =
  56. ArtDecoCardAltElement ({pvs_entity: map f pvs_entity})
  57. instance Foldable ArtDecoCardAltElement where
  58. foldMap f (ArtDecoCardAltElement {pvs_entity}) = mempty <> (foldMap f pvs_entity)
  59. foldl = \x -> foldlDefault x
  60. foldr = \x -> foldrDefault x
  61. instance Traversable ArtDecoCardAltElement where
  62. sequence (ArtDecoCardAltElement {pvs_entity}) = ado
  63. p <- sequence pvs_entity
  64. in ArtDecoCardAltElement {pvs_entity: p}
  65. traverse = \x -> traverseDefault x
  66. queryArtDecoCenterHeaderAlt ∷ Node → ExceptT QueryError Effect (ArtDecoCenterHeaderAlt Node)
  67. queryArtDecoCenterHeaderAlt n = do
  68. bold <- runOne ":scope div.t-bold > span[aria-hidden=true]" n
  69. normal <-
  70. ignoreNotFound $
  71. runOne ":scope span.t-normal:not(t-black--light) > span[aria-hidden=true]" n
  72. pure $ ArtDecoCenterHeaderAlt {bold, normal: normal}
  73. queryArtDecoCardAlt ∷ Node → ExceptT QueryError Effect (ArtDecoCardAltElement Node)
  74. queryArtDecoCardAlt n = do
  75. pvs <- runOne ":scope div.pvs-entity--padded" n
  76. header <- queryArtDecoCenterHeaderAlt pvs
  77. pure $ ArtDecoCardAltElement { pvs_entity: header }
  78. runQuery ∷ ∀ a. ExceptT QueryError Effect a → Effect (Either QueryError a)
  79. runQuery = runExceptT
  80. ignoreNotFound ∷ ∀ a f. Functor f ⇒ ExceptT QueryError f a → ExceptT QueryError f (Maybe a)
  81. ignoreNotFound = mapExceptT (map ignoreNotFound')
  82. where
  83. ignoreNotFound' = case _ of
  84. (Left (QNodeNotFoundError _ )) -> Right Nothing
  85. (Left q) -> Left q
  86. (Right n') -> Right (Just n')
  87. runOne ∷ String → Node → ExceptT QueryError Effect Node
  88. runOne selector node = ExceptT $ do
  89. maybeNode <- queryOne selector node
  90. pure $ note (QNodeNotFoundError selector) maybeNode
  91. runAll ∷ String → Node → ExceptT QueryError Effect (NonEmptyList Node)
  92. runAll selector node = ExceptT $ do
  93. maybeNodes <- queryAll selector node
  94. pure $ note (QNodeListNotFoundError selector) maybeNodes