public async Task DownloadCoverImageAsync(IEnumerable<MovieShort> movies, CancellationTokenSource ct)
{
var watch = Stopwatch.StartNew();
var moviesToProcess = movies.ToList();
try
{
await
moviesToProcess.ForEachAsync(
movie =>
DownloadFileHelper.DownloadFileTaskAsync(movie.MediumCoverImage,
Constants.CoverMoviesDirectory + movie.ImdbCode + Constants.ImageFileExtension, ct: ct),
(movie, t) =>
{
if (t.Item3 == null && !string.IsNullOrEmpty(t.Item2))
movie.CoverImagePath = t.Item2;
});
}
catch (Exception exception) when (exception is TaskCanceledException)
{
Logger.Debug(
"DownloadCoverImageAsync cancelled.");
}
catch (Exception exception)
{
Logger.Error(
$"DownloadCoverImageAsync: {exception.Message}");
throw;
}
finally
{
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Logger.Debug(
$"DownloadCoverImageAsync ({string.Join(";", moviesToProcess.Select(movie => movie.ImdbCode))}) in {elapsedMs} milliseconds.");
}
}