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
|
cache = do
|
||||||
path <- lookupEnv "XDG_CACHE_HOME"
|
path <- lookupEnv "XDG_CACHE_HOME"
|
||||||
case path of
|
case path of
|
||||||
Just x -> return (x ++ "/oglaf")
|
Just x -> return (x <> "/oglaf")
|
||||||
Nothing -> (++ "/.cache/oglaf") <$> getEnv "HOME"
|
Nothing -> (<> "/.cache/oglaf") <$> getEnv "HOME"
|
||||||
|
|
||||||
|
|
||||||
lastComic :: IO Text
|
lastComic :: IO Text
|
||||||
@ -40,7 +40,7 @@ lastComic = do
|
|||||||
res <- try (T.readFile =<< cache)
|
res <- try (T.readFile =<< cache)
|
||||||
case res of
|
case res of
|
||||||
Left (err :: IOError) -> do
|
Left (err :: IOError) -> do
|
||||||
putStrLn ("Can't read saved state " ++ show err)
|
putStrLn ("Can't read saved state " <> show err)
|
||||||
putStrLn "Send anyway"
|
putStrLn "Send anyway"
|
||||||
return ""
|
return ""
|
||||||
Right title -> return title
|
Right title -> return title
|
||||||
@ -53,12 +53,11 @@ setLastComic title = cache >>= flip T.writeFile title
|
|||||||
findPages :: Text -> IO [Comic]
|
findPages :: Text -> IO [Comic]
|
||||||
findPages = findPages' 1 where
|
findPages = findPages' 1 where
|
||||||
findPages' n link = do
|
findPages' n link = do
|
||||||
res <- try $ simpleHttp (url <> page)
|
let page = if n>1 then show n else ""
|
||||||
case res of
|
res <- simpleHttp (url <> (unpack link <> page))
|
||||||
Left (_ :: HttpException) -> return []
|
if isLastPage res
|
||||||
Right cur -> (parseComic cur n :) <$> findPages' (succ n) link
|
then return [parseComic res n]
|
||||||
where
|
else (parseComic res n :) <$> findPages' (succ n) link
|
||||||
page = unpack link <> show n
|
|
||||||
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
|
@ -40,6 +40,15 @@ parseComic str n = Comic
|
|||||||
cur = mkCursor str
|
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 :: ByteString -> Cursor
|
||||||
mkCursor = fromDocument . parseLBS
|
mkCursor = fromDocument . parseLBS
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user