better remove RemoveAccent logic.

pull/2/head
kay.one 11 years ago
parent 9c8c485c98
commit c5c02f08f8

@ -125,7 +125,6 @@
<Compile Include="Messaging\TestCommandExecutor.cs" /> <Compile Include="Messaging\TestCommandExecutor.cs" />
<Compile Include="Reflection\ReflectionExtensions.cs" /> <Compile Include="Reflection\ReflectionExtensions.cs" />
<Compile Include="ServiceFactory.cs" /> <Compile Include="ServiceFactory.cs" />
<Compile Include="StringExtension.cs" />
<Compile Include="HttpProvider.cs" /> <Compile Include="HttpProvider.cs" />
<Compile Include="ConsoleService.cs" /> <Compile Include="ConsoleService.cs" />
<Compile Include="Contract\ReportBase.cs" /> <Compile Include="Contract\ReportBase.cs" />

@ -1,24 +0,0 @@
using System;
using System.Linq;
namespace NzbDrone.Common
{
public static class StringExtension
{
public static string NullSafe(this string target)
{
return ((object)target).NullSafe().ToString();
}
public static object NullSafe(this object target)
{
if (target != null) return target;
return "[NULL]";
}
public static string FirstCharToUpper(this string input)
{
return input.First().ToString().ToUpper() + String.Join("", input.Skip(1));
}
}
}

@ -1,3 +1,6 @@
using System;
using System.Globalization;
using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -5,42 +8,49 @@ namespace NzbDrone.Common
{ {
public static class StringExtensions public static class StringExtensions
{ {
public static string Inject(this string format, params object[] formattingArgs) public static string NullSafe(this string target)
{ {
return string.Format(format, formattingArgs); return ((object)target).NullSafe().ToString();
} }
private static readonly Regex InvalidCharRegex = new Regex(@"[^a-zA-Z0-9\s-]", RegexOptions.Compiled); public static object NullSafe(this object target)
private static readonly Regex InvalidSearchCharRegex = new Regex(@"[^a-zA-Z0-9\s-\.]", RegexOptions.Compiled);
private static readonly Regex CollapseSpace = new Regex(@"\s+", RegexOptions.Compiled);
public static string ToSlug(this string phrase)
{ {
phrase = phrase.RemoveAccent().ToLower(); if (target != null) return target;
return "[NULL]";
}
phrase = InvalidCharRegex.Replace(phrase, string.Empty); public static string FirstCharToUpper(this string input)
phrase = CollapseSpace.Replace(phrase, " ").Trim(); {
phrase = phrase.Replace(" ", "-"); return input.First().ToString().ToUpper() + String.Join("", input.Skip(1));
}
return phrase; public static string Inject(this string format, params object[] formattingArgs)
{
return string.Format(format, formattingArgs);
} }
public static string ToSearchTerm(this string phrase) private static readonly Regex CollapseSpace = new Regex(@"\s+", RegexOptions.Compiled);
public static string RemoveAccent(this string text)
{ {
phrase = phrase.RemoveAccent().ToLower(); var normalizedString = text.Normalize(NormalizationForm.FormD);
var stringBuilder = new StringBuilder();
phrase = phrase.Replace("&", "and"); foreach (var c in normalizedString)
phrase = InvalidSearchCharRegex.Replace(phrase, string.Empty); {
phrase = CollapseSpace.Replace(phrase, " ").Trim(); var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
phrase = phrase.Replace(" ", "+"); if (unicodeCategory != UnicodeCategory.NonSpacingMark)
{
stringBuilder.Append(c);
}
}
return phrase; return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
} }
public static string RemoveAccent(this string txt) public static string CleanSpaces(this string text)
{ {
var bytes = Encoding.GetEncoding("Cyrillic").GetBytes(txt); return CollapseSpace.Replace(text, " ").Trim();
return Encoding.ASCII.GetString(bytes);
} }
} }
} }

@ -2,7 +2,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Core.MediaCover; using NzbDrone.Core.MediaCover;
@ -19,12 +18,25 @@ namespace NzbDrone.Core.MetadataSource
public List<Series> SearchForNewSeries(string title) public List<Series> SearchForNewSeries(string title)
{ {
var client = BuildClient("search", "shows"); var client = BuildClient("search", "shows");
var restRequest = new RestRequest(title.ToSearchTerm()); var restRequest = new RestRequest(GetSearchTerm(title));
var response = client.ExecuteAndValidate<List<Show>>(restRequest); var response = client.ExecuteAndValidate<List<Show>>(restRequest);
return response.Select(MapSeries).ToList(); return response.Select(MapSeries).ToList();
} }
private static readonly Regex InvalidSearchCharRegex = new Regex(@"[^a-zA-Z0-9\s-\.]", RegexOptions.Compiled);
private static string GetSearchTerm(string phrase)
{
phrase = phrase.RemoveAccent().ToLower();
phrase = phrase.Replace("&", "and");
phrase = InvalidSearchCharRegex.Replace(phrase, string.Empty);
phrase = phrase.CleanSpaces().Replace(" ", "+");
return phrase;
}
public Tuple<Series, List<Episode>> GetSeriesInfo(int tvDbSeriesId) public Tuple<Series, List<Episode>> GetSeriesInfo(int tvDbSeriesId)
{ {
var client = BuildClient("show", "summary"); var client = BuildClient("show", "summary");

Loading…
Cancel
Save