mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-06-23 15:30:56 -04:00
Add GetStringArray and GetPersonArray to XmlReaderExtensions
This commit is contained in:
parent
bdca4ed322
commit
1a6ec2c740
@ -1,5 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
using System.Linq;
|
||||||
using System.Xml;
|
using System.Xml;
|
||||||
using Jellyfin.Data.Enums;
|
using Jellyfin.Data.Enums;
|
||||||
using MediaBrowser.Controller.Entities;
|
using MediaBrowser.Controller.Entities;
|
||||||
@ -104,4 +106,40 @@ public static class XmlReaderExtensions
|
|||||||
ImageUrl = imageUrl
|
ImageUrl = imageUrl
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Used to split names of comma or pipe delimited genres and people.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="reader">The <see cref="XmlReader"/>.</param>
|
||||||
|
/// <returns>IEnumerable{System.String}.</returns>
|
||||||
|
public static IEnumerable<string> GetStringArray(this XmlReader reader)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(reader);
|
||||||
|
var value = reader.ReadElementContentAsString();
|
||||||
|
|
||||||
|
// Only split by comma if there is no pipe in the string
|
||||||
|
// We have to be careful to not split names like Matthew, Jr.
|
||||||
|
var separator = !value.Contains('|', StringComparison.Ordinal)
|
||||||
|
&& !value.Contains(';', StringComparison.Ordinal)
|
||||||
|
? new[] { ',' }
|
||||||
|
: new[] { '|', ';' };
|
||||||
|
|
||||||
|
foreach (var part in value.Trim().Trim(separator).Split(separator))
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrWhiteSpace(part))
|
||||||
|
{
|
||||||
|
yield return part.Trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parses a <see cref="PersonInfo"/> array from the xml node.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="reader">The <see cref="XmlReader"/>.</param>
|
||||||
|
/// <param name="personKind">The <see cref="PersonKind"/>.</param>
|
||||||
|
/// <returns>The <see cref="IEnumerable{PersonInfo}"/>.</returns>
|
||||||
|
public static IEnumerable<PersonInfo> GetPersonArray(this XmlReader reader, PersonKind personKind)
|
||||||
|
=> reader.GetStringArray()
|
||||||
|
.Select(part => new PersonInfo { Name = part, Type = personKind });
|
||||||
}
|
}
|
||||||
|
@ -354,93 +354,40 @@ namespace MediaBrowser.LocalMetadata.Parsers
|
|||||||
}
|
}
|
||||||
|
|
||||||
case "Network":
|
case "Network":
|
||||||
|
foreach (var name in reader.GetStringArray())
|
||||||
{
|
{
|
||||||
foreach (var name in SplitNames(reader.ReadElementContentAsString()))
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(name))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
item.AddStudio(name);
|
item.AddStudio(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
case "Director":
|
case "Director":
|
||||||
|
foreach (var director in reader.GetPersonArray(PersonKind.Director))
|
||||||
{
|
{
|
||||||
foreach (var p in SplitNames(reader.ReadElementContentAsString()).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonKind.Director }))
|
itemResult.AddPerson(director);
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(p.Name))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
itemResult.AddPerson(p);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
case "Writer":
|
case "Writer":
|
||||||
|
foreach (var writer in reader.GetPersonArray(PersonKind.Writer))
|
||||||
{
|
{
|
||||||
foreach (var p in SplitNames(reader.ReadElementContentAsString()).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonKind.Writer }))
|
itemResult.AddPerson(writer);
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(p.Name))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
itemResult.AddPerson(p);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
case "Actors":
|
case "Actors":
|
||||||
|
foreach (var actor in reader.GetPersonArray(PersonKind.Actor))
|
||||||
{
|
{
|
||||||
var actors = reader.ReadInnerXml();
|
itemResult.AddPerson(actor);
|
||||||
|
|
||||||
if (actors.Contains('<', StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
// This is one of the mis-named "Actors" full nodes created by MB2
|
|
||||||
// Create a reader and pass it to the persons node processor
|
|
||||||
using var xmlReader = XmlReader.Create(new StringReader($"<Persons>{actors}</Persons>"));
|
|
||||||
FetchDataFromPersonsNode(xmlReader, itemResult);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Old-style piped string
|
|
||||||
foreach (var p in SplitNames(actors).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonKind.Actor }))
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(p.Name))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
itemResult.AddPerson(p);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
case "GuestStars":
|
case "GuestStars":
|
||||||
|
foreach (var guestStar in reader.GetPersonArray(PersonKind.GuestStar))
|
||||||
{
|
{
|
||||||
foreach (var p in SplitNames(reader.ReadElementContentAsString()).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonKind.GuestStar }))
|
itemResult.AddPerson(guestStar);
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(p.Name))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
itemResult.AddPerson(p);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
case "Trailer":
|
case "Trailer":
|
||||||
{
|
{
|
||||||
var val = reader.ReadElementContentAsString();
|
var val = reader.ReadElementContentAsString();
|
||||||
@ -1129,34 +1076,5 @@ namespace MediaBrowser.LocalMetadata.Parsers
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Used to split names of comma or pipe delimited genres and people.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">The value.</param>
|
|
||||||
/// <returns>IEnumerable{System.String}.</returns>
|
|
||||||
private IEnumerable<string> SplitNames(string value)
|
|
||||||
{
|
|
||||||
// Only split by comma if there is no pipe in the string
|
|
||||||
// We have to be careful to not split names like Matthew, Jr.
|
|
||||||
var separator = !value.Contains('|', StringComparison.Ordinal)
|
|
||||||
&& !value.Contains(';', StringComparison.Ordinal) ? new[] { ',' } : new[] { '|', ';' };
|
|
||||||
|
|
||||||
value = value.Trim().Trim(separator);
|
|
||||||
|
|
||||||
return string.IsNullOrWhiteSpace(value) ? Array.Empty<string>() : Split(value, separator, StringSplitOptions.RemoveEmptyEntries);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Provides an additional overload for string.split.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="val">The val.</param>
|
|
||||||
/// <param name="separators">The separators.</param>
|
|
||||||
/// <param name="options">The options.</param>
|
|
||||||
/// <returns>System.String[][].</returns>
|
|
||||||
private string[] Split(string val, char[] separators, StringSplitOptions options)
|
|
||||||
{
|
|
||||||
return val.Split(separators, options);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -527,21 +527,12 @@ namespace MediaBrowser.XbmcMetadata.Parsers
|
|||||||
}
|
}
|
||||||
|
|
||||||
case "director":
|
case "director":
|
||||||
|
foreach (var director in reader.GetPersonArray(PersonKind.Director))
|
||||||
{
|
{
|
||||||
var val = reader.ReadElementContentAsString();
|
itemResult.AddPerson(director);
|
||||||
foreach (var p in SplitNames(val).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonKind.Director }))
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(p.Name))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
itemResult.AddPerson(p);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
case "credits":
|
case "credits":
|
||||||
{
|
{
|
||||||
var val = reader.ReadElementContentAsString();
|
var val = reader.ReadElementContentAsString();
|
||||||
@ -566,21 +557,12 @@ namespace MediaBrowser.XbmcMetadata.Parsers
|
|||||||
}
|
}
|
||||||
|
|
||||||
case "writer":
|
case "writer":
|
||||||
|
foreach (var writer in reader.GetPersonArray(PersonKind.Writer))
|
||||||
{
|
{
|
||||||
var val = reader.ReadElementContentAsString();
|
itemResult.AddPerson(writer);
|
||||||
foreach (var p in SplitNames(val).Select(v => new PersonInfo { Name = v.Trim(), Type = PersonKind.Writer }))
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(p.Name))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
itemResult.AddPerson(p);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
case "actor":
|
case "actor":
|
||||||
var person = reader.GetPersonFromXmlNode();
|
var person = reader.GetPersonFromXmlNode();
|
||||||
if (person is not null)
|
if (person is not null)
|
||||||
@ -1192,24 +1174,6 @@ namespace MediaBrowser.XbmcMetadata.Parsers
|
|||||||
IgnoreComments = true
|
IgnoreComments = true
|
||||||
};
|
};
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Used to split names of comma or pipe delimited genres and people.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">The value.</param>
|
|
||||||
/// <returns>IEnumerable{System.String}.</returns>
|
|
||||||
private IEnumerable<string> SplitNames(string value)
|
|
||||||
{
|
|
||||||
// Only split by comma if there is no pipe in the string
|
|
||||||
// We have to be careful to not split names like Matthew, Jr.
|
|
||||||
var separator = !value.Contains('|', StringComparison.Ordinal) && !value.Contains(';', StringComparison.Ordinal)
|
|
||||||
? new[] { ',' }
|
|
||||||
: new[] { '|', ';' };
|
|
||||||
|
|
||||||
value = value.Trim().Trim(separator);
|
|
||||||
|
|
||||||
return string.IsNullOrWhiteSpace(value) ? Array.Empty<string>() : value.Split(separator, StringSplitOptions.RemoveEmptyEntries);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Parses the <see cref="ImageType"/> from the NFO aspect property.
|
/// Parses the <see cref="ImageType"/> from the NFO aspect property.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user