twilight-library/app/my_robot.hs
2022-03-31 17:12:47 +02:00

50 lines
1.4 KiB
Haskell

{-# LANGUAGE Arrows, NoMonomorphismRestriction #-}
import ShadowLibrary.Core
import Text.XML.HXT.Core
import Text.XML.HXT.XPath
-- import Text.XML.HXT.Curl
import Data.List
import Data.List.Utils (replace)
import Text.Regex.Posix
import Text.Printf
extractRecords = extractLinksWithText "//li/a[contains(@href,'.pdf')]"
toShadowItem :: (String, String) -> ShadowItem
toShadowItem (url, articleTitle) =
(defaultShadowItem url title) {
originalDate = Just date,
format = Just "pdf",
finalUrl = url
}
where title = findText url
date = getDate url
findText url =
case url =~~ "[0-9]{4}.*" :: Maybe String of
Just year -> replace "%C5%82" "l" (replace "%20" " " (replace ".pdf" "" year))
otherwise -> error $ "unexpected url: " ++ url
getDate url =
case url =~~ "[0-9]{4}[-.][0-9]{2}[-.][0-9]{2}|[0-9]{4}" :: Maybe String of
Just year -> replace "." "-" year
otherwise -> error $ "unexpected url: " ++ url
main = do
let start = "http://kolej-szyndzielnia.pl/kolej-gondolowa/"
let shadowLibrary = ShadowLibrary {logoUrl=Nothing,
lname="kolej_gondolowa",
abbrev="kolej_gondolowa",
lLevel=0,
webpage=start}
extractItemsStartingFromUrl shadowLibrary start (extractRecords >>> arr toShadowItem)