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,10 +1,9 @@
using Microsoft.Extensions.Logging;
using System.Collections.ObjectModel;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Logging;
namespace File_Folder_Helper.ADO2025.PI5;
internal static partial class Helper20250219 {
@ -42,10 +41,12 @@ internal static partial class Helper20250219 {
ReadOnlyCollection<int> columnIndices = args[10].Split(',').Select(int.Parse).ToArray().AsReadOnly();
foreach (string segment in segments) {
segmentsB = segment.Split('|');
if (segmentsB.Length != 2)
if (segmentsB.Length != 2) {
continue;
if (distinct.Contains(segmentsB[0]))
}
if (distinct.Contains(segmentsB[0])) {
continue;
}
distinct.Add(segmentsB[0]);
keyValuePairs.Add(segmentsB[0], segmentsB[1]);
}
@ -77,13 +78,15 @@ internal static partial class Helper20250219 {
directory = fileInfo.DirectoryName ?? throw new Exception();
directoryFiles = Directory.GetFiles(directory, searchPattern, SearchOption.TopDirectoryOnly);
matches = (from l in directoryFiles where l != fileInfo.FullName select l).ToArray();
if (matches.Length < 1)
if (matches.Length < 1) {
continue;
}
directorySegment = directory[sourceDirectoryLength..];
processDataStandardFormat = GetProcessDataStandardFormat(logger, fileInfo.LastWriteTime, pdsfMapping.NewColumnNames.Count, columnsLine, fileInfo.FullName, lines: null);
jsonElementsNew = GetArray(logger, pdsfMapping.NewColumnNames.Count, processDataStandardFormat, lookForNumbers: false);
if (jsonElementsNew is null)
if (jsonElementsNew is null) {
continue;
}
if (pdsfMapping.OldColumnNames.Count == pdsfMapping.ColumnIndices.Count) {
processDataStandardFormat = GetProcessDataStandardFormat(logger, pdsfMapping, jsonElementsNew, processDataStandardFormat);
Write(logger, fileInfo, processDataStandardFormat);
@ -132,13 +135,15 @@ internal static partial class Helper20250219 {
valueOld = jsonPropertyOld.Value.ToString();
if (processDataStandardFormatMapping.KeyValuePairs.TryGetValue(jsonPropertyOld.Name, out string? name) && !string.IsNullOrEmpty(name)) {
q = TryGetPropertyIndex(jsonPropertiesNew, name);
if (q is null && i == 0)
if (q is null && i == 0) {
unknownColumns.Add($"{jsonPropertyOld.Name}|{name}");
}
} else {
q = TryGetPropertyIndex(jsonPropertiesNew, jsonPropertyOld.Name);
if (q is null) {
if (i == 0)
if (i == 0) {
unknownColumns.Add(jsonPropertyOld.Name);
}
}
}
if (q is null) {
@ -153,8 +158,9 @@ internal static partial class Helper20250219 {
if (i == last) {
columns.Add("-1");
columnPairs.Add($"{jsonPropertyOld.Name}:");
if (!string.IsNullOrEmpty(valueOld))
if (!string.IsNullOrEmpty(valueOld)) {
logger.LogDebug("{p} )) {jsonPropertyOld.Name} ??", p, jsonPropertyOld.Name);
}
}
} else {
jsonPropertyNew = jsonPropertiesNew[q.Value];
@ -163,16 +169,19 @@ internal static partial class Helper20250219 {
columnPairs.Add($"{jsonPropertyOld.Name}:{jsonPropertyNew.Name}");
}
valueNew = jsonPropertyNew.Value.ToString();
if (i == last)
if (i == last) {
logger.LogDebug("{p} )) {jsonPropertyOld.Name} ~~ {q.Value} => {jsonPropertyNew.Name}", p, jsonPropertyOld.Name, q.Value, jsonPropertyNew.Name);
}
if (valueNew != valueOld && !differentColumns.Contains(jsonPropertyOld.Name)) {
if (valueNew.Length >= 2 && valueNew.Split(' ')[0] == valueOld)
if (valueNew.Length >= 2 && valueNew.Split(' ')[0] == valueOld) {
sameAfterSpaceSplitColumns.Add(jsonPropertyOld.Name);
else {
if (processDataStandardFormatMapping.BackfillColumns.Contains(jsonPropertyOld.Name) && i != last)
} else {
if (processDataStandardFormatMapping.BackfillColumns.Contains(jsonPropertyOld.Name) && i != last) {
continue;
if (processDataStandardFormatMapping.IndexOnlyColumns.Contains(jsonPropertyOld.Name) && int.TryParse(jsonPropertyOld.Name[^2..], out int index) && i != index - 1)
}
if (processDataStandardFormatMapping.IndexOnlyColumns.Contains(jsonPropertyOld.Name) && int.TryParse(jsonPropertyOld.Name[^2..], out int index) && i != index - 1) {
continue;
}
logger.LogWarning("For [{jsonProperty.Name}] <{directory}> doesn't match (valueNew:{valueNew} != valueOld:{valueOld})!", jsonPropertyOld.Name, directory, valueNew, valueOld);
differentColumns.Add(jsonPropertyOld.Name);
}
@ -191,19 +200,23 @@ internal static partial class Helper20250219 {
private static int? TryGetPropertyIndex(JsonProperty[] jsonProperties, string propertyName) {
int? result = null;
for (int i = 0; i < jsonProperties.Length; i++) {
if (jsonProperties[i].Name != propertyName)
if (jsonProperties[i].Name != propertyName) {
continue;
}
result = i;
break;
}
if (result is null) {
for (int i = 0; i < jsonProperties.Length; i++) {
if (jsonProperties[i].Name[0] != propertyName[0])
if (jsonProperties[i].Name[0] != propertyName[0]) {
continue;
if (jsonProperties[i].Name.Length != propertyName.Length)
}
if (jsonProperties[i].Name.Length != propertyName.Length) {
continue;
if (jsonProperties[i].Name != propertyName)
}
if (jsonProperties[i].Name != propertyName) {
continue;
}
result = i;
break;
}
@ -219,9 +232,9 @@ internal static partial class Helper20250219 {
List<string> logistics = [];
bool lookForLogistics = false;
lines ??= File.ReadAllLines(path);
if (lines.Length <= columnsLine)
if (lines.Length <= columnsLine) {
segments = [];
else {
} else {
segments = lines[columnsLine].Split('\t');
if (segments.Length != expectedColumns) {
logger.LogWarning("{segments} != {expectedColumns}", segments.Length, expectedColumns);
@ -230,24 +243,26 @@ internal static partial class Helper20250219 {
}
string[] columns = segments.Select(l => l.Trim('"')).ToArray();
for (int r = columnsLine + 1; r < lines.Length; r++) {
if (lines[r].StartsWith("NUM_DATA_ROWS"))
if (lines[r].StartsWith("NUM_DATA_ROWS")) {
lookForLogistics = true;
}
if (!lookForLogistics) {
body.Add(lines[r]);
continue;
}
if (lines[r].StartsWith("LOGISTICS_1")) {
for (int i = r; i < lines.Length; i++) {
if (lines[r].StartsWith("END_HEADER"))
if (lines[r].StartsWith("END_HEADER")) {
break;
}
logistics.Add(lines[i]);
}
break;
}
}
if (logistics.Count == 0)
if (logistics.Count == 0) {
sequence = lastWriteTime.Ticks;
else {
} else {
segments = logistics[0].Split("SEQUENCE=");
sequence = segments.Length < 2 || !long.TryParse(segments[1].Split(';')[0], out long s) ? lastWriteTime.Ticks : s;
}
@ -260,9 +275,9 @@ internal static partial class Helper20250219 {
private static JsonElement[]? GetArray(ILogger<Worker> logger, int expectedColumns, ProcessDataStandardFormat processDataStandardFormat, bool lookForNumbers) {
JsonElement[]? results;
if (processDataStandardFormat.Body.Count == 0 || !processDataStandardFormat.Body[0].Contains('\t'))
if (processDataStandardFormat.Body.Count == 0 || !processDataStandardFormat.Body[0].Contains('\t')) {
results = JsonSerializer.Deserialize("[]", JsonElementCollectionSourceGenerationContext.Default.JsonElementArray) ?? throw new Exception();
else {
} else {
string value;
string[] segments;
List<string> lines = [];
@ -283,12 +298,13 @@ internal static partial class Helper20250219 {
} else {
for (int c = 0; c < segments.Length; c++) {
value = segments[c].Replace("\\", "\\\\").Replace("\"", "\\\"");
if (string.IsNullOrEmpty(value))
if (string.IsNullOrEmpty(value)) {
_ = stringBuilder.Append('"').Append(processDataStandardFormat.Columns[c]).Append("\":").Append(value).Append("null,");
else if (value.All(char.IsDigit))
} else if (value.All(char.IsDigit)) {
_ = stringBuilder.Append('"').Append(processDataStandardFormat.Columns[c]).Append("\":").Append(value).Append(',');
else
} else {
_ = stringBuilder.Append('"').Append(processDataStandardFormat.Columns[c]).Append("\":\"").Append(value).Append("\",");
}
}
}
_ = stringBuilder.Remove(stringBuilder.Length - 1, 1);
@ -323,9 +339,9 @@ internal static partial class Helper20250219 {
}
for (int c = 0; c < processDataStandardFormatMapping.ColumnIndices.Count; c++) {
column = processDataStandardFormatMapping.ColumnIndices[c];
if (column == -1)
if (column == -1) {
value = processDataStandardFormatMapping.OldColumnNames[c];
else {
} else {
jsonProperty = jsonProperties[column];
value = jsonProperty.Value.ToString();
}
@ -342,8 +358,9 @@ internal static partial class Helper20250219 {
private static void Write(ILogger<Worker> logger, FileInfo fileInfo, ProcessDataStandardFormat processDataStandardFormat) {
List<string> results = [];
if (processDataStandardFormat.Sequence is null)
if (processDataStandardFormat.Sequence is null) {
throw new NullReferenceException(nameof(processDataStandardFormat.Sequence));
}
string endOffset = "E#######T";
string dataOffset = "D#######T";
string headerOffset = "H#######T";