2021-01-24 15:26:07 +01:00
|
|
|
{-|
|
|
|
|
Module : Command.All
|
|
|
|
Description : Generate podcast for all audiobooks
|
|
|
|
Copyright : (c) Raffaele Mignone 2021
|
|
|
|
License : GPL-3
|
|
|
|
Maintainer : git@norangeb.it
|
|
|
|
|
|
|
|
This module exposes the command that generates podcast feeds for all the
|
|
|
|
audiobooks in Ad Alta Voce library.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.All(generateAll) where
|
|
|
|
|
|
|
|
import Control.Monad ( join )
|
2021-01-26 15:34:12 +01:00
|
|
|
import Data.Text (unpack)
|
2021-01-24 15:26:07 +01:00
|
|
|
import Data.Maybe ( catMaybes )
|
2021-01-26 15:34:12 +01:00
|
|
|
import Text.Mustache
|
|
|
|
import Text.Parsec.Error ( ParseError )
|
|
|
|
import System.Directory ( createDirectoryIfMissing )
|
|
|
|
import System.IO
|
2021-01-24 15:26:07 +01:00
|
|
|
import Text.HTML.Scalpel ( scrapeURL, URL )
|
2021-01-24 19:11:10 +01:00
|
|
|
import Command.Single ( singleWithAuthor )
|
2021-01-26 15:34:12 +01:00
|
|
|
import Paths_ad_alta_voce ( getDataFileName )
|
2021-01-24 15:26:07 +01:00
|
|
|
import Scraper.Playlist
|
2021-01-24 19:11:10 +01:00
|
|
|
( playlistPageNumbersScraper, playlistInfosScraper )
|
2021-01-26 15:34:12 +01:00
|
|
|
import Types
|
2021-01-24 15:26:07 +01:00
|
|
|
|
|
|
|
baseUrl = "https://www.raiplayradio.it"
|
|
|
|
playlistBaseUrl = "https://www.raiplayradio.it/programmi/adaltavoce/archivio/audiolibri/tutte/"
|
|
|
|
|
2021-01-24 19:11:10 +01:00
|
|
|
scrapeAudiobooksUrl :: IO (Maybe [(URL, String)])
|
2021-01-24 15:26:07 +01:00
|
|
|
scrapeAudiobooksUrl = do
|
|
|
|
pageNumbers <- scrapeURL playlistBaseUrl playlistPageNumbersScraper
|
|
|
|
case scrapePlaylistPages pageNumbers of
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Just urls -> Just <$> urls
|
|
|
|
|
2021-01-24 19:11:10 +01:00
|
|
|
scrapePlaylistPages :: Maybe [String] -> Maybe (IO [(URL, String)])
|
2021-01-24 15:26:07 +01:00
|
|
|
scrapePlaylistPages pageNumbers = do
|
|
|
|
pageNumbers' <- pageNumbers
|
|
|
|
let playlistUrls = map (playlistBaseUrl ++) pageNumbers'
|
2021-01-24 19:11:10 +01:00
|
|
|
audiobookInfos = mapM (`scrapeURL` playlistInfosScraper) playlistUrls
|
|
|
|
flatAudiobookInfos = join . catMaybes <$> audiobookInfos
|
|
|
|
return $ map (\(u, a) -> (concatBaseUrl u, a)) <$> flatAudiobookInfos
|
|
|
|
where
|
|
|
|
concatBaseUrl :: URL -> URL
|
|
|
|
concatBaseUrl = (++) baseUrl
|
2021-01-24 15:26:07 +01:00
|
|
|
|
2021-01-26 15:34:12 +01:00
|
|
|
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"
|
2021-01-28 15:57:48 +01:00
|
|
|
automaticCompile [".", templateDir] templateName
|
2021-01-26 15:34:12 +01:00
|
|
|
|
|
|
|
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
|
2021-01-24 19:11:10 +01:00
|
|
|
infos <- scrapeAudiobooksUrl
|
|
|
|
case infos of
|
2021-01-24 15:26:07 +01:00
|
|
|
Nothing -> putStrLn "Error"
|
2021-01-24 19:11:10 +01:00
|
|
|
Just infos' -> do
|
2021-01-26 15:34:12 +01:00
|
|
|
podcasts <- mapM (\(url, author) -> singleWithAuthor url outdir author) infos'
|
|
|
|
if indexMode
|
|
|
|
then writeIndex podcasts indexTemplate outdir
|
|
|
|
else putStrLn "All done.\nEnjoy your books!"
|