2019-01-06 20:50:43 +00:00
|
|
|
using System;
|
2018-09-12 17:26:21 +00:00
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text.RegularExpressions;
|
2019-01-13 19:17:29 +00:00
|
|
|
using Emby.Naming.Common;
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
namespace Emby.Naming.Video
|
|
|
|
{
|
|
|
|
/// <summary>
|
2019-10-25 10:47:20 +00:00
|
|
|
/// <see href="http://kodi.wiki/view/Advancedsettings.xml#video" />.
|
2018-09-12 17:26:21 +00:00
|
|
|
/// </summary>
|
|
|
|
public class CleanDateTimeParser
|
|
|
|
{
|
|
|
|
private readonly NamingOptions _options;
|
|
|
|
|
|
|
|
public CleanDateTimeParser(NamingOptions options)
|
|
|
|
{
|
|
|
|
_options = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CleanDateTimeResult Clean(string name)
|
|
|
|
{
|
|
|
|
var originalName = name;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var extension = Path.GetExtension(name) ?? string.Empty;
|
|
|
|
// Check supported extensions
|
2019-05-10 18:37:42 +00:00
|
|
|
if (!_options.VideoFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase)
|
|
|
|
&& !_options.AudioFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
// Dummy up a file extension because the expressions will fail without one
|
|
|
|
// This is tricky because we can't just check Path.GetExtension for empty
|
|
|
|
// If the input is "St. Vincent (2014)", it will produce ". Vincent (2014)" as the extension
|
|
|
|
name += ".mkv";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ArgumentException)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
var result = _options.CleanDateTimeRegexes.Select(i => Clean(name, i))
|
|
|
|
.FirstOrDefault(i => i.HasChanged) ??
|
|
|
|
new CleanDateTimeResult { Name = originalName };
|
|
|
|
|
|
|
|
if (result.HasChanged)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a second pass, running clean string first
|
|
|
|
var cleanStringResult = new CleanStringParser().Clean(name, _options.CleanStringRegexes);
|
|
|
|
|
|
|
|
if (!cleanStringResult.HasChanged)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _options.CleanDateTimeRegexes.Select(i => Clean(cleanStringResult.Name, i))
|
|
|
|
.FirstOrDefault(i => i.HasChanged) ??
|
|
|
|
result;
|
|
|
|
}
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
private static CleanDateTimeResult Clean(string name, Regex expression)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
var result = new CleanDateTimeResult();
|
|
|
|
|
|
|
|
var match = expression.Match(name);
|
|
|
|
|
2019-05-10 18:37:42 +00:00
|
|
|
if (match.Success
|
|
|
|
&& match.Groups.Count == 4
|
|
|
|
&& match.Groups[1].Success
|
|
|
|
&& match.Groups[2].Success
|
|
|
|
&& int.TryParse(match.Groups[2].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var year))
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2019-05-10 18:37:42 +00:00
|
|
|
name = match.Groups[1].Value;
|
|
|
|
result.Year = year;
|
|
|
|
result.HasChanged = true;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result.Name = name;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|