display destination file before youtube-dl download

Rather than after it, which can leave one wondering what file it's
downloading.

youtubeDl should not ever return Right Nothing in normal operation,
becaause it's already asked youtube-dl if it supports the url. So it
would have to succeed at that, then not download any file, but also
exit successfully, in order for the new error message to display.

Also display the name of yt-dlp when using it.
This commit is contained in:
Joey Hess 2023-06-20 14:55:25 -04:00
parent f811468318
commit 72715845a1
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
2 changed files with 19 additions and 11 deletions

View file

@ -12,6 +12,7 @@ module Annex.YoutubeDl (
youtubeDlCheck, youtubeDlCheck,
youtubeDlFileName, youtubeDlFileName,
youtubeDlFileNameHtmlOnly, youtubeDlFileNameHtmlOnly,
youtubeDlCommand,
) where ) where
import Annex.Common import Annex.Common
@ -74,18 +75,18 @@ youtubeDl' url workdir p uo
( runcmd cmd >>= \case ( runcmd cmd >>= \case
Right True -> downloadedfiles cmd >>= \case Right True -> downloadedfiles cmd >>= \case
(f:[]) -> return (Right (Just f)) (f:[]) -> return (Right (Just f))
[] -> return nofiles [] -> return (nofiles cmd)
fs -> return (toomanyfiles fs) fs -> return (toomanyfiles cmd fs)
Right False -> workdirfiles >>= \case Right False -> workdirfiles >>= \case
[] -> return (Right Nothing) [] -> return (Right Nothing)
_ -> return (Left "yt-dlp download is incomplete. Run the command again to resume.") _ -> return (Left $ cmd ++ " download is incomplete. Run the command again to resume.")
Left msg -> return (Left msg) Left msg -> return (Left msg)
, return (Right Nothing) , return (Right Nothing)
) )
| otherwise = return (Right Nothing) | otherwise = return (Right Nothing)
where where
nofiles = Left "yt-dlp did not put any media in its work directory, perhaps it's been configured to store files somewhere else?" nofiles cmd = Left $ cmd ++ " did not put any media in its work directory, perhaps it's been configured to store files somewhere else?"
toomanyfiles fs = Left $ "yt-dlp downloaded multiple media files; git-annex is only able to deal with one per url: " ++ show fs toomanyfiles cmd fs = Left $ cmd ++ " downloaded multiple media files; git-annex is only able to deal with one per url: " ++ show fs
downloadedfiles cmd downloadedfiles cmd
| isytdlp cmd = liftIO $ | isytdlp cmd = liftIO $
(lines <$> readFile filelistfile) (lines <$> readFile filelistfile)
@ -95,7 +96,7 @@ youtubeDl' url workdir p uo
<$> (filterM (doesFileExist) =<< dirContents workdir) <$> (filterM (doesFileExist) =<< dirContents workdir)
filelistfile = workdir </> filelistfilebase filelistfile = workdir </> filelistfilebase
filelistfilebase = "git-annex-file-list-file" filelistfilebase = "git-annex-file-list-file"
isytdlp cmd = "yt-dlp" `isInfixOf` cmd isytdlp cmd = cmd == "yt-dlp"
runcmd cmd = youtubeDlMaxSize workdir >>= \case runcmd cmd = youtubeDlMaxSize workdir >>= \case
Left msg -> return (Left msg) Left msg -> return (Left msg)
Right maxsize -> do Right maxsize -> do
@ -271,7 +272,10 @@ youtubeDlOpts addopts = do
youtubeDlCommand :: Annex String youtubeDlCommand :: Annex String
youtubeDlCommand = annexYoutubeDlCommand <$> Annex.getGitConfig >>= \case youtubeDlCommand = annexYoutubeDlCommand <$> Annex.getGitConfig >>= \case
Just c -> pure c Just c -> pure c
Nothing -> fromMaybe "youtube-dl" <$> liftIO (searchPath "yt-dlp") Nothing -> ifM (liftIO $ inSearchPath "yt-dlp")
( return "yt-dlp"
, return "youtube-dl"
)
supportedScheme :: UrlOptions -> URLString -> Bool supportedScheme :: UrlOptions -> URLString -> Bool
supportedScheme uo url = case parseURIRelaxed url of supportedScheme uo url = case parseURIRelaxed url of

View file

@ -353,21 +353,25 @@ downloadWeb addunlockedmatcher o url urlinfo file =
where where
dl dest = withTmpWorkDir mediakey $ \workdir -> do dl dest = withTmpWorkDir mediakey $ \workdir -> do
let cleanuptmp = pruneTmpWorkDirBefore tmp (liftIO . removeWhenExistsWith R.removeLink) let cleanuptmp = pruneTmpWorkDirBefore tmp (liftIO . removeWhenExistsWith R.removeLink)
showNote "using youtube-dl" dlcmd <- youtubeDlCommand
showNote ("using " <> UnquotedString dlcmd)
Transfer.notifyTransfer Transfer.Download url $ Transfer.notifyTransfer Transfer.Download url $
Transfer.download' webUUID mediakey (AssociatedFile Nothing) Nothing Transfer.noRetry $ \p -> Transfer.download' webUUID mediakey (AssociatedFile Nothing) Nothing Transfer.noRetry $ \p -> do
showDestinationFile dest
youtubeDl url (fromRawFilePath workdir) p >>= \case youtubeDl url (fromRawFilePath workdir) p >>= \case
Right (Just mediafile) -> do Right (Just mediafile) -> do
cleanuptmp cleanuptmp
checkCanAdd o dest $ \canadd -> do checkCanAdd o dest $ \canadd -> do
showDestinationFile dest
addWorkTree canadd addunlockedmatcher webUUID mediaurl dest mediakey (Just (toRawFilePath mediafile)) addWorkTree canadd addunlockedmatcher webUUID mediaurl dest mediakey (Just (toRawFilePath mediafile))
return $ Just mediakey return $ Just mediakey
Right Nothing -> checkRaw Nothing o (pure Nothing) (normalfinish tmp backend)
Left msg -> do Left msg -> do
cleanuptmp cleanuptmp
warning (UnquotedString msg) warning (UnquotedString msg)
return Nothing return Nothing
Right Nothing -> do
cleanuptmp
warning (UnquotedString dlcmd <> " did not download anything")
return Nothing
mediaurl = setDownloader url YoutubeDownloader mediaurl = setDownloader url YoutubeDownloader
mediakey = Backend.URL.fromUrl mediaurl Nothing mediakey = Backend.URL.fromUrl mediaurl Nothing
-- Does the already annexed file have the mediaurl -- Does the already annexed file have the mediaurl