49 lines
2.0 KiB
Haskell
49 lines
2.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)
|
||
|
|
||
|
import Text.Regex.Posix
|
||
|
import Text.Printf
|
||
|
|
||
|
|
||
|
extractRecords = extractLinksWithText "//a[contains(@href,'.pdf')]"
|
||
|
>>> second (arr $ replace "\r\n " " ") -- czyścimy drugi element pary, czyli tytuł z niepotrzebnych białych znaków
|
||
|
>>> second (arr $ replace "\323" "N") -- czyścimy drugi element pary, czyli tytuł z niepotrzebnych białych znaków
|
||
|
>>> second (arr $ replace "\322" "L") -- czyścimy drugi element pary, czyli tytuł z niepotrzebnych białych znaków
|
||
|
>>> second (arr $ replace "\324" "n") -- czyścimy drugi element pary, czyli tytuł z niepotrzebnych białych znaków
|
||
|
>>> second (arr $ replace "\160" " ") -- czyścimy drugi element pary, czyli tytuł z niepotrzebnych białych znaków
|
||
|
|
||
|
-- ... a tutaj te trójki przerabiamy do docelowej struktury ShadowItem
|
||
|
toShadowItem :: (String, String) -> ShadowItem
|
||
|
toShadowItem (url, articleTitle) =
|
||
|
(defaultShadowItem url title) {
|
||
|
originalDate = extractMonthAndYear title,
|
||
|
itype = "periodical",
|
||
|
format = Just "pdf",
|
||
|
finalUrl = url,
|
||
|
fileSize = extractFileSize title
|
||
|
}
|
||
|
where title = articleTitle
|
||
|
extractMonthAndYear :: String -> Maybe String
|
||
|
extractMonthAndYear n =
|
||
|
case n =~~ ("([A-Z]* ?- ?)?[A-Z]* ((1[6789]|20)[0-9][0-9]-)?(1[6789]|20)[0-9][0-9]" :: String) of
|
||
|
Just year -> Just year
|
||
|
otherwise -> Nothing
|
||
|
extractFileSize :: String -> Maybe String
|
||
|
extractFileSize n =
|
||
|
case n =~~ ("([0-9]*)\\,([0-9]*) MB" :: String) of
|
||
|
Just fileSize -> Just fileSize
|
||
|
otherwise -> Nothing
|
||
|
|
||
|
main = do
|
||
|
let start = "http://www.rytwiany.com.pl/index.php?sid=5"
|
||
|
let shadowLibrary = ShadowLibrary {logoUrl=Nothing,lname="Rytwiany",abbrev="Ryt",lLevel=0,webpage="http://www.rytwiany.com.pl"}
|
||
|
extractItemsStartingFromUrl shadowLibrary start (extractRecords >>> arr toShadowItem)
|
||
|
|