130 lines
4.5 KiB
C#
130 lines
4.5 KiB
C#
using dbsync.Config;
|
|
using Serilog;
|
|
using TvSyncApp;
|
|
|
|
namespace dbsync.Services
|
|
{
|
|
public class Loader
|
|
{
|
|
private readonly Downloader _downloader;
|
|
|
|
public Loader()
|
|
{
|
|
_downloader = new Downloader();
|
|
}
|
|
|
|
public async Task<(int Success, int Failed)> LoadJsonAsync(
|
|
Dictionary<string, string> directories,
|
|
List<string> updateList,
|
|
Dictionary<string, long> updateDict,
|
|
Settings settings)
|
|
|
|
{
|
|
int success = 0;
|
|
int failed = 0;
|
|
int completed = 0;
|
|
int maxParallelDownloads = settings.Global!.MaxParallelDownloads!.Value;
|
|
|
|
var semaphore = new System.Threading.SemaphoreSlim(maxParallelDownloads);
|
|
|
|
var filesNeeded = new List<(string Directory, string FileName)>();
|
|
|
|
foreach (var seriesId in updateList)
|
|
{
|
|
foreach (var dir in directories.Values)
|
|
{
|
|
string filePath = Path.Combine(dir, $"{seriesId}.json");
|
|
if (!File.Exists(filePath))
|
|
filesNeeded.Add((dir, $"{seriesId}.json"));
|
|
}
|
|
}
|
|
|
|
Log.Information("Downloading {Count} files...", filesNeeded.Count);
|
|
var totalFiles = filesNeeded.Count;
|
|
var progress = new Progress<int>(value =>
|
|
{
|
|
completed += value;
|
|
Console.Write($"\rDownloading files: {completed}/{totalFiles}");
|
|
});
|
|
|
|
var tasks = new List<Task>();
|
|
foreach (var file in filesNeeded)
|
|
{
|
|
tasks.Add(DownloadWithThrottleAsync(
|
|
file.Directory,
|
|
file.FileName,
|
|
directories,
|
|
settings,
|
|
semaphore,
|
|
progress));
|
|
|
|
}
|
|
|
|
await Task.WhenAll(tasks);
|
|
Console.WriteLine("\nDownload complete.");
|
|
|
|
foreach (var file in filesNeeded)
|
|
{
|
|
string path = Path.Combine(file.Directory, file.FileName);
|
|
if (File.Exists(path)) success++;
|
|
else failed++;
|
|
}
|
|
|
|
return (success, failed);
|
|
}
|
|
|
|
private async Task DownloadWithThrottleAsync(
|
|
string directory,
|
|
string fileName,
|
|
Dictionary<string, string> directories,
|
|
Settings settings,
|
|
SemaphoreSlim semaphore,
|
|
IProgress<int> progress)
|
|
|
|
{
|
|
List<string> episodeIds = new List<string>();
|
|
await semaphore.WaitAsync();
|
|
try
|
|
{
|
|
string seriesId = Path.GetFileNameWithoutExtension(fileName);
|
|
string url;
|
|
|
|
if (directory.Contains("series", StringComparison.OrdinalIgnoreCase))
|
|
url = settings.Json!.ShowApi!.Replace("<tvmazeid>", seriesId);
|
|
else if (directory.Contains("episodes", StringComparison.OrdinalIgnoreCase))
|
|
url = settings.Json!.EpisodeApi!.Replace("<tvmazeid>", seriesId);
|
|
else if (directory.Contains("cast", StringComparison.OrdinalIgnoreCase))
|
|
url = settings.Json!.CastApi!.Replace("<tvmazeid>", seriesId);
|
|
else if (directory.Contains("crew", StringComparison.OrdinalIgnoreCase))
|
|
url = settings.Json!.CrewApi!.Replace("<tvmazeid>", seriesId);
|
|
else
|
|
url = $"https://api.tvmaze.com/shows/{seriesId}";
|
|
|
|
// Download the file
|
|
var epids = await Task.Run(() =>
|
|
_downloader.DownloadAsync(url, directory, fileName, Log.Logger));
|
|
|
|
if (epids.Count > 0)
|
|
{
|
|
// await _downloader.DownloadEpisodeExtrasAsync(
|
|
// epids,
|
|
// directories,
|
|
// Log.Logger);
|
|
}
|
|
|
|
// Report progress
|
|
progress?.Report(1);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Failed to download {FileName}", fileName);
|
|
progress?.Report(1); // still report so progress moves forward
|
|
}
|
|
finally
|
|
{
|
|
semaphore.Release();
|
|
}
|
|
}
|
|
}
|
|
}
|