tajemnica-atari/app/tajemnicaatari.hs

70 lines
2.7 KiB
Haskell
Raw Permalink Normal View History

2021-04-06 16:38:26 +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
2021-05-01 05:27:55 +02:00
import Data.List.Utils
2021-04-07 12:10:27 +02:00
2021-04-06 16:38:26 +02:00
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) {
2021-05-01 05:27:55 +02:00
originalDate = Just newDate,
2021-04-06 16:38:26 +02:00
itype = "periodical",
format = Just "zip",
finalUrl = url
}
where title = "Tajemnica Atari " ++ yearlyTitle ++ " " ++ (replace "\r\n" "" (replace "\r\n " "" articleTitle))
date = getDate url
2021-04-07 12:10:27 +02:00
splitDate = split "_" date
2021-05-01 05:27:55 +02:00
month = splitDate !! 0
year = splitDate !! 1
newDate = newFormatDate month year
2021-04-06 16:38:26 +02:00
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
2021-05-01 05:27:55 +02:00
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"
2021-04-06 16:38:26 +02:00
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)
2021-05-01 05:27:55 +02:00