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(); 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> /// <summary>
/// Parses a <see cref="DateTime"/> from the current node. /// Parses a <see cref="DateTime"/> from the current node.
/// </summary> /// </summary>

View File

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

View File

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

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Globalization;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using System.Xml; using System.Xml;
@ -113,130 +112,49 @@ namespace MediaBrowser.XbmcMetadata.Parsers
switch (reader.Name) switch (reader.Name)
{ {
case "season": case "season":
if (reader.TryReadInt(out var seasonNumber))
{ {
var number = reader.ReadElementContentAsString(); item.ParentIndexNumber = seasonNumber;
if (!string.IsNullOrWhiteSpace(number))
{
if (int.TryParse(number, out var num))
{
item.ParentIndexNumber = num;
}
}
break;
} }
break;
case "episode": case "episode":
if (reader.TryReadInt(out var episodeNumber))
{ {
var number = reader.ReadElementContentAsString(); item.IndexNumber = episodeNumber;
if (!string.IsNullOrWhiteSpace(number))
{
if (int.TryParse(number, out var num))
{
item.IndexNumber = num;
}
}
break;
} }
break;
case "episodenumberend": case "episodenumberend":
if (reader.TryReadInt(out var episodeNumberEnd))
{ {
var number = reader.ReadElementContentAsString(); item.IndexNumberEnd = episodeNumberEnd;
if (!string.IsNullOrWhiteSpace(number))
{
if (int.TryParse(number, out var num))
{
item.IndexNumberEnd = num;
}
}
break;
} }
break;
case "airsbefore_episode": 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": case "displayepisode":
if (reader.TryReadInt(out var airsBeforeEpisode))
{ {
var val = reader.ReadElementContentAsString(); item.AirsBeforeEpisodeNumber = airsBeforeEpisode;
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;
} }
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": case "showtitle":
item.SeriesName = reader.ReadNormalizedString(); item.SeriesName = reader.ReadNormalizedString();
break; break;

View File

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