tajemnica-atari/app/tajemnicaatari.hs
2021-05-01 05:27:55 +02:00

70 lines
2.7 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
import Data.List.Utils
extractRecords = extractLinksWithText "//a[@class='ramka']" -- pary adres-tytuł
>>> first (extractLinksWithText "//div/a[contains(@href, '.zip') and not (contains(@href,'text_offline.zip'))]") -- tutaj wybieramy paczki zip z plikami DjVu, jeśli byśmy chcieli bezpośrednio format DjVu wystarczy trochę zmienić zapytanie XPatha.
-- ... a tutaj te trójki przerabiamy do docelowej struktury ShadowItem
toShadowItem :: ((String, String), String) -> ShadowItem
toShadowItem ((url, articleTitle), yearlyTitle) =
(defaultShadowItem url title) {
originalDate = Just newDate,
itype = "periodical",
format = Just "zip",
finalUrl = url
}
where title = "Tajemnica Atari " ++ yearlyTitle ++ " " ++ (replace "\r\n" "" (replace "\r\n " "" articleTitle))
date = getDate url
splitDate = split "_" date
month = splitDate !! 0
year = splitDate !! 1
newDate = newFormatDate month year
getDate url =
case url =~~ "/([1-9]_9[0-9]|1[0-2]_9[0-9]|[1-9]-[1-9]_9[0-9]|1[0-2]-1[0-2]_9[0-9])/" :: Maybe [[String]] of
Just [[_, year]] -> year
otherwise -> error $ "unexpected url: " ++ url
newFormatDate :: String -> String -> String
newFormatDate month year =
if month =~ "^[1-9]$" then "19" ++ year ++ "-0" ++ month
else if month =~ "^1[0-2]$" then "19" ++ year ++ "-" ++ month
else splittedMonth month year
splittedMonth :: String -> String -> String
splittedMonth month year = do
if month =~ "^1*[0-9]-1*[0-9]$" then do
let firstMonth = split "-" month !! 0
let secondMonth = split "-" month !! 1
if firstMonth =~ "^[1-9]$" && secondMonth =~ "^[1-9]$" then "19" ++ year ++ "-0" ++ firstMonth ++ "-0" ++ secondMonth
else if firstMonth =~ "^[1-9]$" && secondMonth =~ "^1[0-9]$" then "19" ++ year ++ "-0" ++ firstMonth ++ "-" ++ secondMonth
else if firstMonth =~ "^1[1-9]$" && secondMonth =~ "^1[0-9]$" then "19" ++ year ++ "-" ++ firstMonth ++ "-" ++ secondMonth
else "wrong date"
else "wrong date"
main = do
let start = "http://krap.pl/mirrorz/atari/horror.mirage.com.pl/pixel/"
let shadowLibrary = ShadowLibrary {logoUrl=Nothing,
lname="Tajemnica Atari",
abbrev="TA",
lLevel=0,
webpage=start}
extractItemsStartingFromUrl shadowLibrary start (extractRecords >>> arr toShadowItem)