converted ImportFeed

This commit is contained in:
Joey Hess 2015-07-13 11:06:41 -04:00
parent aff4b9a3dc
commit 9c1a42873c
3 changed files with 47 additions and 36 deletions

View file

@ -84,7 +84,7 @@ import qualified Command.Sync
import qualified Command.Mirror import qualified Command.Mirror
import qualified Command.AddUrl import qualified Command.AddUrl
#ifdef WITH_FEED #ifdef WITH_FEED
--import qualified Command.ImportFeed import qualified Command.ImportFeed
#endif #endif
import qualified Command.RmUrl import qualified Command.RmUrl
--import qualified Command.Import --import qualified Command.Import
@ -133,7 +133,7 @@ cmds =
, Command.Mirror.cmd , Command.Mirror.cmd
, Command.AddUrl.cmd , Command.AddUrl.cmd
#ifdef WITH_FEED #ifdef WITH_FEED
-- , Command.ImportFeed.cmd , Command.ImportFeed.cmd
#endif #endif
, Command.RmUrl.cmd , Command.RmUrl.cmd
-- , Command.Import.cmd -- , Command.Import.cmd

View file

@ -61,11 +61,17 @@ optParser desc = AddUrlOptions
( long "pathdepth" <> metavar paramNumber ( long "pathdepth" <> metavar paramNumber
<> help "path components to use in filename" <> help "path components to use in filename"
)) ))
<*> switch <*> parseRelaxedOption
<*> parseRawOption
parseRelaxedOption :: Parser Bool
parseRelaxedOption = switch
( long "relaxed" ( long "relaxed"
<> help "skip size check" <> help "skip size check"
) )
<*> switch
parseRawOption :: Parser Bool
parseRawOption = switch
( long "raw" ( long "raw"
<> help "disable special handling for torrents, quvi, etc" <> help "disable special handling for torrents, quvi, etc"
) )

View file

@ -30,7 +30,7 @@ import Types.UrlContents
import Logs.Web import Logs.Web
import qualified Utility.Format import qualified Utility.Format
import Utility.Tmp import Utility.Tmp
import Command.AddUrl (addUrlFile, downloadRemoteFile, relaxedOption, rawOption) import Command.AddUrl (addUrlFile, downloadRemoteFile, parseRelaxedOption, parseRawOption)
import Annex.Perms import Annex.Perms
import Annex.UUID import Annex.UUID
import Backend.URL (fromUrl) import Backend.URL (fromUrl)
@ -44,33 +44,38 @@ import Logs.MetaData
import Annex.MetaData import Annex.MetaData
cmd :: Command cmd :: Command
cmd = notBareRepo $ withOptions [templateOption, relaxedOption, rawOption] $ cmd = notBareRepo $
command "importfeed" SectionCommon "import files from podcast feeds" command "importfeed" SectionCommon "import files from podcast feeds"
(paramRepeating paramUrl) (withParams seek) (paramRepeating paramUrl) (seek <$$> optParser)
templateOption :: Option data ImportFeedOptions = ImportFeedOptions
templateOption = fieldOption [] "template" paramFormat "template for filenames" { feedUrls :: CmdParams
, templateOption :: Maybe String
seek :: CmdParams -> CommandSeek , relaxedOption :: Bool
seek ps = do , rawOption :: Bool
tmpl <- getOptionField templateOption return
relaxed <- getOptionFlag relaxedOption
raw <- getOptionFlag rawOption
let opts = Opts { relaxedOpt = relaxed, rawOpt = raw }
cache <- getCache tmpl
withStrings (start opts cache) ps
data Opts = Opts
{ relaxedOpt :: Bool
, rawOpt :: Bool
} }
start :: Opts -> Cache -> URLString -> CommandStart optParser :: CmdParamsDesc -> Parser ImportFeedOptions
optParser desc = ImportFeedOptions
<$> cmdParams desc
<*> optional (strOption
( long "template" <> metavar paramFormat
<> help "template for filenames"
))
<*> parseRelaxedOption
<*> parseRawOption
seek :: ImportFeedOptions -> CommandSeek
seek o = do
cache <- getCache (templateOption o)
withStrings (start o cache) (feedUrls o)
start :: ImportFeedOptions -> Cache -> URLString -> CommandStart
start opts cache url = do start opts cache url = do
showStart "importfeed" url showStart "importfeed" url
next $ perform opts cache url next $ perform opts cache url
perform :: Opts -> Cache -> URLString -> CommandPerform perform :: ImportFeedOptions -> Cache -> URLString -> CommandPerform
perform opts cache url = do perform opts cache url = do
v <- findDownloads url v <- findDownloads url
case v of case v of
@ -160,15 +165,15 @@ downloadFeed url
, return Nothing , return Nothing
) )
performDownload :: Opts -> Cache -> ToDownload -> Annex Bool performDownload :: ImportFeedOptions -> Cache -> ToDownload -> Annex Bool
performDownload opts cache todownload = case location todownload of performDownload opts cache todownload = case location todownload of
Enclosure url -> checkknown url $ Enclosure url -> checkknown url $
rundownload url (takeExtension url) $ \f -> do rundownload url (takeExtension url) $ \f -> do
r <- Remote.claimingUrl url r <- Remote.claimingUrl url
if Remote.uuid r == webUUID || rawOpt opts if Remote.uuid r == webUUID || rawOption opts
then do then do
urlinfo <- Url.withUrlOptions (Url.getUrlInfo url) urlinfo <- Url.withUrlOptions (Url.getUrlInfo url)
maybeToList <$> addUrlFile (relaxedOpt opts) url urlinfo f maybeToList <$> addUrlFile (relaxedOption opts) url urlinfo f
else do else do
res <- tryNonAsync $ maybe res <- tryNonAsync $ maybe
(error $ "unable to checkUrl of " ++ Remote.name r) (error $ "unable to checkUrl of " ++ Remote.name r)
@ -178,10 +183,10 @@ performDownload opts cache todownload = case location todownload of
Left _ -> return [] Left _ -> return []
Right (UrlContents sz _) -> Right (UrlContents sz _) ->
maybeToList <$> maybeToList <$>
downloadRemoteFile r (relaxedOpt opts) url f sz downloadRemoteFile r (relaxedOption opts) url f sz
Right (UrlMulti l) -> do Right (UrlMulti l) -> do
kl <- forM l $ \(url', sz, subf) -> kl <- forM l $ \(url', sz, subf) ->
downloadRemoteFile r (relaxedOpt opts) url' (f </> fromSafeFilePath subf) sz downloadRemoteFile r (relaxedOption opts) url' (f </> fromSafeFilePath subf) sz
return $ if all isJust kl return $ if all isJust kl
then catMaybes kl then catMaybes kl
else [] else []
@ -199,7 +204,7 @@ performDownload opts cache todownload = case location todownload of
let videourl = Quvi.linkUrl link let videourl = Quvi.linkUrl link
checkknown videourl $ checkknown videourl $
rundownload videourl ("." ++ fromMaybe "m" (Quvi.linkSuffix link)) $ \f -> rundownload videourl ("." ++ fromMaybe "m" (Quvi.linkSuffix link)) $ \f ->
maybeToList <$> addUrlFileQuvi (relaxedOpt opts) quviurl videourl f maybeToList <$> addUrlFileQuvi (relaxedOption opts) quviurl videourl f
#else #else
return False return False
#endif #endif