Skip to content

Commit 3524560

Browse files
author
Meyn
committed
Refactor
1 parent 98809b3 commit 3524560

20 files changed

+135
-201
lines changed

Tubifarry/Core/AlbumData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private string ConstructTitle()
102102
/// </summary>
103103
/// <param name="albumName">The album name to normalize.</param>
104104
/// <returns>The normalized album name.</returns>
105-
private string NormalizeAlbumName(string albumName)
105+
private static string NormalizeAlbumName(string albumName)
106106
{
107107
Regex featRegex = new(@"(?i)\b(feat\.|ft\.|featuring)\b", RegexOptions.IgnoreCase);
108108
if (featRegex.IsMatch(albumName))

Tubifarry/Core/ReleaseFormatter.cs

Lines changed: 93 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,48 @@
33
using NzbDrone.Core.Parser.Model;
44
using System.Text.RegularExpressions;
55

6-
public class ReleaseFormatter
6+
namespace Tubifarry.Core
77
{
8-
private readonly ReleaseInfo _releaseInfo;
9-
private readonly Artist _artist;
10-
private readonly NamingConfig? _namingConfig;
11-
12-
public ReleaseFormatter(ReleaseInfo releaseInfo, Artist artist, NamingConfig? namingConfig)
8+
public class ReleaseFormatter
139
{
14-
_releaseInfo = releaseInfo;
15-
_artist = artist;
16-
_namingConfig = namingConfig;
17-
}
10+
private readonly ReleaseInfo _releaseInfo;
11+
private readonly Artist _artist;
12+
private readonly NamingConfig? _namingConfig;
1813

19-
public string BuildTrackFilename(string? pattern, Track track, Album album)
20-
{
21-
pattern ??= _namingConfig?.StandardTrackFormat ?? "{track:0} {Track Title}";
22-
Dictionary<string, Func<string>> tokenHandlers = GetTokenHandlers(track, album);
23-
string formattedString = ReplaceTokens(pattern, tokenHandlers);
24-
return CleanFileName(formattedString);
25-
}
14+
public ReleaseFormatter(ReleaseInfo releaseInfo, Artist artist, NamingConfig? namingConfig)
15+
{
16+
_releaseInfo = releaseInfo;
17+
_artist = artist;
18+
_namingConfig = namingConfig;
19+
}
2620

27-
public string BuildAlbumFilename(string? pattern, Album album)
28-
{
29-
pattern ??= "{Album Title}";
30-
Dictionary<string, Func<string>> tokenHandlers = GetTokenHandlers(null, album);
31-
string formattedString = ReplaceTokens(pattern, tokenHandlers);
32-
return CleanFileName(formattedString);
33-
}
21+
public string BuildTrackFilename(string? pattern, Track track, Album album)
22+
{
23+
pattern ??= _namingConfig?.StandardTrackFormat ?? "{track:0} {Track Title}";
24+
Dictionary<string, Func<string>> tokenHandlers = GetTokenHandlers(track, album);
25+
string formattedString = ReplaceTokens(pattern, tokenHandlers);
26+
return CleanFileName(formattedString);
27+
}
3428

35-
public string BuildArtistFolderName(string? pattern)
36-
{
37-
pattern ??= _namingConfig?.ArtistFolderFormat ?? "{Artist Name}";
38-
Dictionary<string, Func<string>> tokenHandlers = GetTokenHandlers(null, null);
39-
string formattedString = ReplaceTokens(pattern, tokenHandlers);
40-
return CleanFileName(formattedString);
41-
}
29+
public string BuildAlbumFilename(string? pattern, Album album)
30+
{
31+
pattern ??= "{Album Title}";
32+
Dictionary<string, Func<string>> tokenHandlers = GetTokenHandlers(null, album);
33+
string formattedString = ReplaceTokens(pattern, tokenHandlers);
34+
return CleanFileName(formattedString);
35+
}
4236

43-
private Dictionary<string, Func<string>> GetTokenHandlers(Track? track, Album? album)
44-
{
45-
return new Dictionary<string, Func<string>>(StringComparer.OrdinalIgnoreCase)
37+
public string BuildArtistFolderName(string? pattern)
38+
{
39+
pattern ??= _namingConfig?.ArtistFolderFormat ?? "{Artist Name}";
40+
Dictionary<string, Func<string>> tokenHandlers = GetTokenHandlers(null, null);
41+
string formattedString = ReplaceTokens(pattern, tokenHandlers);
42+
return CleanFileName(formattedString);
43+
}
44+
45+
private Dictionary<string, Func<string>> GetTokenHandlers(Track? track, Album? album)
46+
{
47+
return new Dictionary<string, Func<string>>(StringComparer.OrdinalIgnoreCase)
4648
{
4749
// Album Tokens (only added if album is provided)
4850
{ "{Album Title}", () => album?.Title ?? string.Empty },
@@ -82,71 +84,72 @@ private Dictionary<string, Func<string>> GetTokenHandlers(Track? track, Album? a
8284
// Release Info Tokens
8385
{ "{Original Title}", () => _releaseInfo?.Title ?? string.Empty }
8486
};
85-
}
86-
87-
private static string ReplaceTokens(string pattern, Dictionary<string, Func<string>> tokenHandlers) => Regex.Replace(pattern, @"\{([^}]+)\}", match =>
88-
{
89-
string token = match.Groups[1].Value;
90-
if (tokenHandlers.TryGetValue($"{{{token}}}", out Func<string>? handler))
91-
return handler();
87+
}
9288

93-
return string.Empty;
94-
});
89+
private static string ReplaceTokens(string pattern, Dictionary<string, Func<string>> tokenHandlers) => Regex.Replace(pattern, @"\{([^}]+)\}", match =>
90+
{
91+
string token = match.Groups[1].Value;
92+
if (tokenHandlers.TryGetValue($"{{{token}}}", out Func<string>? handler))
93+
return handler();
9594

96-
private string CleanFileName(string fileName)
97-
{
98-
char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
99-
char[] invalidPathChars = Path.GetInvalidPathChars();
100-
char[] invalidChars = invalidFileNameChars.Union(invalidPathChars).ToArray();
101-
fileName = invalidChars.Aggregate(fileName, (current, invalidChar) => current.Replace(invalidChar.ToString(), string.Empty));
95+
return string.Empty;
96+
});
10297

103-
switch (_namingConfig?.ColonReplacementFormat)
98+
private string CleanFileName(string fileName)
10499
{
105-
case ColonReplacementFormat.Delete:
106-
fileName = fileName.Replace(":", string.Empty);
107-
break;
108-
case ColonReplacementFormat.Dash:
109-
fileName = fileName.Replace(":", "-");
110-
break;
111-
case ColonReplacementFormat.SpaceDash:
112-
fileName = fileName.Replace(":", " -");
113-
break;
114-
case ColonReplacementFormat.SpaceDashSpace:
115-
fileName = fileName.Replace(":", " - ");
116-
break;
117-
case ColonReplacementFormat.Smart:
118-
fileName = Regex.Replace(fileName, @":\s*", " - ");
119-
break;
100+
char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
101+
char[] invalidPathChars = Path.GetInvalidPathChars();
102+
char[] invalidChars = invalidFileNameChars.Union(invalidPathChars).ToArray();
103+
fileName = invalidChars.Aggregate(fileName, (current, invalidChar) => current.Replace(invalidChar.ToString(), string.Empty));
104+
105+
switch (_namingConfig?.ColonReplacementFormat)
106+
{
107+
case ColonReplacementFormat.Delete:
108+
fileName = fileName.Replace(":", string.Empty);
109+
break;
110+
case ColonReplacementFormat.Dash:
111+
fileName = fileName.Replace(":", "-");
112+
break;
113+
case ColonReplacementFormat.SpaceDash:
114+
fileName = fileName.Replace(":", " -");
115+
break;
116+
case ColonReplacementFormat.SpaceDashSpace:
117+
fileName = fileName.Replace(":", " - ");
118+
break;
119+
case ColonReplacementFormat.Smart:
120+
fileName = Regex.Replace(fileName, @":\s*", " - ");
121+
break;
122+
}
123+
124+
return fileName.Trim();
120125
}
121126

122-
return fileName.Trim();
123-
}
124-
125-
private static string CleanTitle(string? title)
126-
{
127-
if (string.IsNullOrEmpty(title)) return string.Empty;
128-
return title.Replace("&", "and").Replace("/", " ").Replace("\\", " ").Trim();
129-
}
127+
private static string CleanTitle(string? title)
128+
{
129+
if (string.IsNullOrEmpty(title)) return string.Empty;
130+
return title.Replace("&", "and").Replace("/", " ").Replace("\\", " ").Trim();
131+
}
130132

131-
private static string TitleThe(string? title)
132-
{
133-
if (string.IsNullOrEmpty(title)) return string.Empty;
134-
return Regex.Replace(title, @"^(The|A|An)\s+(.+)$", "$2, $1", RegexOptions.IgnoreCase);
135-
}
133+
private static string TitleThe(string? title)
134+
{
135+
if (string.IsNullOrEmpty(title)) return string.Empty;
136+
return Regex.Replace(title, @"^(The|A|An)\s+(.+)$", "$2, $1", RegexOptions.IgnoreCase);
137+
}
136138

137-
private static string CleanTitleThe(string? title) => CleanTitle(TitleThe(title));
139+
private static string CleanTitleThe(string? title) => CleanTitle(TitleThe(title));
138140

139-
private static string TitleFirstCharacter(string? title)
140-
{
141-
if (string.IsNullOrEmpty(title)) return "_";
142-
return char.IsLetterOrDigit(title[0]) ? title[..1].ToUpper() : "_";
143-
}
141+
private static string TitleFirstCharacter(string? title)
142+
{
143+
if (string.IsNullOrEmpty(title)) return "_";
144+
return char.IsLetterOrDigit(title[0]) ? title[..1].ToUpper() : "_";
145+
}
144146

145-
private static string FormatTrackNumber(string? trackNumber, string? format)
146-
{
147-
if (string.IsNullOrEmpty(trackNumber)) return string.Empty;
148-
if (int.TryParse(trackNumber, out int trackNumberInt))
149-
return trackNumberInt.ToString(format);
150-
return trackNumber;
147+
private static string FormatTrackNumber(string? trackNumber, string? format)
148+
{
149+
if (string.IsNullOrEmpty(trackNumber)) return string.Empty;
150+
if (int.TryParse(trackNumber, out int trackNumberInt))
151+
return trackNumberInt.ToString(format);
152+
return trackNumber;
153+
}
151154
}
152155
}

Tubifarry/Download/Clients/Soulseek/SlskdClient.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public class SlskdClient : DownloadClientBase<SlskdProviderSettings>
1818
private static readonly Dictionary<DownloadKey, SlskdDownloadItem> _downloadMappings = new();
1919

2020
public override string Name => "Slskd";
21-
2221
public override string Protocol => nameof(SoulseekDownloadProtocol);
2322

2423
public SlskdClient(IHttpClient httpClient, IConfigService configService, IDiskProvider diskProvider, IRemotePathMappingService remotePathMappingService, Logger logger)
@@ -208,7 +207,7 @@ private async Task RemoveItemAsync(SlskdDownloadItem item)
208207
if (response.StatusCode != HttpStatusCode.NoContent)
209208
_logger.Warn($"Failed to remove download with ID {item.ID}. Status Code: {response.StatusCode}");
210209
else
211-
_logger.Info($"Successfully removed download with ID {item.ID}.");
210+
_logger.Trace($"Successfully removed download with ID {item.ID}.");
212211
}
213212

214213
private async Task<HttpResponse> ExecuteAsync(HttpRequest request) => await _httpClient.ExecuteAsync(request);

Tubifarry/Download/Clients/Soulseek/SlskdModels.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using NzbDrone.Common.Disk;
2+
using NzbDrone.Common.Instrumentation;
23
using NzbDrone.Core.Indexers.Soulseek;
34
using NzbDrone.Core.Parser.Model;
45
using System.Text.Json;
@@ -32,7 +33,8 @@ public SlskdDownloadItem(RemoteAlbum remoteAlbum)
3233

3334
public DownloadClientItem GetDownloadClientItem(string downloadPath)
3435
{
35-
_downloadClientItem.OutputPath = new OsPath(Path.Combine(downloadPath, SlskdDownloadDirectory?.Directory ?? ""));
36+
_downloadClientItem.OutputPath = new OsPath(Path.Combine(downloadPath, Path.GetFileName(SlskdDownloadDirectory?.Directory.TrimEnd('\\') ?? "")));
37+
NzbDroneLogger.GetLogger(true).Info(_downloadClientItem.OutputPath);
3638
_downloadClientItem.Title = RemoteAlbum.Release.Title;
3739

3840
if (SlskdDownloadDirectory?.Files == null)
@@ -54,8 +56,7 @@ public DownloadClientItem GetDownloadClientItem(string downloadPath)
5456
List<DownloadItemStatus> fileStatuses = SlskdDownloadDirectory.Files.Select(file => file.GetStatus()).ToList();
5557
List<string> failedFiles = SlskdDownloadDirectory.Files
5658
.Where(file => file.GetStatus() == DownloadItemStatus.Failed)
57-
.Select(file => Path.GetFileName(file.Filename))
58-
.ToList();
59+
.Select(file => Path.GetFileName(file.Filename)).ToList();
5960

6061
DownloadItemStatus status = DownloadItemStatus.Queued;
6162

@@ -184,6 +185,5 @@ public DownloadKey(int outerKey, int innerKey)
184185
public override readonly int GetHashCode() => HashCode.Combine(OuterKey, InnerKey);
185186
public static bool operator ==(DownloadKey left, DownloadKey right) => left.Equals(right);
186187
public static bool operator !=(DownloadKey left, DownloadKey right) => !(left == right);
187-
188188
}
189189
}

