twilight-library/app/ptd.hs

53 lines
2.1 KiB
Haskell
Raw Normal View History

2021-04-05 18:48:35 +02:00
{-# LANGUAGE UTF #-}
2021-04-05 18:48:35 +02:00
{-# LANGUAGE Arrows, NoMonomorphismRestriction #-}
2021-04-05 18:48:35 +02:00
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 = (downloadDocumentWithEncoding "UTF-8" &&& this)
>>> first (getXPathTrees xpathCondition
>>> ((getXPathTrees "//a" >>> getAttrValue "href")
&&& (listA (deep isText >>> getText)
>>> arr (intercalate " "))))
>>> arr rotateSecTh
>>> first expandURIFixed
2021-04-05 18:48:35 +02:00
extractRecords = extractLinksWithText "//div[@class='entry-content']/p/a[contains(@href, 'id')]"
>>> first (extractNestedLinksWithText "//div[@class='entry-content']/p[strong[a]] | //div[@class='entry-content']/p[a]")
2021-04-05 18:48:35 +02:00
toShadowItem :: ((String, String), String) -> ShadowItem
toShadowItem ((url, articleTitle), magazineTitle) =
2021-04-05 18:48:35 +02:00
(defaultShadowItem url title) {
originalDate = Just date,
itype = "periodical",
format = Just "pdf",
finalUrl = url
}
where title = magazineTitle ++ " - " ++ (replace " \8211 pdf" "" articleTitle)
date = getYear url
2021-04-05 18:48:35 +02:00
getYear :: String -> String
getYear url =
case url =~~ "/rocznik[0-9]{2}/" :: Maybe [[String]] of
2021-04-05 18:48:35 +02:00
Just [[_, year]] -> year
otherwise -> case url =~~ "/(19[0-9][0-9]|20[0-9][0-9])/" :: Maybe [[String]] of
Just [[_, year]] -> "19" ++ (replace "rocznik" "" year)
otherwise -> ""
2021-04-05 18:48:35 +02:00
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)