twilight-library/app/ptd.hs

63 lines
2.5 KiB
Haskell

{-# LANGUAGE Arrows, NoMonomorphismRestriction #-}
import ShadowLibrary.Core
import Text.XML.HXT.Core
import Text.XML.HXT.XPath
import Data.List
import Data.List.Utils (replace)
import Text.Regex.Posix
import Text.Printf
extractNestedLinksWithText xpathCondition = (downloadDocument &&& this)
>>> first (getXPathTrees xpathCondition
>>> ((getXPathTrees "//a" >>> getAttrValue "href")
&&& (listA (deep isText >>> getText)
>>> arr (intercalate " "))))
>>> arr rotateSecTh
>>> first expandURIFixed
extractRecords = extractLinksWithText "//div[@class='entry-content']/p/a[contains(@href, 'id')]"
>>> first (arr $ replace "http:" "https:")
>>> first (extractNestedLinksWithText "//div[@class='entry-content']/p[strong[a]] | //div[@class='entry-content']/p[a]")
toShadowItem :: ((String, String), String) -> ShadowItem
toShadowItem ((url, articleTitle), magazineTitle) =
(defaultShadowItem url title) {
originalDate = date,
itype = "periodical",
format = Just "pdf",
finalUrl = url
}
where title = magazineTitle ++ " - " ++ (replace " \8211 pdf" "" articleTitle)
date = getYear url
getYear :: String -> Maybe String
getYear url =
case url =~~ "/(rocznik[0-9][0-9])/" :: Maybe [[String]] of
Just [[_, raw_year]] -> Just (catalogueMapper (replace "rocznik" "" raw_year))
otherwise -> case url =~~ "/(19[0-9][0-9]|20[0-9][0-9])/" :: Maybe [[String]] of
Just [[_, year]] -> (Just year)
otherwise -> Nothing
-- Rozwiązanie także zadziała
-- catalogueMapper :: String -> String
-- catalogueMapper number = show((read number :: Integer) + 1952)
catalogueMapper :: String -> String
catalogueMapper number
| number == "67" = "2019"
| number == "66" = "2018"
| number == "65" = "2017"
| number == "64" = "2016"
main = do
let baseUrl = "https://www.ptd.pl/?page_id=7"
let shadowLibrary = ShadowLibrary {logoUrl=Nothing,
lname="Polskie Towarzystwo Dendrologiczne",
abbrev="ptd",
lLevel=0,
webpage=baseUrl}
extractItemsStartingFromUrl shadowLibrary baseUrl (extractRecords >>> arr toShadowItem)