Tubifarry/Download/Clients/YouTube/YoutubeDownloadManager.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public interface IYoutubeDownloadManager
2727
public class YoutubeDownloadManager : IYoutubeDownloadManager
2828
{
2929
private readonly RequestContainer<YouTubeAlbumRequest> _queue;
30+
private readonly Logger _logger;
3031
private YouTubeMusicClient _ytClient;
3132
private Task? _testTask;
3233
private string? _cookiePath;
33-
private Logger _logger;
3434

3535

3636
/// <summary>
@@ -42,7 +42,6 @@ public YoutubeDownloadManager(Logger logger)
4242
_logger = logger;
4343
_queue = new();
4444
_ytClient = new YouTubeMusicClient();
45-
_logger.Trace("Initialized");
4645
}
4746

4847
public void SetCookies(string path)

Tubifarry/Download/Clients/YouTubeAlbumRequest.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ internal class YouTubeAlbumRequest : Request<YouTubeAlbumOptions, string, string
3636
private byte[]? _albumCover;
3737

3838
private ReleaseInfo ReleaseInfo => _remoteAlbum.Release;
39-
4039
public override Task Task => _requestContainer.Task;
4140
public override RequestState State => _requestContainer.State;
4241
public string ID { get; } = Guid.NewGuid().ToString();
@@ -267,7 +266,6 @@ private string GetDistinctMessages()
267266
ReadOnlyMemory<char> chunk = chunkEnumerator.Current;
268267
distinctMessages.Add(chunk.ToString());
269268
}
270-
271269
return string.Join("", distinctMessages);
272270
}
273271

Tubifarry/ImportLists/ArrStack/ArrSoundtrackImport.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,5 @@ public override IEnumerable<ProviderDefinition> DefaultDefinitions
122122
APIItemEndpoint = apiItemEndpoint,
123123
APIStatusEndpoint = apiStatusEndpoint
124124
};
125-
126125
}
127126
}

0 commit comments

Comments
 (0)