Skip to content

Commit 7fad794

Browse files
committed
Allow custom TV parsing regex - address #4
1 parent 7acfc0b commit 7fad794

8 files changed

+56
-14
lines changed

.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"request": "launch",
1111
"preLaunchTask": "build",
1212
// If you have changed target frameworks, make sure to update the program path.
13-
"program": "${workspaceFolder}/autotag.cli/bin/Debug/netcoreapp3.1/autotag.dll",
13+
"program": "${workspaceFolder}/autotag.cli/bin/Debug/net5.0/autotag.dll",
1414
"args": [],
1515
"cwd": "${workspaceFolder}/autotag.cli",
1616
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ Options:
2828
--no-rename Disable file renaming
2929
--no-tag Disable file tagging
3030
--no-cover Disable cover art tagging
31-
--manual Manually choose the series to tag
31+
--manual Manually choose the series to tag from search results
32+
-p|--pattern <PATTERN> Custom regex to parse TV episode information
3233
-v|--verbose Enable verbose output mode
3334
--set-default Set the current arguments as the default
3435
--version Print version number and exit
3536
-?|-h|--help Show help information
3637
3738
```
3839

40+
The custom regex pattern is used on the full file path, not just the file name. This allows AutoTag to tag file structures where the series name is not in the file name, e.g. for the structure `Series/Season 1/S01E01 Title.mkv`.
41+
42+
The regex pattern should have 3 named capturing groups: `SeriesName`, `Season` and `Episode`. For the example given above, a pattern could be `.*/(?<SeriesName>.+)/Season (?<Season>\d+)/S\d+E(?<Episode>\d+)`.
43+
3944
## Config
4045
AutoTag creates a config file to store default preferences at `~/.config/autotag/conf.json` or `%APPDATA%\Roaming\autotag\conf.json`. A different config file can be specified using the `-c` option. If the file does not exist, a file will be created with the default settings:
4146
```
@@ -48,6 +53,7 @@ AutoTag creates a config file to store default preferences at `~/.config/autotag
4853
"renameFiles": true, // Rename files
4954
"tvRenamePattern": "%1 - %2x%3 - %4", // Pattern to rename TV files, %1 = Series Name, %2 = Season, %3 = Episode, %4 = Episode Title
5055
"movieRenamePattern": "%1 (%2)" // Pattern to rename movie files, %1 = Title, %2 = Year
56+
"parsePattern": "" // Custom regex to parse TV episode information
5157
```
5258

5359
## Known Issues

autotag.Core/AutoTagConfig.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
namespace autotag.Core {
22
public class AutoTagConfig {
33
public enum Modes { TV, Movie };
4-
public int configVer { get; set; } = 1;
4+
public static int currentVer = 2;
5+
public int configVer { get; set; } = currentVer;
56
public Modes mode { get; set; } = Modes.TV;
67
public bool manualMode { get; set; } = false;
78
public bool verbose { get; set; } = false;
@@ -10,5 +11,6 @@ public enum Modes { TV, Movie };
1011
public bool renameFiles { get; set; } = true;
1112
public string tvRenamePattern { get; set; } = "%1 - %2x%3 - %4";
1213
public string movieRenamePattern { get; set; } = "%1 (%2)";
14+
public string parsePattern { get; set; } = "";
1315
}
1416
}

