Skip to content

Commit

Permalink
Merge pull request #1 from azam/develop
Browse files Browse the repository at this point in the history
Add %any% placeholder for wildcard matching
  • Loading branch information
azam authored Mar 14, 2023
2 parents 6bb3b03 + b69fcc7 commit 7f46e85
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 41 deletions.
6 changes: 3 additions & 3 deletions Jellyfin.Plugin.LocalSubs/Jellyfin.Plugin.LocalSubs.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>0.1.0.0</Version>
<AssemblyVersion>0.1.0.0</AssemblyVersion>
<FileVersion>0.1.0.0</FileVersion>
<Version>0.1.1.0</Version>
<AssemblyVersion>0.1.1.0</AssemblyVersion>
<FileVersion>0.1.1.0</FileVersion>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>Jellyfin.Plugin.LocalSubs</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
1 change: 1 addition & 0 deletions Jellyfin.Plugin.LocalSubs/LocalSubsPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ <h3 class="sectionTitle sectionTitle-cards">
<li><code>%fe%</code> : Filename extension</li>
<li><code>%l%</code> : Language</li>
<li><code>%n%</code> : Numbers</li>
<li><code>%any%</code> : Any characters</li>
</ul>
</div>
</div>
Expand Down
79 changes: 42 additions & 37 deletions Jellyfin.Plugin.LocalSubs/LocalSubsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,49 +137,54 @@ public Task<IEnumerable<RemoteSubtitleInfo>> Search(SubtitleSearchRequest reques
return Task.FromResult(Enumerable.Empty<RemoteSubtitleInfo>());
}

List<string> langStrings = new List<string>();
langStrings.Add(request.Language); // Three letter language code
langStrings.Add(request.TwoLetterISOLanguageName); // Two letter language code
try
{
string[] langStrings = new string[] { request.Language, request.TwoLetterISOLanguageName, CultureInfo.GetCultureInfo(request.TwoLetterISOLanguageName).EnglishName };
string dir = Path.GetDirectoryName(request.MediaPath) ?? string.Empty;
string f = Path.GetFileName(request.MediaPath);
string fn = Path.GetFileNameWithoutExtension(request.MediaPath);
string fe = Path.GetExtension(request.MediaPath);
Dictionary<string, string> placeholders = new Dictionary<string, string>();
placeholders.Add("%f%", Regex.Escape(f));
placeholders.Add("%fn%", Regex.Escape(fn));
placeholders.Add("%fe%", Regex.Escape(fe));
placeholders.Add("%n%", "[0-9]+");
placeholders.Add("%l%", "(?i)(" + string.Join("|", langStrings) + ")");
List<RemoteSubtitleInfo> matches = new List<RemoteSubtitleInfo>();
foreach (string template in templates)
langStrings.Add(CultureInfo.GetCultureInfo(request.TwoLetterISOLanguageName).EnglishName);
}
catch (CultureNotFoundException e)
{
_logger.LogError(e, "Culture not found for {0}", request.TwoLetterISOLanguageName);
}

string dir = Path.GetDirectoryName(request.MediaPath) ?? string.Empty;
string f = Path.GetFileName(request.MediaPath);
string fn = Path.GetFileNameWithoutExtension(request.MediaPath);
string fe = Path.GetExtension(request.MediaPath);
Dictionary<string, string> placeholders = new Dictionary<string, string>();
placeholders.Add("%f%", Regex.Escape(f));
placeholders.Add("%fn%", Regex.Escape(fn));
placeholders.Add("%fe%", Regex.Escape(fe));
placeholders.Add("%n%", "[0-9]+");
placeholders.Add("%l%", "(?i)(" + string.Join("|", langStrings) + ")");
placeholders.Add("%any%", ".+");
List<RemoteSubtitleInfo> matches = new List<RemoteSubtitleInfo>();
foreach (string template in templates)
{
if (string.IsNullOrEmpty(template))
{
if (string.IsNullOrEmpty(template))
{
continue;
}
continue;
}

foreach (string match in MatchFile(dir, template, placeholders))
foreach (string match in MatchFile(dir, template, placeholders))
{
string ext = (Path.GetExtension(match) ?? "srt").ToLowerInvariant().Replace(".", string.Empty, StringComparison.OrdinalIgnoreCase);
string lang = request.Language;
string id = string.Join(LocalSubsConstants.IDSEPARATOR, ext, lang, match);
_logger.LogInformation("RemoteSubtitleInfo MediaPath: {0} Format: {1} Language: {2} Id: {3}", request.MediaPath, ext, lang, id);
matches.Add(new RemoteSubtitleInfo
{
string ext = (Path.GetExtension(match) ?? "srt").ToLowerInvariant().Replace(".", string.Empty, StringComparison.OrdinalIgnoreCase);
string lang = request.Language;
string id = string.Join(LocalSubsConstants.IDSEPARATOR, ext, lang, match);
_logger.LogInformation("RemoteSubtitleInfo MediaPath: {0} Format: {1} Language: {2} Id: {3}", request.MediaPath, ext, lang, id);
matches.Add(new RemoteSubtitleInfo
{
Id = id,
ProviderName = LocalSubsConstants.NAME,
Format = ext,
ThreeLetterISOLanguageName = lang,
DateCreated = new FileInfo(match).CreationTime,
});
}
Id = id,
ProviderName = LocalSubsConstants.NAME,
Format = ext,
ThreeLetterISOLanguageName = lang,
DateCreated = new FileInfo(match).CreationTime,
});
}

return Task.FromResult(matches.AsEnumerable());
}
catch (CultureNotFoundException e)
{
return Task.FromException<IEnumerable<RemoteSubtitleInfo>>(e);
}

return Task.FromResult(matches.AsEnumerable());
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Available placeholders are:
* `%fe%` : Filename extension
* `%l%` : Language
* `%n%` : Any arbitrary number
* `%any%` : Any characters (This exhaustively tries to match files/directories, so use with care)

## Installation

Expand Down
2 changes: 1 addition & 1 deletion build.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: "Local Subs"
guid: "7de4aa03-f418-4e1c-a8ba-08ccecba4ab5"
version: "0.1.0.0"
version: "0.1.1.0"
targetAbi: "10.8.0.0"
framework: "net6.0"
overview: "Subtitle provider for local subtitle files."
Expand Down

0 comments on commit 7f46e85

Please sign in to comment.