From 9f55b8e6bea13b731854f78297607a418fc278f1 Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Sat, 17 Dec 2016 15:15:03 +0100 Subject: [PATCH] New method to figure number of pages --- Main.hs | 17 ++++++++--------- Parser.hs | 9 +++++++++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Main.hs b/Main.hs index 6123096..96c220a 100755 --- a/Main.hs +++ b/Main.hs @@ -31,8 +31,8 @@ cache :: IO String cache = do path <- lookupEnv "XDG_CACHE_HOME" case path of - Just x -> return (x ++ "/oglaf") - Nothing -> (++ "/.cache/oglaf") <$> getEnv "HOME" + Just x -> return (x <> "/oglaf") + Nothing -> (<> "/.cache/oglaf") <$> getEnv "HOME" lastComic :: IO Text @@ -40,7 +40,7 @@ lastComic = do res <- try (T.readFile =<< cache) case res of Left (err :: IOError) -> do - putStrLn ("Can't read saved state " ++ show err) + putStrLn ("Can't read saved state " <> show err) putStrLn "Send anyway" return "" Right title -> return title @@ -53,12 +53,11 @@ setLastComic title = cache >>= flip T.writeFile title findPages :: Text -> IO [Comic] findPages = findPages' 1 where findPages' n link = do - res <- try $ simpleHttp (url <> page) - case res of - Left (_ :: HttpException) -> return [] - Right cur -> (parseComic cur n :) <$> findPages' (succ n) link - where - page = unpack link <> show n + let page = if n>1 then show n else "" + res <- simpleHttp (url <> (unpack link <> page)) + if isLastPage res + then return [parseComic res n] + else (parseComic res n :) <$> findPages' (succ n) link main :: IO () diff --git a/Parser.hs b/Parser.hs index bc57f89..6574294 100644 --- a/Parser.hs +++ b/Parser.hs @@ -40,6 +40,15 @@ parseComic str n = Comic cur = mkCursor str +isLastPage :: ByteString -> Bool +isLastPage str = (cur $// page &| link) == (cur $// story &| link) + where + cur = mkCursor str + page = element "div" >=> attributeIs "id" "nx" >=> parent + story = element "div" >=> attributeIs "id" "ns" >=> parent + link = T.concat . attribute "href" + + mkCursor :: ByteString -> Cursor mkCursor = fromDocument . parseLBS