AlbumParser.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. continue;
  40. }
  41. var tmp = filename.Substring(prefix.Length);
  42. tmp = tmp.Trim().Split(' ').FirstOrDefault() ?? string.Empty;
  43. if (int.TryParse(tmp, NumberStyles.Integer, CultureInfo.InvariantCulture, out _))
  44. {
  45. result.IsMultiPart = true;
  46. break;
  47. }
  48. }
  49. return result;
  50. }
  51. }
  52. }