autotag.Core/AutoTagSettings.cs

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public AutoTagSettings(string configPath) {
2222
} catch (Exception e) {
2323
Console.Error.WriteLine($"Error reading config file '{configPath}': {e.Message}");
2424
}
25+
26+
if (config.configVer != AutoTagConfig.currentVer) {
27+
// if config file outdated, update it with new options
28+
config.configVer = AutoTagConfig.currentVer;
29+
Save();
30+
}
2531
} else {
2632
Console.WriteLine($"Generating new config file with default options '{configPath}'");
2733
FileInfo configFile = new FileInfo(configPath);

autotag.Core/TVProcessor.cs

+25-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Text.RegularExpressions;
56
using System.Threading.Tasks;
67

78
using TvDbSharper;
@@ -28,19 +29,37 @@ public async Task<bool> process(
2829
AutoTagConfig config) {
2930

3031
if (tvdb.Authentication.Token == null) {
31-
await tvdb.Authentication.AuthenticateAsync("TQLC3N5YDI1AQVJF");
32+
await tvdb.Authentication.AuthenticateAsync(apiKey);
3233
}
3334

3435
FileMetadata result = new FileMetadata(FileMetadata.Types.TV);
3536

3637
#region Filename parsing
3738
FileMetadata episodeData;
3839

39-
try {
40-
episodeData = EpisodeParser.ParseEpisodeInfo(Path.GetFileName(filePath)); // Parse info from filename
41-
} catch (FormatException ex) {
42-
setStatus($"Error: {ex.Message}", true);
43-
return false;
40+
if (string.IsNullOrEmpty(config.parsePattern)) {
41+
try {
42+
episodeData = EpisodeParser.ParseEpisodeInfo(Path.GetFileName(filePath)); // Parse info from filename
43+
} catch (FormatException ex) {
44+
setStatus($"Error: {ex.Message}", true);
45+
return false;
46+
}
47+
} else {
48+
try {
49+
var match = Regex.Match(filePath, config.parsePattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
50+
51+
episodeData = new FileMetadata(FileMetadata.Types.TV);
52+
episodeData.SeriesName = match.Groups["SeriesName"].Value;
53+
episodeData.Season = int.Parse(match.Groups["Season"].Value);
54+
episodeData.Episode = int.Parse(match.Groups["Episode"].Value);
55+
} catch (FormatException ex) {
56+
if (config.verbose) {
57+
setStatus($"Error: {ex.Message}", true);
58+
} else {
59+
setStatus($"Error: Unable to parse required information from filename", true);
60+
}
61+
return false;
62+
}
4463
}
4564

4665
result.Season = episodeData.Season;

autotag.Core/autotag.Core.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="System.Text.Json" Version="5.0.0" />
8+
<PackageReference Include="System.Text.Json" Version="5.0.1" />
99
<PackageReference Include="TagLibSharp" Version="2.2.0" />
1010
<PackageReference Include="TMDbLib" Version="1.6.0" />
1111
<PackageReference Include="TvDbSharper" Version="3.2.2" />

autotag.cli/Program.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ private async Task OnExecuteAsync() {
4646
if (manualMode) {
4747
settings.config.manualMode = true;
4848
}
49+
if (!string.IsNullOrEmpty(pattern)) {
50+
settings.config.parsePattern = pattern;
51+
}
4952
if (verbose) {
5053
settings.config.verbose = true;
5154
}
@@ -201,9 +204,12 @@ private void AddFiles(string[] paths) {
201204
[Option("--no-cover", "Disable cover art tagging", CommandOptionType.NoValue)]
202205
private bool noCoverArt { get; set; }
203206

204-
[Option("--manual", "Manually choose the series to tag", CommandOptionType.NoValue)]
207+
[Option("--manual", "Manually choose the series to tag from search results", CommandOptionType.NoValue)]
205208
private bool manualMode { get; set; }
206209

210+
[Option(Description = "Custom regex to parse TV episode information")]
211+
private string pattern { get; set; } = "";
212+
207213
[Option(Description = "Enable verbose output mode")]
208214
private bool verbose { get; set; }
209215

autotag.cli/autotag.cli.csproj

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
<AssemblyName>autotag</AssemblyName>
55
<OutputType>Exe</OutputType>
66
<TargetFramework>net5.0</TargetFramework>
7-
<AssemblyVersion>3.0.1.0</AssemblyVersion>
8-
<Version>3.0.1</Version>
7+
<AssemblyVersion>3.0.2.0</AssemblyVersion>
8+
<Version>3.0.2</Version>
9+
</PropertyGroup>
10+
11+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
912
<PublishSingleFile>true</PublishSingleFile>
1013
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
1114
<SelfContained>true</SelfContained>
@@ -14,7 +17,7 @@
1417
</PropertyGroup>
1518

1619
<ItemGroup>
17-
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="3.0.0" />
20+
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="3.1.0" />
1821
</ItemGroup>
1922

2023
<ItemGroup>

0 commit comments

Comments
 (0)