isi-2021-twilight-library/app/maszynista.hs
2021-04-04 00:46:19 +02:00

97 lines
4.0 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, split, join)
import Text.Regex.Posix
import Text.Printf
extractRecords = extractLinksWithText "//section[@class='widget widget_zzm_gm_archive']//a" -- pary adres-tytuł
>>> first (arr $ replace "https" "http")
>>> first (extractLinksWithText "//article//h2//a")
>>> first (arr $ first (arr $ replace "https" "http"))
>>> first (arr $ second (arr $ replace "ń" "n"))
>>> first (arr $ second (arr $ replace "ś" "s"))
>>> first (arr $ second (arr $ replace "ź" "z"))
>>> first (arr $ second (arr $ replace "ł" "l"))
>>> first (first (extractLinksWithText "//h1[@class='entry-title']//a[contains(@href,'.pdf')]"))
>>> first (arr $ first (arr $ second(arr $ replace "ń" "n")))
>>> first (arr $ first (arr $ second(arr $ replace "ś" "s")))
>>> first (arr $ first (arr $ second(arr $ replace "ź" "z")))
>>> first (arr $ first (arr $ second(arr $ replace "ł" "l")))
>>> first (arr $ second (arr $ replace "Styczen" "01"))
>>> first (arr $ second (arr $ replace "styczen" "01"))
>>> first (arr $ second (arr $ replace "Luty" "02"))
>>> first (arr $ second (arr $ replace "luty" "02"))
>>> first (arr $ second (arr $ replace "Marzec" "03"))
>>> first (arr $ second (arr $ replace "marzec" "03"))
>>> first (arr $ second (arr $ replace "Kwiecien" "04"))
>>> first (arr $ second (arr $ replace "kwiecien" "04"))
>>> first (arr $ second (arr $ replace "Maj" "05"))
>>> first (arr $ second (arr $ replace "maj" "05"))
>>> first (arr $ second (arr $ replace "Czerwiec" "06"))
>>> first (arr $ second (arr $ replace "czerwiec" "06"))
>>> first (arr $ second (arr $ replace "Lipiec" "07"))
>>> first (arr $ second (arr $ replace "lipiec" "07"))
>>> first (arr $ second (arr $ replace "Sierpien" "08"))
>>> first (arr $ second (arr $ replace "sierpien" "08"))
>>> first (arr $ second (arr $ replace "sierpnia" "08"))
>>> first (arr $ second (arr $ replace "Wrzesien" "09"))
>>> first (arr $ second (arr $ replace "wrzesien" "09" ))
>>> first (arr $ second (arr $ replace "wrzesnia" "09" ))
>>> first (arr $ second (arr $ replace "pazdziernika" "10" ))
>>> first (arr $ second (arr $ replace "Pazdziernik" "10" ))
>>> first (arr $ second (arr $ replace "pazdziernik" "10" ))
>>> first (arr $ second (arr $ replace "listopada" "11" ))
>>> first (arr $ second (arr $ replace "Listopad" "11" ))
>>> first (arr $ second (arr $ replace "listopad" "11" ))
>>> first (arr $ second (arr $ replace "Grudzien" "12"))
>>> first (arr $ second (arr $ replace "grudzien" "12"))
>>> first (arr $ second (arr $ replace "grudnia" "12"))
>>> first (arr $ second (arr $ replace " " "-"))
-- ... a tutaj te trójki przerabiamy do docelowej struktury ShadowItem
toShadowItem :: (((String, String), String), String) -> ShadowItem
toShadowItem (((url, articleTitle), articleDatePart), year) =
(defaultShadowItem url title) {
originalDate = getDate articleDatePart,
itype = "periodical",
format = Just "pdf",
finalUrl = url
}
where title = fixTitle articleTitle
fixTitle :: String -> String
fixTitle title
| title =~ "Glos Maszynisty .*" = title
| otherwise = "Glos Maszynisty " ++ title
getDate :: String -> String
getDate url =
case url =~~ ("[0-9]*-?[0-9]{2}-[0-9]{4}" :: String) of
Just date -> concat([join "-" (reverse (split "-" date))]) :: String
otherwise -> ""
main = do
let start = "http://zzm.org.pl/glos-maszynisty/"
let shadowLibrary = ShadowLibrary {logoUrl=Nothing,
lname="Głos Maszynisty",
abbrev="GloMasz",
lLevel=0,
webpage=start}
extractItemsStartingFromUrl shadowLibrary start (extractRecords >>> arr toShadowItem)