AlbumParser.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #pragma warning disable CS1591
  2. #pragma warning disable SA1600
  3. using System;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text.RegularExpressions;
  8. using Emby.Naming.Common;
  9. namespace Emby.Naming.Audio
  10. {
  11. public class AlbumParser
  12. {
  13. private readonly NamingOptions _options;
  14. public AlbumParser(NamingOptions options)
  15. {
  16. _options = options;
  17. }
  18. public MultiPartResult ParseMultiPart(string path)
  19. {
  20. var result = new MultiPartResult();
  21. var filename = Path.GetFileName(path);
  22. if (string.IsNullOrEmpty(filename))
  23. {
  24. return result;
  25. }
  26. // TODO: Move this logic into options object
  27. // Even better, remove the prefixes and come up with regexes
  28. // But Kodi documentation seems to be weak for audio
  29. // Normalize
  30. // Remove whitespace
  31. filename = filename.Replace('-', ' ');
  32. filename = filename.Replace('.', ' ');
  33. filename = filename.Replace('(', ' ');
  34. filename = filename.Replace(')', ' ');
  35. filename = Regex.Replace(filename, @"\s+", " ");
  36. filename = filename.TrimStart();
  37. foreach (var prefix in _options.AlbumStackingPrefixes)
  38. {
  39. if (filename.IndexOf(prefix, StringComparison.OrdinalIgnoreCase) != 0)
  40. {
  41. continue;
  42. }
  43. var tmp = filename.Substring(prefix.Length);
  44. tmp = tmp.Trim().Split(' ').FirstOrDefault() ?? string.Empty;
  45. if (int.TryParse(tmp, NumberStyles.Integer, CultureInfo.InvariantCulture, out _))
  46. {
  47. result.IsMultiPart = true;
  48. break;
  49. }
  50. }
  51. return result;
  52. }
  53. }
  54. }