2020-02-09 02:35:16 +00:00
|
|
|
using System.Globalization;
|
2017-11-05 09:42:03 +00:00
|
|
|
using System.Linq;
|
2017-04-15 08:45:10 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2017-11-05 09:42:03 +00:00
|
|
|
using Microsoft.AspNetCore.WebUtilities;
|
2017-04-15 08:45:10 +00:00
|
|
|
|
2018-03-10 08:05:56 +00:00
|
|
|
namespace Jackett.Common.Utils
|
2017-04-15 08:45:10 +00:00
|
|
|
{
|
|
|
|
public static class ParseUtil
|
|
|
|
{
|
|
|
|
private static readonly Regex InvalidXmlChars =
|
|
|
|
new Regex(
|
|
|
|
@"(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]",
|
|
|
|
RegexOptions.Compiled);
|
2019-10-06 19:32:22 +00:00
|
|
|
private static readonly Regex ImdbId = new Regex(@"^(?:tt)?(\d{1,8})$", RegexOptions.Compiled);
|
2017-04-15 08:45:10 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public static string NormalizeSpace(string s) => s.Trim();
|
2017-04-15 08:45:10 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public static string NormalizeMultiSpaces(string s) =>
|
|
|
|
new Regex(@"\s+").Replace(NormalizeSpace(s), " ");
|
2017-04-15 08:45:10 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public static string NormalizeNumber(string s) =>
|
|
|
|
NormalizeSpace(s)
|
|
|
|
.Replace("-", "0")
|
|
|
|
.Replace(",", "");
|
2017-04-15 08:45:10 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public static string RemoveInvalidXmlChars(string text) => string.IsNullOrEmpty(text) ? "" : InvalidXmlChars.Replace(text, "");
|
2017-04-15 08:45:10 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public static double CoerceDouble(string str) => double.Parse(NormalizeNumber(str), NumberStyles.Any, CultureInfo.InvariantCulture);
|
2017-04-15 08:45:10 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public static float CoerceFloat(string str) => float.Parse(NormalizeNumber(str), NumberStyles.Any, CultureInfo.InvariantCulture);
|
2017-04-15 08:45:10 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public static int CoerceInt(string str) => int.Parse(NormalizeNumber(str), NumberStyles.Any, CultureInfo.InvariantCulture);
|
2017-04-15 08:45:10 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public static long CoerceLong(string str) => long.Parse(NormalizeNumber(str), NumberStyles.Any, CultureInfo.InvariantCulture);
|
2017-04-15 08:45:10 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public static bool TryCoerceDouble(string str, out double result) => double.TryParse(NormalizeNumber(str), NumberStyles.Any, CultureInfo.InvariantCulture, out result);
|
2017-04-15 08:45:10 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public static bool TryCoerceFloat(string str, out float result) => float.TryParse(NormalizeNumber(str), NumberStyles.Any, CultureInfo.InvariantCulture, out result);
|
2017-04-15 08:45:10 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public static bool TryCoerceInt(string str, out int result) => int.TryParse(NormalizeNumber(str), NumberStyles.Any, CultureInfo.InvariantCulture, out result);
|
2017-04-15 08:45:10 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public static bool TryCoerceLong(string str, out long result) => long.TryParse(NormalizeNumber(str), NumberStyles.Any, CultureInfo.InvariantCulture, out result);
|
2017-04-15 08:45:10 +00:00
|
|
|
|
|
|
|
public static string GetArgumentFromQueryString(string url, string argument)
|
|
|
|
{
|
|
|
|
if (url == null || argument == null)
|
|
|
|
return null;
|
|
|
|
var qsStr = url.Split(new char[] { '?' }, 2)[1];
|
|
|
|
qsStr = qsStr.Split(new char[] { '#' }, 2)[0];
|
2017-11-05 09:42:03 +00:00
|
|
|
var qs = QueryHelpers.ParseQuery(qsStr);
|
|
|
|
return qs[argument].FirstOrDefault();
|
2017-04-15 08:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static long? GetLongFromString(string str)
|
|
|
|
{
|
|
|
|
if (str == null)
|
|
|
|
return null;
|
2020-02-10 22:16:19 +00:00
|
|
|
var IdRegEx = new Regex(@"(\d+)", RegexOptions.Compiled);
|
2017-04-15 08:45:10 +00:00
|
|
|
var IdMatch = IdRegEx.Match(str);
|
|
|
|
if (!IdMatch.Success)
|
|
|
|
return null;
|
|
|
|
var Id = IdMatch.Groups[1].Value;
|
|
|
|
return CoerceLong(Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int? GetImdbID(string imdbstr)
|
|
|
|
{
|
|
|
|
if (imdbstr == null)
|
|
|
|
return null;
|
|
|
|
var match = ImdbId.Match(imdbstr);
|
|
|
|
if (!match.Success)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return int.Parse(match.Groups[1].Value, NumberStyles.Any, CultureInfo.InvariantCulture);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string GetFullImdbID(string imdbstr)
|
|
|
|
{
|
|
|
|
var imdbid = GetImdbID(imdbstr);
|
|
|
|
if (imdbid == null)
|
|
|
|
return null;
|
2020-02-09 02:35:16 +00:00
|
|
|
|
2019-10-07 21:51:18 +00:00
|
|
|
return "tt" + ((int)imdbid).ToString("D7");
|
2017-04-15 08:45:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-11 15:36:29 +00:00
|
|
|
}
|