106 lines
4.2 KiB
C#
106 lines
4.2 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Npgsql;
|
|
using Serilog;
|
|
|
|
namespace TvSyncApp
|
|
{
|
|
public class Program
|
|
{
|
|
public static async Task<int> Main(string[] args)
|
|
{
|
|
string settingsPath = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");
|
|
var settings = TvSyncApp.Settings.Load(settingsPath)
|
|
?? throw new Exception("Failed to load settings from appsettings.json");
|
|
|
|
Console.WriteLine($"Log level: {settings.Global.LogLevel}");
|
|
Console.WriteLine($"Database type: {settings.Database.DbType}");
|
|
// Console.WriteLine($"TVMaze series API: {settings.Json.SeriesApi}");
|
|
|
|
string rootDir = Directory.GetCurrentDirectory();
|
|
|
|
var directories = new Dictionary<string, string>
|
|
{
|
|
["SERIESDIR"] = Path.Combine(rootDir, "cache", "series"),
|
|
["EPISODEDIR"] = Path.Combine(rootDir, "cache", "episodes"),
|
|
["CASTDIR"] = Path.Combine(rootDir, "cache", "cast"),
|
|
// ["GUESTCASTDIR"] = Path.Combine(rootDir, "cache", "guestcast"),
|
|
["CREWDIR"] = Path.Combine(rootDir, "cache", "crew"),
|
|
["UNKNOWN"] = Path.Combine(rootDir, "cache", "unknown")
|
|
};
|
|
|
|
CreateDirectories(rootDir);
|
|
|
|
Log.Logger = new LoggerConfiguration()
|
|
.MinimumLevel.Debug()
|
|
.WriteTo.Console()
|
|
.WriteTo.File(Path.Combine(rootDir, "log", "sync.log"))
|
|
.CreateLogger();
|
|
|
|
Log.Information("Starting sync process...");
|
|
|
|
// --- Fetch series IDs from PostgreSQL ---
|
|
string connectionString = $"Host={settings.DbSettings.Hostname};Port={settings.DbSettings.PgsqlPort};Username={settings.DbSettings.PgsqlUsername};Password={settings.DbSettings.PgsqlPassword};Database={settings.DbSettings.DbName}";
|
|
List<int> seriesIds = GetSeriesIdsFromDb(connectionString);
|
|
|
|
Log.Information("Fetched {Count} series IDs from tvupdates table.", seriesIds.Count);
|
|
|
|
// Convert to string list if your Loader expects strings
|
|
List<string> updateList = seriesIds.ConvertAll(id => id.ToString());
|
|
|
|
// Optionally, you can also fetch timestamps if needed
|
|
Dictionary<string, long> updateDict = new Dictionary<string, long>();
|
|
|
|
var loader = new Loader();
|
|
|
|
var result = await loader.LoadJsonAsync(directories, updateList, updateDict, settings);
|
|
|
|
Log.Information("Sync complete. Success: {Success}, Failed: {Failed}", result.Success, result.Failed);
|
|
Log.CloseAndFlush();
|
|
|
|
return 0;
|
|
}
|
|
|
|
private static List<int> GetSeriesIdsFromDb(string connectionString)
|
|
{
|
|
var seriesIds = new List<int>();
|
|
|
|
try
|
|
{
|
|
using var conn = new NpgsqlConnection(connectionString);
|
|
conn.Open();
|
|
|
|
string query = "SELECT seriesid FROM updates.tvupdates ORDER BY seriesid";
|
|
|
|
using var cmd = new NpgsqlCommand(query, conn);
|
|
using var reader = cmd.ExecuteReader();
|
|
|
|
while (reader.Read())
|
|
{
|
|
if (!reader.IsDBNull(0))
|
|
seriesIds.Add(reader.GetInt32(0));
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Failed to fetch series IDs from PostgreSQL.");
|
|
}
|
|
|
|
return seriesIds;
|
|
}
|
|
|
|
private static void CreateDirectories(string rootDir)
|
|
{
|
|
Directory.CreateDirectory(Path.Combine(rootDir, "log"));
|
|
Directory.CreateDirectory(Path.Combine(rootDir, "temp"));
|
|
Directory.CreateDirectory(Path.Combine(rootDir, "cache", "series"));
|
|
Directory.CreateDirectory(Path.Combine(rootDir, "cache", "episodes"));
|
|
Directory.CreateDirectory(Path.Combine(rootDir, "cache", "cast"));
|
|
Directory.CreateDirectory(Path.Combine(rootDir, "cache", "guestcast"));
|
|
Directory.CreateDirectory(Path.Combine(rootDir, "cache", "crew"));
|
|
}
|
|
}
|
|
}
|