TxtLyricProvider.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Lyrics;
  7. namespace MediaBrowser.Providers.Lyric
  8. {
  9. /// <summary>
  10. /// TXT Lyric Provider.
  11. /// </summary>
  12. public class TxtLyricProvider : ILyricProvider
  13. {
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="TxtLyricProvider"/> class.
  16. /// </summary>
  17. public TxtLyricProvider()
  18. {
  19. Name = "TxtLyricProvider";
  20. SupportedMediaTypes = new Collection<string>
  21. {
  22. "lrc", "txt"
  23. };
  24. }
  25. /// <summary>
  26. /// Gets a value indicating the provider name.
  27. /// </summary>
  28. public string Name { get; }
  29. /// <summary>
  30. /// Gets a value indicating the File Extenstions this provider supports.
  31. /// </summary>
  32. public IEnumerable<string> SupportedMediaTypes { get; }
  33. /// <summary>
  34. /// Opens lyric file for the requested item, and processes it for API return.
  35. /// </summary>
  36. /// <param name="item">The item to to process.</param>
  37. /// <returns>If provider can determine lyrics, returns a <see cref="LyricResponse"/>; otherwise, null.</returns>
  38. public LyricResponse? GetLyrics(BaseItem item)
  39. {
  40. string? lyricFilePath = LyricInfo.GetLyricFilePath(this, item.Path);
  41. if (string.IsNullOrEmpty(lyricFilePath))
  42. {
  43. return null;
  44. }
  45. List<Controller.Lyrics.Lyric> lyricList = new List<Controller.Lyrics.Lyric>();
  46. string lyricData = System.IO.File.ReadAllText(lyricFilePath);
  47. // Splitting on Environment.NewLine caused some new lines to be missed in Windows.
  48. char[] newLineDelims = new[] { '\r', '\n' };
  49. string[] lyricTextLines = lyricData.Split(newLineDelims, StringSplitOptions.RemoveEmptyEntries);
  50. if (!lyricTextLines.Any())
  51. {
  52. return null;
  53. }
  54. foreach (string lyricTextLine in lyricTextLines)
  55. {
  56. lyricList.Add(new Controller.Lyrics.Lyric { Text = lyricTextLine });
  57. }
  58. return new LyricResponse { Lyrics = lyricList };
  59. }
  60. }
  61. }