Skip to content

Commit e9d7ede

Browse files
Save tag data to a local JSON file (#93)
* Add LastWriteTime to cached data * Add album artists * Add IMediaData interface * Add TagSummary class * Remove setters from interface * Add default value to TagSummary members * Don't unescape JSON (as it causes errors for me in another tool) * Remove IMediaFile; etc. * Remove TagSummary record type; add path info to JSON * Move title to top of the confirm-tag dialog * Fix text * Remove comment
1 parent 72faf3d commit e9d7ede

3 files changed

Lines changed: 26 additions & 34 deletions

File tree

src/AudioTagger.Console/OperationLibrary.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,14 @@ internal static class OperationLibrary
8686
["-p", "--parse"],
8787
"Get a single tag value by parsing the data of another (generally Comments).",
8888
new TagParser()),
89+
new(
90+
["--cache-tags"],
91+
"Cache files' tag data to a local JSON file whose path is specified in the settings.",
92+
new TagCacher()),
8993
new(
9094
["--scan"],
9195
"Ad-hoc maintenance scanning work. (Not intended for normal use.)",
9296
new TagScanner(),
93-
isHidden: true),
94-
new(
95-
["--cache-tags"],
96-
"Cache files' tag data locally to a JSON file whose path is specified in the settings. (Eventually, this will be helpful in speeding up certain operations.)",
97-
new TagCacher(),
9897
isHidden: true)
9998
];
10099

src/AudioTagger.Console/Operations/TagCacher.cs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@ namespace AudioTagger.Console.Operations;
77

88
public sealed class TagCacher : IPathOperation
99
{
10-
private record TagSummary(
11-
string[] Artists,
12-
string Album,
13-
uint TrackNo,
14-
string Title,
15-
uint Year,
16-
string[] Genres,
17-
TimeSpan Duration
18-
);
19-
2010
public void Start(
2111
IReadOnlyCollection<MediaFile> mediaFiles,
2212
DirectoryInfo workingDirectory,
@@ -25,35 +15,38 @@ public void Start(
2515
{
2616
if (settings.TagCacheFilePath is null)
2717
{
28-
printer.Error("You must specify the save file path in the settings file.");
18+
printer.Error("You must specify the save file path in the settings.");
2919
return;
3020
}
3121

3222
Watch watch = new();
3323

34-
var summaries = mediaFiles.Select(m => {
35-
return new TagSummary(
36-
m.Artists,
37-
m.Album,
38-
m.TrackNo,
39-
m.Title,
40-
m.Year,
41-
m.Genres,
42-
m.Duration
43-
);
24+
var summaries =
25+
mediaFiles.Select(m => new
26+
{
27+
m.FileNameOnly,
28+
m.FileInfo.DirectoryName,
29+
m.Artists,
30+
m.AlbumArtists,
31+
m.Album,
32+
m.TrackNo,
33+
m.Title,
34+
m.Year,
35+
m.Genres,
36+
m.Duration,
37+
m.FileInfo.LastWriteTime,
4438
});
4539

46-
printer.Print("Serializing the tags to JSON...");
40+
printer.Print("Serializing tag data to JSON...");
4741
JsonSerializerOptions options = new()
4842
{
4943
WriteIndented = true,
5044
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
5145
};
5246
var json = JsonSerializer.Serialize(summaries, options);
53-
var unescapedJson = System.Text.RegularExpressions.Regex.Unescape(json); // Avoids `\0027`, etc.
5447

5548
printer.Print($"Saving cached tag data to \"{settings.TagCacheFilePath}\"...");
56-
File.WriteAllText(settings.TagCacheFilePath, unescapedJson);
49+
File.WriteAllText(settings.TagCacheFilePath, json);
5750
printer.Print($"Saved in {watch.ElapsedFriendly}.");
5851
}
5952
}

src/AudioTagger.Library/UpdatableFields.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ public Dictionary<string, string> GetUpdateKeyValuePairs(MediaFile fileData)
131131
{
132132
var updateOutput = new Dictionary<string, string>();
133133

134+
if (Title != null && Title != fileData.Title)
135+
{
136+
updateOutput.Add("Title", Title);
137+
}
138+
134139
if (AlbumArtists?.All(a => fileData.AlbumArtists.Contains(a)) == false)
135140
{
136141
updateOutput.Add("Album Artists", string.Join("; ", AlbumArtists));
@@ -141,11 +146,6 @@ public Dictionary<string, string> GetUpdateKeyValuePairs(MediaFile fileData)
141146
updateOutput.Add("Artists", string.Join("; ", Artists));
142147
}
143148

144-
if (Title != null && Title != fileData.Title)
145-
{
146-
updateOutput.Add("Title", Title);
147-
}
148-
149149
if (Album != null && Album != fileData.Album)
150150
{
151151
updateOutput.Add("Album", Album);

0 commit comments

Comments
 (0)