twilight-library/app/my_robot.hs

50 lines
1.4 KiB
Haskell
Raw Normal View History

2022-03-28 18:30:58 +02:00
{-# 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
2022-03-31 17:12:47 +02:00
Just year -> replace "%C5%82" "l" (replace "%20" " " (replace ".pdf" "" year))
2022-03-28 18:30:58 +02:00
otherwise -> error $ "unexpected url: " ++ url
getDate url =
2022-03-31 17:12:47 +02:00
case url =~~ "[0-9]{4}[-.][0-9]{2}[-.][0-9]{2}|[0-9]{4}" :: Maybe String of
2022-03-28 18:30:58 +02:00
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)