Skip to content

Commit

Permalink
Extend Rating and StarRating
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadowghost committed Dec 17, 2024
1 parent 333ce47 commit 7f8dcf5
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/Jellyfin.XmlTv/Entities/XmlTvProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ public XmlTvProgram(string channelId)
/// <summary>
/// Gets or sets the rating.
/// </summary>
public XmlTvRating? Rating { get; set; }
public List<XmlTvRating>? Ratings { get; set; }

/// <summary>
/// Gets or sets the star rating.
/// Gets or sets the star ratings.
/// </summary>
public float? StarRating { get; set; }
public List<XmlTvStarRating>? StarRatings { get; set; }

/// <summary>
/// Gets or sets the images.
Expand Down
25 changes: 13 additions & 12 deletions src/Jellyfin.XmlTv/Entities/XmlTvRating.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
#pragma warning disable CA1002 // Do not expose generic lists
#pragma warning disable CA2227 // Collection properties should be read only

using System.Collections.Generic;
using System.Text;
using Jellyfin.XmlTv.Interfaces;

namespace Jellyfin.XmlTv.Entities;

/// <summary>
/// Describes the rating (certification) applied to a program.
/// </summary>
public class XmlTvRating
public class XmlTvRating : IHasIcons
{
/// <summary>
/// Initializes a new instance of the <see cref="XmlTvRating"/> class.
/// </summary>
/// <param name="value">The rating.</param>
public XmlTvRating(string value)
{
Value = value;
}

/// <summary>
/// Gets or sets the literal name of the rating system.
/// </summary>
/// Example: MPAA
public string? System { get; set; }

/// <summary>
/// Gets the rating using the system specified.
/// Gets or sets the rating using the system specified.
/// </summary>
// Example: TV-14
public string Value { get; }
public string? Value { get; set; }

/// <summary>
/// Gets or sets the icons.
/// </summary>
public List<XmlTvIcon>? Icons { get; set; }

/// <inheritdoc />
public override string ToString()
Expand Down
44 changes: 44 additions & 0 deletions src/Jellyfin.XmlTv/Entities/XmlTvStarRating.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma warning disable CA1002 // Do not expose generic lists
#pragma warning disable CA2227 // Collection properties should be read only

using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Jellyfin.XmlTv.Interfaces;

namespace Jellyfin.XmlTv.Entities;

/// <summary>
/// StarRating class.
/// </summary>
public class XmlTvStarRating : IHasIcons
{
/// <summary>
/// Gets or sets the system.
/// </summary>
/// Example: TV Guide
public string? System { get; set; }

/// <summary>
/// Gets or setsthe star rating.
/// </summary>
public decimal? StarRating { get; set; }

/// <summary>
/// Gets or sets the icons.
/// </summary>
public List<XmlTvIcon>? Icons { get; set; }

/// <inheritdoc />
public override string ToString()
{
var builder = new StringBuilder(StarRating?.ToString(CultureInfo.InvariantCulture));

if (!string.IsNullOrEmpty(System))
{
builder.Append(" (").Append(System).Append(')');
}

return builder.ToString();
}
}
115 changes: 96 additions & 19 deletions src/Jellyfin.XmlTv/XmlTvReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ public void ProcessCredits(XmlReader creditsXml, XmlTvProgram result)
}
else
{
result.Credits!.Add(credit);
result.Credits.Add(credit);
}
}
}
Expand All @@ -457,52 +457,129 @@ public void ProcessCredits(XmlReader creditsXml, XmlTvProgram result)
public void ProcessStarRating(XmlReader reader, XmlTvProgram result)
{
/*
<star-rating>
<value>3/3</value>
<star-rating system="TV Guide">
<value>4/5</value>
<icon src="stars.png" />
</star-rating>
*/

reader.ReadToDescendant("value");
if (reader.Name == "value")
var rating = new XmlTvStarRating();
var system = reader.GetAttribute("system");
if (!string.IsNullOrEmpty(system))
{
rating.System = system;
}

// Loop through each element
using var starRatingXml = reader.ReadSubtree();
starRatingXml.MoveToContent();
starRatingXml.Read();
while (!starRatingXml.EOF && starRatingXml.ReadState == ReadState.Interactive)
{
var textValue = reader.ReadElementContentAsString();
int index = textValue.IndexOf('/', StringComparison.Ordinal);
if (index != -1)
if (starRatingXml.NodeType == XmlNodeType.Element)
{
var substring = textValue.Substring(index);
if (float.TryParse(substring, out var value))
var nodeName = starRatingXml.Name;
if (string.Equals(nodeName, "value", StringComparison.OrdinalIgnoreCase))
{
// Value
var textValue = reader.ReadElementContentAsString();
int index = textValue.IndexOf('/', StringComparison.Ordinal);
if (index != -1)
{
var substring = textValue.Substring(index);
if (decimal.TryParse(substring, out var value))
{
rating.StarRating = value;
}
else
{
starRatingXml.Skip();
return;
}
}
}
else if (string.Equals(nodeName, "icon", StringComparison.OrdinalIgnoreCase))
{
result.StarRating = value;
// Icon
ProcessIconNode(starRatingXml, rating);
}

starRatingXml.Skip();
}
else
{
starRatingXml.Skip();
}
}

if (result.StarRatings is null)
{
result.StarRatings = [rating];
}
else
{
reader.Skip();
result.StarRatings.Add(rating);
}
}

public void ProcessRating(XmlReader reader, XmlTvProgram result)
{
/*
<rating system="MPAA">
<value>TV-G</value>
<value>NC-17</value>
<icon src="NC-17_symbol.png" />
</rating>
*/

var rating = new XmlTvRating();
var system = reader.GetAttribute("system");
if (!string.IsNullOrEmpty(system))
{
rating.System = system;
}

reader.ReadToDescendant("value");
if (reader.Name == "value")
// Loop through each element
using var starRatingXml = reader.ReadSubtree();
starRatingXml.MoveToContent();
starRatingXml.Read();
while (!starRatingXml.EOF && starRatingXml.ReadState == ReadState.Interactive)
{
result.Rating = new XmlTvRating(reader.ReadElementContentAsString())
if (starRatingXml.NodeType == XmlNodeType.Element)
{
System = system
};
var nodeName = starRatingXml.Name;
if (string.Equals(nodeName, "value", StringComparison.OrdinalIgnoreCase))
{
// Value
var value = reader.ReadElementContentAsString();
if (string.IsNullOrEmpty(value))
{
starRatingXml.Skip();
return;
}

rating.Value = value;
}
else if (string.Equals(nodeName, "icon", StringComparison.OrdinalIgnoreCase))
{
// Icon
ProcessIconNode(starRatingXml, rating);
}

starRatingXml.Skip();
}
else
{
starRatingXml.Skip();
}
}

if (result.Ratings is null)
{
result.Ratings = [rating];
}
else
{
reader.Skip();
result.Ratings.Add(rating);
}
}

Expand Down

0 comments on commit 7f8dcf5

Please sign in to comment.