public async Task DownloadSubtitleAsync(MovieFull movie, IProgress<long> progress, CancellationTokenSource ct)
{
if (movie.SelectedSubtitle == null)
return;
var watch = Stopwatch.StartNew();
var filePath = Constants.Subtitles + movie.ImdbCode + "\\" + movie.SelectedSubtitle.Language.EnglishName +
".zip";
try
{
var result = await
DownloadFileHelper.DownloadFileTaskAsync(
Constants.YifySubtitles + movie.SelectedSubtitle.Url, filePath, progress: progress, ct: ct);
if (result.Item3 == null && !string.IsNullOrEmpty(result.Item2))
{
using (var archive = ZipFile.OpenRead(result.Item2))
{
foreach (var entry in archive.Entries)
{
if (entry.FullName.StartsWith("_") ||
!entry.FullName.EndsWith(".srt", StringComparison.OrdinalIgnoreCase)) continue;
var subtitlePath = Path.Combine(Constants.Subtitles + movie.ImdbCode,
entry.FullName);
if (!File.Exists(subtitlePath))
entry.ExtractToFile(subtitlePath);
movie.SelectedSubtitle.FilePath = subtitlePath;
}
}
}
}
catch (Exception exception) when (exception is TaskCanceledException)
{
Logger.Debug(
"DownloadSubtitleAsync cancelled.");
}
catch (Exception exception)
{
Logger.Error(
$"DownloadSubtitleAsync: {exception.Message}");
throw;
}
finally
{
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Logger.Debug(
$"DownloadSubtitleAsync ({movie.ImdbCode}) in {elapsedMs} milliseconds.");
}
}