2019-01-13 19:54:44 +00:00
|
|
|
using System;
|
2018-09-12 17:26:21 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2019-01-13 19:17:29 +00:00
|
|
|
using Emby.Naming.Common;
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
namespace Emby.Naming.Subtitles
|
|
|
|
{
|
2020-11-10 18:23:10 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Subtitle Parser class.
|
|
|
|
/// </summary>
|
2018-09-12 17:26:21 +00:00
|
|
|
public class SubtitleParser
|
|
|
|
{
|
|
|
|
private readonly NamingOptions _options;
|
|
|
|
|
2020-11-10 18:23:10 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="SubtitleParser"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="options"><see cref="NamingOptions"/> object containing SubtitleFileExtensions, SubtitleDefaultFlags, SubtitleForcedFlags and SubtitleFlagDelimiters.</param>
|
2018-09-12 17:26:21 +00:00
|
|
|
public SubtitleParser(NamingOptions options)
|
|
|
|
{
|
|
|
|
_options = options;
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:23:10 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Parse file to determine if is subtitle and <see cref="SubtitleInfo"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">Path to file.</param>
|
|
|
|
/// <returns>Returns null or <see cref="SubtitleInfo"/> object if parsing is successful.</returns>
|
2020-04-19 09:57:03 +00:00
|
|
|
public SubtitleInfo? ParseFile(string path)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2020-04-19 09:57:03 +00:00
|
|
|
if (path.Length == 0)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2020-11-10 18:23:10 +00:00
|
|
|
return null;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var extension = Path.GetExtension(path);
|
|
|
|
if (!_options.SubtitleFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var flags = GetFlags(path);
|
2020-11-01 09:47:31 +00:00
|
|
|
var info = new SubtitleInfo(
|
|
|
|
path,
|
|
|
|
_options.SubtitleDefaultFlags.Any(i => flags.Contains(i, StringComparer.OrdinalIgnoreCase)),
|
|
|
|
_options.SubtitleForcedFlags.Any(i => flags.Contains(i, StringComparer.OrdinalIgnoreCase)));
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2020-03-26 15:31:23 +00:00
|
|
|
var parts = flags.Where(i => !_options.SubtitleDefaultFlags.Contains(i, StringComparer.OrdinalIgnoreCase)
|
|
|
|
&& !_options.SubtitleForcedFlags.Contains(i, StringComparer.OrdinalIgnoreCase))
|
2018-09-12 17:26:21 +00:00
|
|
|
.ToList();
|
|
|
|
|
|
|
|
// Should have a name, language and file extension
|
|
|
|
if (parts.Count >= 3)
|
|
|
|
{
|
2020-01-18 15:18:55 +00:00
|
|
|
info.Language = parts[^2];
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
private string[] GetFlags(string path)
|
|
|
|
{
|
2020-11-18 13:23:45 +00:00
|
|
|
// Note: the tags need be surrounded be either a space ( ), hyphen -, dot . or underscore _.
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
var file = Path.GetFileName(path);
|
|
|
|
|
|
|
|
return file.Split(_options.SubtitleFlagDelimiters, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|