diff --git a/data/templates/index.html.mustache b/data/templates/index.html.mustache
new file mode 100644
index 0000000..d58ab0d
--- /dev/null
+++ b/data/templates/index.html.mustache
@@ -0,0 +1,13 @@
+
+
+ Ad alta voce - Podcast non ufficiale
+
+
+ Lista audiobook
+
+
+
diff --git a/src/Command/All.hs b/src/Command/All.hs
index 052549b..db16add 100644
--- a/src/Command/All.hs
+++ b/src/Command/All.hs
@@ -12,11 +12,18 @@ audiobooks in Ad Alta Voce library.
module Command.All(generateAll) where
import Control.Monad ( join )
+import Data.Text (unpack)
import Data.Maybe ( catMaybes )
+import Text.Mustache
+import Text.Parsec.Error ( ParseError )
+import System.Directory ( createDirectoryIfMissing )
+import System.IO
import Text.HTML.Scalpel ( scrapeURL, URL )
import Command.Single ( singleWithAuthor )
+import Paths_ad_alta_voce ( getDataFileName )
import Scraper.Playlist
( playlistPageNumbersScraper, playlistInfosScraper )
+import Types
baseUrl = "https://www.raiplayradio.it"
playlistBaseUrl = "https://www.raiplayradio.it/programmi/adaltavoce/archivio/audiolibri/tutte/"
@@ -39,11 +46,36 @@ scrapePlaylistPages pageNumbers = do
concatBaseUrl :: URL -> URL
concatBaseUrl = (++) baseUrl
-generateAll :: String -> IO ()
-generateAll outdir = do
+writeIndex :: [Maybe Podcast] -> String -> String -> IO ()
+writeIndex podcasts templateName outdir = do
+ let index = Index $ catMaybes podcasts
+ template <- compileIndexTemplate templateName
+ writeIndexTemplate template index outputFile outdir
+ where
+ outputFile = outdir ++ "/" ++ take (length templateName - 9) templateName
+
+compileIndexTemplate :: String -> IO (Either ParseError Template)
+compileIndexTemplate templateName = do
+ templateDir <- getDataFileName "templates"
+ automaticCompile [templateDir, "."] templateName
+
+writeIndexTemplate :: Either ParseError Template -> Index -> String -> String -> IO ()
+writeIndexTemplate (Left err) _ _ _ = print err
+writeIndexTemplate (Right template) index fileName outdir = do
+ createDirectoryIfMissing True outdir
+ withFile fileName WriteMode (\handle -> do
+ hPutStr handle $ unpack indexContent
+ putStrLn "Index done!\nAll done, enjoy your books!")
+ where
+ indexContent = substitute template index
+
+generateAll :: String -> Bool -> String -> IO ()
+generateAll outdir indexMode indexTemplate = do
infos <- scrapeAudiobooksUrl
case infos of
Nothing -> putStrLn "Error"
Just infos' -> do
- mapM_ (\(url, author) -> singleWithAuthor url outdir author) infos'
- putStrLn "All done.\nEnjoy your books!"
+ podcasts <- mapM (\(url, author) -> singleWithAuthor url outdir author) infos'
+ if indexMode
+ then writeIndex podcasts indexTemplate outdir
+ else putStrLn "All done.\nEnjoy your books!"
diff --git a/src/Command/CLI.hs b/src/Command/CLI.hs
index ef2e6f9..07fa0fa 100644
--- a/src/Command/CLI.hs
+++ b/src/Command/CLI.hs
@@ -16,7 +16,9 @@ import Options.Applicative.Types ( ParserInfo, Parser )
import Command.All ( generateAll )
import Command.Single ( single )
-newtype AllOption = AllOption { outputDirectoryAll :: String}
+data AllOption = AllOption { outputDirectoryAll :: String
+ , isIndexEnable :: Bool
+ , indexTemplate :: String }
data SingleOption = SingleOption { audiobookUrl :: String
, outputDirectorySingle :: String }
@@ -41,7 +43,14 @@ allParser = AllOption
<> metavar "DIRECTORY"
<> help "Directory where save the podcasts"
<> value "out"
- <> showDefault)
+ <> showDefault )
+ <*> switch (long "index"
+ <> short 'i' )
+ <*> strOption (long "index-template"
+ <> short 't'
+ <> metavar "TEMPLATE"
+ <> value "index.html.mustache"
+ <> showDefault )
singleParserInfo :: ParserInfo Command
singleParserInfo = Single
@@ -64,4 +73,4 @@ commandParserInfo = info (commandParser <**> helper)
execute :: Command -> IO ()
execute (Single opt) = single (audiobookUrl opt) (outputDirectorySingle opt)
-execute (All opt) = generateAll $ outputDirectoryAll opt
+execute (All opt) = generateAll (outputDirectoryAll opt) (isIndexEnable opt) (indexTemplate opt)
diff --git a/src/Types.hs b/src/Types.hs
index bb3731c..790cba4 100644
--- a/src/Types.hs
+++ b/src/Types.hs
@@ -31,7 +31,7 @@ module Types
, audiobookEpisodes
, audiobookAuthor
, audiobook
- , baseUrl
+ , podcastBaseUrl
, pubDay
, generatePodcastFileName
) where
@@ -75,7 +75,7 @@ toAudiobookWithAuthor (Audiobook title _ description coverUrl episodes) author =
-- | The 'Podcast' data type represents the podcast.
-- 'Podcast' is an istance of 'ToMustache' typeclass.
data Podcast = Podcast { audiobook :: Audiobook
- , baseUrl :: String
+ , podcastBaseUrl :: String
, pubDay :: Day
}
deriving (Show)
@@ -108,7 +108,7 @@ instance ToMustache Audiobook where
instance ToMustache Podcast where
toMustache podcast = object $ [
- "base-url" ~> baseUrl podcast,
+ "base-url" ~> podcastBaseUrl podcast,
"pub-day" ~> show (pubDay podcast),
"audiobook-file" ~> generatePodcastFileName podcast
] ++ (toPairList (audiobook podcast))