person-key-to-immich-import birthday json (Day-Helper-2024-05-18)

csharp_prefer_braces = true
This commit is contained in:
2025-09-06 11:16:55 -07:00
parent 8ec89953bc
commit 6102da7266
54 changed files with 2218 additions and 1721 deletions

View File

@ -1,35 +1,58 @@
using File_Folder_Helper.Models;
using Microsoft.Extensions.Logging;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace File_Folder_Helper.ADO2024.PI2;
internal static partial class Helper20240518
{
internal static partial class Helper20240518 {
internal static void PersonKeyToImmichImport(ILogger<Worker> logger, List<string> args)
{
internal static void PersonKeyToImmichImport(ILogger<Worker> logger, List<string> args) {
string json;
string name;
Person person;
string birthDate;
string ownerId = args[5];
List<string> inserts = [];
string tableName = args[3];
string birthDateFormat = args[6];
string[] columns = args[4].Split(',');
string json = File.ReadAllText(args[2]);
string root = Path.GetFullPath(args[0]);
Dictionary<long, Person> people = JsonSerializer.Deserialize(json, PeopleSourceGenerationContext.Default.DictionaryInt64Person) ?? throw new NullReferenceException();
foreach (KeyValuePair<long, Person> keyValuePair in people)
{
if (keyValuePair.Value.Birth?.Note is null || keyValuePair.Value.Name?.ForwardSlashFull is null || keyValuePair.Value.Birth?.Date is null)
Dictionary<string, string> keyValuePairs = [];
json = File.ReadAllText(Path.Combine(root, args[2]));
DateOnly minimumBirthDate = DateOnly.ParseExact(args[7], birthDateFormat, CultureInfo.InvariantCulture);
Dictionary<long, Person> people = JsonSerializer.Deserialize(json, PeopleSourceGenerationContext.Default.DictionaryInt64Person) ??
throw new NullReferenceException();
foreach (KeyValuePair<long, Person> keyValuePair in people) {
person = keyValuePair.Value;
if (string.IsNullOrEmpty(person.Birth?.Note) || string.IsNullOrEmpty(person.Name?.ForwardSlashFull) || person.Birth.Date < minimumBirthDate) {
continue;
birthDate = keyValuePair.Value.Birth.Date.Value.ToString(birthDateFormat);
name = keyValuePair.Value.Name.ForwardSlashFull.Replace("/", string.Empty);
}
name = string.IsNullOrEmpty(person.Name.Suffix) ? person.Name.ForwardSlashFull : $"{person.Name.ForwardSlashFull} {person.Name.Suffix}";
keyValuePairs.Add(person.Birth.Note, name);
}
json = JsonSerializer.Serialize(keyValuePairs, DictionaryStringStringSourceGenerationContext.Default.DictionaryStringString);
string jsonFile = Path.Combine(root, $"{DateTime.Now.Ticks}.json");
logger.LogInformation("<{file}> saved", jsonFile);
File.WriteAllText(jsonFile, json);
foreach (KeyValuePair<long, Person> keyValuePair in people) {
person = keyValuePair.Value;
if (string.IsNullOrEmpty(person.Birth?.Note) || string.IsNullOrEmpty(person.Name?.ForwardSlashFull) || person.Birth?.Date is null) {
continue;
}
birthDate = person.Birth.Date.Value.ToString(birthDateFormat);
name = person.Name.ForwardSlashFull.Replace("/", string.Empty);
inserts.Add($"insert into \"{tableName}\" (\"{string.Join("\", \"", columns)}\") values ('{ownerId}', '{name}', '{birthDate}');");
}
string file = Path.Combine(root, $"{DateTime.Now.Ticks}.sql");
logger.LogInformation("<{file}> saved", file);
File.WriteAllLines(file, inserts);
string sqlFile = Path.Combine(root, $"{DateTime.Now.Ticks}.sql");
logger.LogInformation("<{file}> saved", sqlFile);
File.WriteAllLines(sqlFile, inserts);
}
}
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Dictionary<string, string>))]
internal partial class DictionaryStringStringSourceGenerationContext : JsonSerializerContext {
}