New method to figure number of pages

This commit is contained in:
rnhmjoj 2016-12-17 15:15:03 +01:00
parent 3ce3844d9d
commit 9f55b8e6be
No known key found for this signature in database
GPG Key ID: 362BB82B7E496B7C
2 changed files with 17 additions and 9 deletions

17
Main.hs
View File

@ -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 ()

View File

@ -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