Skip to content

Commit

Permalink
cleanup: prittify get attribute value
Browse files Browse the repository at this point in the history
- Prittified how we get the attribute value.
  • Loading branch information
revam committed Mar 30, 2024
1 parent a4c7913 commit 8ab9444
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
10 changes: 3 additions & 7 deletions Shokofin/API/ShokoAPIManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,11 @@ internal void AddFileLookupIds(string path, string fileId, string seriesId, IEnu
// Fast-path for VFS.
if (path.StartsWith(Plugin.Instance.VirtualRoot + Path.DirectorySeparatorChar)) {
var fileName = Path.GetFileNameWithoutExtension(path);
if (!int.TryParse(fileName.GetAttributeValue("shoko-series"), out var seriesIdRaw))
if (!fileName.TryGetAttributeValue("shoko-series", out var sI) || !int.TryParse(sI, out _))
return (null, null, null);
if (!int.TryParse(fileName.GetAttributeValue("shoko-file"), out var fileIdRaw))
if (!fileName.TryGetAttributeValue("shoko-file", out var fI) || !int.TryParse(fI, out _))
return (null, null, null);

var sI = seriesIdRaw.ToString();
var fI = fileIdRaw.ToString();
var fileInfo = await GetFileInfo(fI, sI).ConfigureAwait(false);
if (fileInfo == null)
return (null, null, null);
Expand Down Expand Up @@ -686,11 +684,9 @@ public bool TryGetDefaultSeriesIdForSeriesId(string seriesId, out string? defaul

// Fast-path for VFS.
if (path.StartsWith(Plugin.Instance.VirtualRoot + Path.DirectorySeparatorChar)) {
var seriesSegment = Path.GetFileName(path).GetAttributeValue("shoko-series");
if (!int.TryParse(seriesSegment, out var seriesIdRaw))
if (!Path.GetFileName(path).TryGetAttributeValue("shoko-series", out seriesId) || !int.TryParse(seriesId, out _))
return null;

seriesId = seriesIdRaw.ToString();
PathToSeriesIdDictionary[path] = seriesId;
SeriesIdToPathDictionary.TryAdd(seriesId, path);

Expand Down
12 changes: 5 additions & 7 deletions Shokofin/Resolvers/ShokoResolveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,7 @@ private async Task<bool> ScanFile(string partialPath, string fullPath, bool shou
return null;

if (parent.Id == mediaFolder.Id && fileInfo.IsDirectory) {
var seriesSegment = fileInfo.Name.GetAttributeValue("shoko-series");
if (!int.TryParse(seriesSegment, out var seriesId))
if (!fileInfo.Name.TryGetAttributeValue("shoko-series", out var seriesId) || !int.TryParse(seriesId, out _))
return null;

return new TvSeries()
Expand Down Expand Up @@ -644,11 +643,10 @@ private async Task<bool> ScanFile(string partialPath, string fullPath, bool shou
var items = FileSystem.GetDirectories(vfsPath)
.AsParallel()
.SelectMany(dirInfo => {
var seriesSegment = dirInfo.Name.GetAttributeValue("shoko-series");
if (!int.TryParse(seriesSegment, out var seriesId))
if (!dirInfo.Name.TryGetAttributeValue("shoko-series", out var seriesId) || !int.TryParse(seriesId, out _))
return Array.Empty<BaseItem>();

var season = ApiManager.GetSeasonInfoForSeries(seriesId.ToString())
var season = ApiManager.GetSeasonInfoForSeries(seriesId)
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
Expand All @@ -659,12 +657,12 @@ private async Task<bool> ScanFile(string partialPath, string fullPath, bool shou
return FileSystem.GetFiles(dirInfo.FullName)
.AsParallel()
.Select(fileInfo => {
if (!int.TryParse(fileInfo.Name.GetAttributeValue("shoko-file"), out var fileId))
if (!fileInfo.Name.TryGetAttributeValue("shoko-file", out var fileId) || !int.TryParse(fileId, out _))
return null;

// This will hopefully just re-use the pre-cached entries from the cache, but it may
// also get it from remote if the cache was emptied for whatever reason.
var file = ApiManager.GetFileInfo(fileId.ToString(), seriesId.ToString())
var file = ApiManager.GetFileInfo(fileId, seriesId)
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
Expand Down
5 changes: 4 additions & 1 deletion Shokofin/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static string ReplaceInvalidPathCharacters(this string path)
.Replace(@"?", "\uff1f") // ? (FULL WIDTH QUESTION MARK)
.Replace(@".", "\u2024") // ․ (ONE DOT LEADER)
.Trim();

/// <summary>
/// Gets the attribute value for <paramref name="attribute"/> in <paramref name="text"/>.
/// </summary>
Expand Down Expand Up @@ -117,4 +117,7 @@ public static string ReplaceInvalidPathCharacters(this string path)

return null;
}

public static bool TryGetAttributeValue(this string text, string attribute, out string? value)
=> !string.IsNullOrEmpty(value = GetAttributeValue(text, attribute));
}

0 comments on commit 8ab9444

Please sign in to comment.