Add index mode CLI
This commit is contained in:
parent
793e52814b
commit
1ada2e1166
|
@ -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>
|
||||||
|
|
|
@ -12,11 +12,18 @@ audiobooks in Ad Alta Voce library.
|
||||||
module Command.All(generateAll) where
|
module Command.All(generateAll) where
|
||||||
|
|
||||||
import Control.Monad ( join )
|
import Control.Monad ( join )
|
||||||
|
import Data.Text (unpack)
|
||||||
import Data.Maybe ( catMaybes )
|
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 Text.HTML.Scalpel ( scrapeURL, URL )
|
||||||
import Command.Single ( singleWithAuthor )
|
import Command.Single ( singleWithAuthor )
|
||||||
|
import Paths_ad_alta_voce ( getDataFileName )
|
||||||
import Scraper.Playlist
|
import Scraper.Playlist
|
||||||
( playlistPageNumbersScraper, playlistInfosScraper )
|
( playlistPageNumbersScraper, playlistInfosScraper )
|
||||||
|
import Types
|
||||||
|
|
||||||
baseUrl = "https://www.raiplayradio.it"
|
baseUrl = "https://www.raiplayradio.it"
|
||||||
playlistBaseUrl = "https://www.raiplayradio.it/programmi/adaltavoce/archivio/audiolibri/tutte/"
|
playlistBaseUrl = "https://www.raiplayradio.it/programmi/adaltavoce/archivio/audiolibri/tutte/"
|
||||||
|
@ -39,11 +46,36 @@ scrapePlaylistPages pageNumbers = do
|
||||||
concatBaseUrl :: URL -> URL
|
concatBaseUrl :: URL -> URL
|
||||||
concatBaseUrl = (++) baseUrl
|
concatBaseUrl = (++) baseUrl
|
||||||
|
|
||||||
generateAll :: String -> IO ()
|
writeIndex :: [Maybe Podcast] -> String -> String -> IO ()
|
||||||
generateAll outdir = do
|
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
|
infos <- scrapeAudiobooksUrl
|
||||||
case infos of
|
case infos of
|
||||||
Nothing -> putStrLn "Error"
|
Nothing -> putStrLn "Error"
|
||||||
Just infos' -> do
|
Just infos' -> do
|
||||||
mapM_ (\(url, author) -> singleWithAuthor url outdir author) infos'
|
podcasts <- mapM (\(url, author) -> singleWithAuthor url outdir author) infos'
|
||||||
putStrLn "All done.\nEnjoy your books!"
|
if indexMode
|
||||||
|
then writeIndex podcasts indexTemplate outdir
|
||||||
|
else putStrLn "All done.\nEnjoy your books!"
|
||||||
|
|
|
@ -16,7 +16,9 @@ import Options.Applicative.Types ( ParserInfo, Parser )
|
||||||
import Command.All ( generateAll )
|
import Command.All ( generateAll )
|
||||||
import Command.Single ( single )
|
import Command.Single ( single )
|
||||||
|
|
||||||
newtype AllOption = AllOption { outputDirectoryAll :: String}
|
data AllOption = AllOption { outputDirectoryAll :: String
|
||||||
|
, isIndexEnable :: Bool
|
||||||
|
, indexTemplate :: String }
|
||||||
|
|
||||||
data SingleOption = SingleOption { audiobookUrl :: String
|
data SingleOption = SingleOption { audiobookUrl :: String
|
||||||
, outputDirectorySingle :: String }
|
, outputDirectorySingle :: String }
|
||||||
|
@ -42,6 +44,13 @@ allParser = AllOption
|
||||||
<> help "Directory where save the podcasts"
|
<> help "Directory where save the podcasts"
|
||||||
<> value "out"
|
<> 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 :: ParserInfo Command
|
||||||
singleParserInfo = Single
|
singleParserInfo = Single
|
||||||
|
@ -64,4 +73,4 @@ commandParserInfo = info (commandParser <**> helper)
|
||||||
|
|
||||||
execute :: Command -> IO ()
|
execute :: Command -> IO ()
|
||||||
execute (Single opt) = single (audiobookUrl opt) (outputDirectorySingle opt)
|
execute (Single opt) = single (audiobookUrl opt) (outputDirectorySingle opt)
|
||||||
execute (All opt) = generateAll $ outputDirectoryAll opt
|
execute (All opt) = generateAll (outputDirectoryAll opt) (isIndexEnable opt) (indexTemplate opt)
|
||||||
|
|
|
@ -31,7 +31,7 @@ module Types
|
||||||
, audiobookEpisodes
|
, audiobookEpisodes
|
||||||
, audiobookAuthor
|
, audiobookAuthor
|
||||||
, audiobook
|
, audiobook
|
||||||
, baseUrl
|
, podcastBaseUrl
|
||||||
, pubDay
|
, pubDay
|
||||||
, generatePodcastFileName
|
, generatePodcastFileName
|
||||||
) where
|
) where
|
||||||
|
@ -75,7 +75,7 @@ toAudiobookWithAuthor (Audiobook title _ description coverUrl episodes) author =
|
||||||
-- | The 'Podcast' data type represents the podcast.
|
-- | The 'Podcast' data type represents the podcast.
|
||||||
-- 'Podcast' is an istance of 'ToMustache' typeclass.
|
-- 'Podcast' is an istance of 'ToMustache' typeclass.
|
||||||
data Podcast = Podcast { audiobook :: Audiobook
|
data Podcast = Podcast { audiobook :: Audiobook
|
||||||
, baseUrl :: String
|
, podcastBaseUrl :: String
|
||||||
, pubDay :: Day
|
, pubDay :: Day
|
||||||
}
|
}
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
@ -108,7 +108,7 @@ instance ToMustache Audiobook where
|
||||||
|
|
||||||
instance ToMustache Podcast where
|
instance ToMustache Podcast where
|
||||||
toMustache podcast = object $ [
|
toMustache podcast = object $ [
|
||||||
"base-url" ~> baseUrl podcast,
|
"base-url" ~> podcastBaseUrl podcast,
|
||||||
"pub-day" ~> show (pubDay podcast),
|
"pub-day" ~> show (pubDay podcast),
|
||||||
"audiobook-file" ~> generatePodcastFileName podcast
|
"audiobook-file" ~> generatePodcastFileName podcast
|
||||||
] ++ (toPairList (audiobook podcast))
|
] ++ (toPairList (audiobook podcast))
|
||||||
|
|
Loading…
Reference in New Issue