 FilePath → [Tag] → IO ()
downloadTo fp xs = doesDirectoryExist fp >>= \case
  False → print (fp <> " doesn't exist.") >> exitFailure
  True → do
    let f p = run <$> fetchAllTaggedPosts p XML xs
          where
            run s =
              fromList [ (unVAL $ (Proxy ∷ Proxy "md5") `rget` r,
                          unVAL $ (Proxy ∷ Proxy "file_url") `rget` r)
                       | Right r ← s ]

    g ← f Gelbooru
    i ← f Ichijou
    k ← f Konachan
    s ← f Safebooru
    y ← f Yandere
    let ls = g <> i <> k <> s <> y
        mkFp (m, u) = let e = snd $ splitExtension u
                      in fp </> m <.> e
    dcs ← Prelude.map (fp </>) <$> getDirectoryContents fp
    let notInFiles = (`notElem` dcs) . snd
        fs = Prelude.filter notInFiles [ (snd x, mkFp x) | x ← assocs ls ]
        lfs = show $ length fs
    ds ← atomically newTChan
    got ← newTVarIO (0 ∷ Integer)
    let loop = atomically (readTChan ds) >>= \case
          EndOfQueue → putStrLn "Done."
          x → do
            v ← atomically $ modifyTVar got succ >> readTVar got
            putStrLn (concat ["(", show v, "/", lfs, "): ", show x]) >> loop

    void . forkIO $ downloadFiles fs ds 5
    loop


-- | Fetches a map of images, with keys being the md5 and values being
-- URLs.
fetchImageLinks ∷ [Tag] → IO [Parse String]
fetchImag
downloadTo ∷ FilePath → [Tag] → IO ()
downloadTo fp xs = doesDirectoryExist fp >>= \case
  False → print (fp <> " doesn't exist.") >> exitFailure
  True → do
    let f p = run <$> fetchAllTaggedPosts p XML xs
          where
            run s =
              fromList [ (unVAL $ (Proxy ∷ Proxy "md5") `rget` r,
                          unVAL $ (Proxy ∷ Proxy "file_url") `rget` r)
                       | Right r ← s ]

    g ← f Gelbooru
    i ← f Ichijou
    k ← f Konachan
    s ← f Safebooru
    y ← f Yandere
    let ls = g <> i <> k <> s <> y
        mkFp (m, u) = let e = snd $ splitExtension u
                      in fp </> m <.> e
    dcs ← Prelude.map (fp </>) <$> getDirectoryContents fp
    let notInFiles = (`notElem` dcs) . snd
        fs = Prelude.filter notInFiles [ (snd x, mkFp x) | x ← assocs ls ]
        lfs = show $ length fs
    ds ← atomically newTChan
    got ← newTVarIO (0 ∷ Integer)
    let loop = atomically (readTChan ds) >>= \case
          EndOfQueue → putStrLn "Done."
          x → do
            v ← atomically $ modifyTVar got succ >> readTVar got
            putStrLn (concat ["(", show v, "/", lfs, "): ", show x]) >> loop

    void . forkIO $ downloadFiles fs ds 5
    loop


-- | Fetches a map of images, with keys being the md5 and values being
-- URLs.
downloadTo fp xs = doesDirectoryExist fp >>= \case
  False → print (fp <> " doesn't exist.") >> exitFailure
  True → do
    let f p = run <$> fetchAllTaggedPosts p XML xs
          where
            run s =
              fromList [ (unVAL $ (Proxy ∷ Proxy "md5") `rget` r,
                          unVAL $ (Proxy ∷ Proxy "file_url") `rget` r)
                       | Right r ← s ]

    g ← f Gelbooru
    i ← f Ichijou
    k ← f Konachan
    s ← f Safebooru
    y ← f Yandere
    let ls = g <> i <> k <> s <> y
        mkFp (m, u) = let e = snd $ splitExtension u
                      in fp </> m <.> e
    dcs ← Prelude.map (fp </>) <$> getDirectoryContents fp
    let notInFiles = (`notElem` dcs) . snd
        fs = Prelude.filter notInFiles [ (snd x, mkFp x) | x ← assocs ls ]
        lfs = show $ length fs
    ds ← atomically newTChan
    got ← newTVarIO (0 ∷ Integer)
    let loop = atomically (readTChan ds) >>= \case
          EndOfQueue → putStrLn "Done."
          x → do
            v ← atomically $ modifyTVar got succ >> readTVar got
            putStrLn (concat ["(", show v, "/", lfs, "): ", show x]) >> loop

    void . forkIO $ downloadFiles fs ds 5
    loop


-- | Fetches a map of images, with keys being the md5 and values being
-- URLs.