New method to figure number of pages
This commit is contained in:
parent
3ce3844d9d
commit
9f55b8e6be
17
Main.hs
17
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 ()
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user