From 9d5cf67dfe2d5871d42a55a5e114c5ead1036ff0 Mon Sep 17 00:00:00 2001
From: 1hitsong <3330318+1hitsong@users.noreply.github.com>
Date: Sat, 10 Sep 2022 14:58:03 -0400
Subject: Create ILyricsProvider
---
Jellyfin.Api/Models/UserDtos/ILyricsProvider.cs | 34 +++++++
Jellyfin.Api/Models/UserDtos/LrcLyricsProvider.cs | 117 ++++++++++++++++++++++
Jellyfin.Api/Models/UserDtos/Lyric.cs | 18 ++++
Jellyfin.Api/Models/UserDtos/Lyrics.cs | 23 -----
Jellyfin.Api/Models/UserDtos/TxtLyricsProvider.cs | 81 +++++++++++++++
5 files changed, 250 insertions(+), 23 deletions(-)
create mode 100644 Jellyfin.Api/Models/UserDtos/ILyricsProvider.cs
create mode 100644 Jellyfin.Api/Models/UserDtos/LrcLyricsProvider.cs
create mode 100644 Jellyfin.Api/Models/UserDtos/Lyric.cs
delete mode 100644 Jellyfin.Api/Models/UserDtos/Lyrics.cs
create mode 100644 Jellyfin.Api/Models/UserDtos/TxtLyricsProvider.cs
(limited to 'Jellyfin.Api/Models')
diff --git a/Jellyfin.Api/Models/UserDtos/ILyricsProvider.cs b/Jellyfin.Api/Models/UserDtos/ILyricsProvider.cs
new file mode 100644
index 000000000..37f1f5bbe
--- /dev/null
+++ b/Jellyfin.Api/Models/UserDtos/ILyricsProvider.cs
@@ -0,0 +1,34 @@
+using System.Collections.ObjectModel;
+using MediaBrowser.Controller.Entities;
+
+namespace Jellyfin.Api.Models.UserDtos
+{
+ ///
+ /// Interface ILyricsProvider.
+ ///
+ public interface ILyricsProvider
+ {
+ ///
+ /// Gets a value indicating the File Extenstions this provider works with.
+ ///
+ public Collection? FileExtensions { get; }
+
+ ///
+ /// Gets a value indicating whether Process() generated data.
+ ///
+ /// true if data generated; otherwise, false.
+ bool HasData { get; }
+
+ ///
+ /// Gets Data object generated by Process() method.
+ ///
+ /// Object with data if no error occured; otherwise, null.
+ object? Data { get; }
+
+ ///
+ /// Opens lyric file for [the specified item], and processes it for API return.
+ ///
+ /// The item to to process.
+ void Process(BaseItem item);
+ }
+}
diff --git a/Jellyfin.Api/Models/UserDtos/LrcLyricsProvider.cs b/Jellyfin.Api/Models/UserDtos/LrcLyricsProvider.cs
new file mode 100644
index 000000000..029acd6ca
--- /dev/null
+++ b/Jellyfin.Api/Models/UserDtos/LrcLyricsProvider.cs
@@ -0,0 +1,117 @@
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Dynamic;
+using System.Globalization;
+using System.Linq;
+using LrcParser.Model;
+using LrcParser.Parser;
+using MediaBrowser.Controller.Entities;
+
+namespace Jellyfin.Api.Models.UserDtos
+{
+ ///
+ /// LRC File Lyric Provider.
+ ///
+ public class LrcLyricsProvider : ILyricsProvider
+ {
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public LrcLyricsProvider()
+ {
+ FileExtensions = new Collection
+ {
+ "lrc"
+ };
+ }
+
+ ///
+ /// Gets a value indicating the File Extenstions this provider works with.
+ ///
+ public Collection? FileExtensions { get; }
+
+ ///
+ /// Gets or Sets a value indicating whether Process() generated data.
+ ///
+ /// true if data generated; otherwise, false.
+ public bool HasData { get; set; }
+
+ ///
+ /// Gets or Sets Data object generated by Process() method.
+ ///
+ /// Object with data if no error occured; otherwise, null.
+ public object? Data { get; set; }
+
+ ///
+ /// Opens lyric file for [the specified item], and processes it for API return.
+ ///
+ /// The item to to process.
+ public void Process(BaseItem item)
+ {
+ string? lyricFilePath = Helpers.ItemHelper.GetLyricFilePath(item.Path);
+
+ if (string.IsNullOrEmpty(lyricFilePath))
+ {
+ return;
+ }
+
+ List lyricsList = new List();
+
+ List sortedLyricData = new List();
+ var metaData = new ExpandoObject() as IDictionary;
+ string lrcFileContent = System.IO.File.ReadAllText(lyricFilePath);
+
+ try
+ {
+ // Parse and sort lyric rows
+ LyricParser lrcLyricParser = new LrcParser.Parser.Lrc.LrcParser();
+ Song lyricData = lrcLyricParser.Decode(lrcFileContent);
+ sortedLyricData = lyricData.Lyrics.Where(x => x.TimeTags.Count > 0).OrderBy(x => x.TimeTags.ToArray()[0].Value).ToList();
+
+ // Parse metadata rows
+ var metaDataRows = lyricData.Lyrics
+ .Where(x => x.TimeTags.Count == 0)
+ .Where(x => x.Text.StartsWith("[", StringComparison.Ordinal) && x.Text.EndsWith("]", StringComparison.Ordinal))
+ .Select(x => x.Text)
+ .ToList();
+
+ foreach (string metaDataRow in metaDataRows)
+ {
+ var metaDataField = metaDataRow.Split(":");
+
+ string metaDataFieldName = metaDataField[0].Replace("[", string.Empty, StringComparison.Ordinal);
+ string metaDataFieldValue = metaDataField[1].Replace("]", string.Empty, StringComparison.Ordinal);
+
+ metaData.Add(metaDataFieldName, metaDataFieldValue);
+ }
+ }
+ catch
+ {
+ return;
+ }
+
+ if (!sortedLyricData.Any())
+ {
+ return;
+ }
+
+ for (int i = 0; i < sortedLyricData.Count; i++)
+ {
+ var timeData = sortedLyricData[i].TimeTags.ToArray()[0].Value;
+ double ticks = Convert.ToDouble(timeData, new NumberFormatInfo()) * 10000;
+ lyricsList.Add(new Lyric { Start = Math.Ceiling(ticks), Text = sortedLyricData[i].Text });
+ }
+
+ this.HasData = true;
+ if (metaData.Any())
+ {
+ this.Data = new { MetaData = metaData, lyrics = lyricsList };
+ }
+ else
+ {
+ this.Data = new { lyrics = lyricsList };
+ }
+ }
+ }
+}
diff --git a/Jellyfin.Api/Models/UserDtos/Lyric.cs b/Jellyfin.Api/Models/UserDtos/Lyric.cs
new file mode 100644
index 000000000..2794cd78a
--- /dev/null
+++ b/Jellyfin.Api/Models/UserDtos/Lyric.cs
@@ -0,0 +1,18 @@
+namespace Jellyfin.Api.Models.UserDtos
+{
+ ///
+ /// Lyric dto.
+ ///
+ public class Lyric
+ {
+ ///
+ /// Gets or sets the start time (ticks).
+ ///
+ public double Start { get; set; }
+
+ ///
+ /// Gets or sets the text.
+ ///
+ public string Text { get; set; } = string.Empty;
+ }
+}
diff --git a/Jellyfin.Api/Models/UserDtos/Lyrics.cs b/Jellyfin.Api/Models/UserDtos/Lyrics.cs
deleted file mode 100644
index cd548eb03..000000000
--- a/Jellyfin.Api/Models/UserDtos/Lyrics.cs
+++ /dev/null
@@ -1,23 +0,0 @@
-namespace Jellyfin.Api.Models.UserDtos
-{
- ///
- /// Lyric dto.
- ///
- public class Lyrics
- {
- ///
- /// Gets or sets the start.
- ///
- public double? Start { get; set; }
-
- ///
- /// Gets or sets the text.
- ///
- public string? Text { get; set; }
-
- ///
- /// Gets or sets the error.
- ///
- public string? Error { get; set; }
- }
-}
diff --git a/Jellyfin.Api/Models/UserDtos/TxtLyricsProvider.cs b/Jellyfin.Api/Models/UserDtos/TxtLyricsProvider.cs
new file mode 100644
index 000000000..03cce1ffb
--- /dev/null
+++ b/Jellyfin.Api/Models/UserDtos/TxtLyricsProvider.cs
@@ -0,0 +1,81 @@
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Dynamic;
+using System.Globalization;
+using System.Linq;
+using LrcParser.Model;
+using LrcParser.Parser;
+using MediaBrowser.Controller.Entities;
+
+namespace Jellyfin.Api.Models.UserDtos
+{
+ ///
+ /// TXT File Lyric Provider.
+ ///
+ public class TxtLyricsProvider : ILyricsProvider
+ {
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public TxtLyricsProvider()
+ {
+ FileExtensions = new Collection
+ {
+ "lrc", "txt"
+ };
+ }
+
+ ///
+ /// Gets a value indicating the File Extenstions this provider works with.
+ ///
+ public Collection? FileExtensions { get; }
+
+ ///
+ /// Gets or Sets a value indicating whether Process() generated data.
+ ///
+ /// true if data generated; otherwise, false.
+ public bool HasData { get; set; }
+
+ ///
+ /// Gets or Sets Data object generated by Process() method.
+ ///
+ /// Object with data if no error occured; otherwise, null.
+ public object? Data { get; set; }
+
+ ///
+ /// Opens lyric file for [the specified item], and processes it for API return.
+ ///
+ /// The item to to process.
+ public void Process(BaseItem item)
+ {
+ string? lyricFilePath = Helpers.ItemHelper.GetLyricFilePath(item.Path);
+
+ if (string.IsNullOrEmpty(lyricFilePath))
+ {
+ return;
+ }
+
+ List lyricsList = new List();
+
+ string lyricData = System.IO.File.ReadAllText(lyricFilePath);
+
+ // Splitting on Environment.NewLine caused some new lines to be missed in Windows.
+ char[] newLinedelims = new[] { '\r', '\n' };
+ string[] lyricTextLines = lyricData.Split(newLinedelims, StringSplitOptions.RemoveEmptyEntries);
+
+ if (!lyricTextLines.Any())
+ {
+ return;
+ }
+
+ foreach (string lyricLine in lyricTextLines)
+ {
+ lyricsList.Add(new Lyric { Text = lyricLine });
+ }
+
+ this.HasData = true;
+ this.Data = new { lyrics = lyricsList };
+ }
+ }
+}
--
cgit v1.2.3