Add index mode CLI

This commit is contained in:
Raffaele Mignone 2021-01-26 15:34:12 +01:00
parent 793e52814b
commit 1ada2e1166
Signed by: norangebit
GPG Key ID: F5255658CB220573
4 changed files with 64 additions and 10 deletions

View File

@ -0,0 +1,13 @@
<html>
<head>
<title>Ad alta voce - Podcast non ufficiale</title>
<meta charset="utf-8" />
</head>
<h1>Lista audiobook</h1>
<ul>
{{#entries}}
<li><a href='{{audiobook-file}}'>{{audiobook-title}}</a> {{audiobook-author}}</li>
{{/entries}}
</ul>
</html>

View File

@ -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!"

View File

@ -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)

View File

@ -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))