Add TryReadInt to XmlReaderExtensions

This commit is contained in:
Patrick Barron 2023-10-06 15:35:26 -04:00
parent 8a7a1cc723
commit 0e51ffa169
5 changed files with 62 additions and 166 deletions

View File

@ -26,6 +26,19 @@ public static class XmlReaderExtensions
return reader.ReadElementContentAsString().Trim();
}
/// <summary>
/// Reads an int from the current node.
/// </summary>
/// <param name="reader">The <see cref="XmlReader"/>.</param>
/// <param name="value">The parsed <c>int</c>.</param>
/// <returns>A value indicating whether the parsing succeeded.</returns>
public static bool TryReadInt(this XmlReader reader, out int value)
{
ArgumentNullException.ThrowIfNull(reader);
return int.TryParse(reader.ReadElementContentAsString(), CultureInfo.InvariantCulture, out value);
}
/// <summary>
/// Parses a <see cref="DateTime"/> from the current node.
/// </summary>

View File

@ -234,20 +234,16 @@ namespace MediaBrowser.LocalMetadata.Parsers
item.CustomRating = reader.ReadNormalizedString();
break;
case "RunningTime":
var runtimeText = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(runtimeText))
{
var text = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(text))
{
if (int.TryParse(text.AsSpan().LeftPart(' '), NumberStyles.Integer, CultureInfo.InvariantCulture, out var runtime))
if (int.TryParse(runtimeText.AsSpan().LeftPart(' '), NumberStyles.Integer, CultureInfo.InvariantCulture, out var runtime))
{
item.RunTimeTicks = TimeSpan.FromMinutes(runtime).Ticks;
}
}
break;
}
case "AspectRatio":
var aspectRatio = reader.ReadNormalizedString();
if (!string.IsNullOrEmpty(aspectRatio) && item is IHasAspectRatio hasAspectRatio)
@ -256,7 +252,6 @@ namespace MediaBrowser.LocalMetadata.Parsers
}
break;
case "LockData":
{
var val = reader.ReadElementContentAsString();
@ -336,20 +331,12 @@ namespace MediaBrowser.LocalMetadata.Parsers
}
case "ProductionYear":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
if (int.TryParse(val, out var productionYear) && productionYear > 1850)
if (reader.TryReadInt(out var productionYear) && productionYear > 1850)
{
item.ProductionYear = productionYear;
}
}
break;
}
case "Rating":
case "IMDBrating":
{

View File

@ -325,9 +325,7 @@ namespace MediaBrowser.XbmcMetadata.Parsers
}
case "playcount":
{
var val = reader.ReadElementContentAsString();
if (int.TryParse(val, NumberStyles.Integer, CultureInfo.InvariantCulture, out var count)
if (reader.TryReadInt(out var count)
&& Guid.TryParse(nfoConfiguration.UserId, out var playCountUserId))
{
var user = _userManager.GetUserById(playCountUserId);
@ -337,8 +335,6 @@ namespace MediaBrowser.XbmcMetadata.Parsers
}
break;
}
case "lastplayed":
if (reader.TryReadDateTime(Logger, out var lastPlayed)
&& Guid.TryParse(nfoConfiguration.UserId, out var lastPlayedUserId))
@ -398,17 +394,13 @@ namespace MediaBrowser.XbmcMetadata.Parsers
item.CustomRating = reader.ReadNormalizedString();
break;
case "runtime":
{
var text = reader.ReadElementContentAsString();
if (int.TryParse(text.AsSpan().LeftPart(' '), NumberStyles.Integer, CultureInfo.InvariantCulture, out var runtime))
var runtimeText = reader.ReadElementContentAsString();
if (int.TryParse(runtimeText.AsSpan().LeftPart(' '), NumberStyles.Integer, CultureInfo.InvariantCulture, out var runtime))
{
item.RunTimeTicks = TimeSpan.FromMinutes(runtime).Ticks;
}
break;
}
case "aspectratio":
var aspectRatio = reader.ReadNormalizedString();
if (!string.IsNullOrEmpty(aspectRatio) && item is IHasAspectRatio hasAspectRatio)
@ -502,17 +494,12 @@ namespace MediaBrowser.XbmcMetadata.Parsers
break;
case "year":
{
var val = reader.ReadElementContentAsString();
if (int.TryParse(val, out var productionYear) && productionYear > 1850)
if (reader.TryReadInt(out var productionYear) && productionYear > 1850)
{
item.ProductionYear = productionYear;
}
break;
}
case "rating":
{
var rating = reader.ReadElementContentAsString();

View File

@ -1,5 +1,4 @@
using System;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Xml;
@ -113,130 +112,49 @@ namespace MediaBrowser.XbmcMetadata.Parsers
switch (reader.Name)
{
case "season":
if (reader.TryReadInt(out var seasonNumber))
{
var number = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(number))
{
if (int.TryParse(number, out var num))
{
item.ParentIndexNumber = num;
}
item.ParentIndexNumber = seasonNumber;
}
break;
}
case "episode":
if (reader.TryReadInt(out var episodeNumber))
{
var number = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(number))
{
if (int.TryParse(number, out var num))
{
item.IndexNumber = num;
}
item.IndexNumber = episodeNumber;
}
break;
}
case "episodenumberend":
if (reader.TryReadInt(out var episodeNumberEnd))
{
var number = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(number))
{
if (int.TryParse(number, out var num))
{
item.IndexNumberEnd = num;
}
item.IndexNumberEnd = episodeNumberEnd;
}
break;
}
case "airsbefore_episode":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
// int.TryParse is local aware, so it can be problematic, force us culture
if (int.TryParse(val, NumberStyles.Integer, CultureInfo.InvariantCulture, out var rval))
{
item.AirsBeforeEpisodeNumber = rval;
}
}
break;
}
case "airsafter_season":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
// int.TryParse is local aware, so it can be problematic, force us culture
if (int.TryParse(val, NumberStyles.Integer, CultureInfo.InvariantCulture, out var rval))
{
item.AirsAfterSeasonNumber = rval;
}
}
break;
}
case "airsbefore_season":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
// int.TryParse is local aware, so it can be problematic, force us culture
if (int.TryParse(val, NumberStyles.Integer, CultureInfo.InvariantCulture, out var rval))
{
item.AirsBeforeSeasonNumber = rval;
}
}
break;
}
case "displayseason":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
// int.TryParse is local aware, so it can be problematic, force us culture
if (int.TryParse(val, NumberStyles.Integer, CultureInfo.InvariantCulture, out var rval))
{
item.AirsBeforeSeasonNumber = rval;
}
}
break;
}
case "displayepisode":
if (reader.TryReadInt(out var airsBeforeEpisode))
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
// int.TryParse is local aware, so it can be problematic, force us culture
if (int.TryParse(val, NumberStyles.Integer, CultureInfo.InvariantCulture, out var rval))
{
item.AirsBeforeEpisodeNumber = rval;
}
item.AirsBeforeEpisodeNumber = airsBeforeEpisode;
}
break;
case "airsafter_season":
if (reader.TryReadInt(out var airsAfterSeason))
{
item.AirsAfterSeasonNumber = airsAfterSeason;
}
break;
case "airsbefore_season":
case "displayseason":
if (reader.TryReadInt(out var airsBeforeSeason))
{
item.AirsBeforeSeasonNumber = airsBeforeSeason;
}
break;
case "showtitle":
item.SeriesName = reader.ReadNormalizedString();
break;

View File

@ -1,4 +1,3 @@
using System.Globalization;
using System.Xml;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Entities.TV;
@ -42,20 +41,12 @@ namespace MediaBrowser.XbmcMetadata.Parsers
switch (reader.Name)
{
case "seasonnumber":
if (reader.TryReadInt(out var seasonNumber))
{
var number = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(number))
{
if (int.TryParse(number, NumberStyles.Integer, CultureInfo.InvariantCulture, out var num))
{
item.IndexNumber = num;
}
item.IndexNumber = seasonNumber;
}
break;
}
case "seasonname":
item.Name = reader.ReadNormalizedString();
break;