AlbumParser.cs 1.8 KB

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