{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RecordWildCards #-}
module Text.Playlist.Internal.Resolve
( resolve
) where
import Control.Monad
import Data.Text (Text)
import qualified Data.Text as Text
import Text.Playlist.Internal.Format
import Text.Playlist.Types
data Resolution = Flat Playlist | Again Playlist
resolve :: forall m. (Monad m)
=> Playlist
-> (Text -> m Playlist)
-> m Playlist
resolve :: forall (m :: * -> *).
Monad m =>
Playlist -> (Text -> m Playlist) -> m Playlist
resolve Playlist
playlist Text -> m Playlist
download = Int -> Playlist -> m Playlist
go Int
10 Playlist
playlist where
go :: Int -> Playlist -> m Playlist
go :: Int -> Playlist -> m Playlist
go Int
_ [] = Playlist -> m Playlist
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return []
go Int
0 Playlist
xs = Playlist -> m Playlist
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Playlist
xs
go Int
n Playlist
xs = ([Playlist] -> Playlist) -> m [Playlist] -> m Playlist
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Playlist] -> Playlist
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (m [Playlist] -> m Playlist) -> m [Playlist] -> m Playlist
forall a b. (a -> b) -> a -> b
$ Playlist -> (Track -> m Playlist) -> m [Playlist]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM Playlist
xs ((Track -> m Playlist) -> m [Playlist])
-> (Track -> m Playlist) -> m [Playlist]
forall a b. (a -> b) -> a -> b
$ \Track
track -> do
Resolution
r <- Track -> m Resolution
process Track
track
case Resolution
r of
Flat Playlist
p -> Playlist -> m Playlist
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Playlist
p
Again Playlist
p -> Int -> Playlist -> m Playlist
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Playlist
p
process :: Track -> m Resolution
process :: Track -> m Resolution
process t :: Track
t@Track {Maybe Float
Maybe Text
Text
trackURL :: Text
trackTitle :: Maybe Text
trackDuration :: Maybe Float
trackURL :: Track -> Text
trackTitle :: Track -> Maybe Text
trackDuration :: Track -> Maybe Float
..} =
case FilePath -> Maybe Format
fileNameToFormat (Text -> FilePath
Text.unpack Text
trackURL) of
Maybe Format
Nothing -> Resolution -> m Resolution
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Playlist -> Resolution
Flat [Track
t])
Just Format
_ -> Playlist -> Resolution
Again (Playlist -> Resolution) -> m Playlist -> m Resolution
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> m Playlist
download Text
trackURL