Move UnicodeToAscii to Dinah.Core, minor refactoring

This commit is contained in:
Robert McRackan 2021-07-01 11:46:23 -04:00
parent 7ff4953f7b
commit 08644fb937
2 changed files with 29 additions and 119 deletions

View File

@ -1,5 +1,6 @@
using System.Linq; using System;
using System.Text.RegularExpressions; using System.Linq;
using Dinah.Core;
using TagLib; using TagLib;
using TagLib.Mpeg4; using TagLib.Mpeg4;
@ -7,58 +8,40 @@ namespace AaxDecrypter
{ {
public class AaxcTagLibFile : TagLib.Mpeg4.File public class AaxcTagLibFile : TagLib.Mpeg4.File
{ {
// ©
private const byte COPYRIGHT = 0xa9;
private static ReadOnlyByteVector narratorType { get; } = new ReadOnlyByteVector(COPYRIGHT, (byte)'n', (byte)'r', (byte)'t');
private static ReadOnlyByteVector descriptionType { get; } = new ReadOnlyByteVector(COPYRIGHT, (byte)'d', (byte)'e', (byte)'s');
private static ReadOnlyByteVector publisherType { get; } = new ReadOnlyByteVector(COPYRIGHT, (byte)'p', (byte)'u', (byte)'b');
public string AsciiTitleSansUnabridged => TitleSansUnabridged?.UnicodeToAscii();
public string AsciiFirstAuthor => FirstAuthor?.UnicodeToAscii();
public string AsciiNarrator => Narrator?.UnicodeToAscii();
public string AsciiComment => Comment?.UnicodeToAscii();
public string AsciiLongDescription => LongDescription?.UnicodeToAscii();
public AppleTag AppleTags => GetTag(TagTypes.Apple) as AppleTag; public AppleTag AppleTags => GetTag(TagTypes.Apple) as AppleTag;
private static ReadOnlyByteVector narratorType = new ReadOnlyByteVector(0xa9, (byte)'n', (byte)'r', (byte)'t');
private static ReadOnlyByteVector descriptionType = new ReadOnlyByteVector(0xa9, (byte)'d', (byte)'e', (byte)'s');
private static ReadOnlyByteVector publisherType = new ReadOnlyByteVector(0xa9, (byte)'p', (byte)'u', (byte)'b');
public string AsciiTitleSansUnabridged => TitleSansUnabridged is not null? unicodeToAscii(TitleSansUnabridged) : default;
public string AsciiFirstAuthor => FirstAuthor is not null? unicodeToAscii(FirstAuthor) : default;
public string AsciiNarrator => Narrator is not null ? unicodeToAscii(Narrator) : default;
public string AsciiComment => Comment is not null ? unicodeToAscii(Comment) : default;
public string AsciiLongDescription => LongDescription is not null ? unicodeToAscii(LongDescription) : default;
public string Comment => AppleTags.Comment; public string Comment => AppleTags.Comment;
public string[] Authors => AppleTags.Performers; public string[] Authors => AppleTags.Performers;
public string FirstAuthor => Authors?.Length > 0 ? Authors[0] : default; public string FirstAuthor => Authors?.Length > 0 ? Authors[0] : default;
public string TitleSansUnabridged => AppleTags.Title?.Replace(" (Unabridged)", ""); public string TitleSansUnabridged => AppleTags.Title?.Replace(" (Unabridged)", "");
public string BookCopyright => _copyright is not null && _copyright.Length > 0 ? _copyright[0] : default; public string BookCopyright => _copyright is not null && _copyright.Length > 0 ? _copyright[0] : default;
public string RecordingCopyright => _copyright is not null && _copyright.Length > 1 ? _copyright[1] : default; public string RecordingCopyright => _copyright is not null && _copyright.Length > 1 ? _copyright[1] : default;
public string Narrator private string[] _copyright => AppleTags.Copyright?.Replace("©", string.Empty)?.Replace("(P)", string.Empty)?.Split(';');
public string Narrator => getAppleTagsText(narratorType);
public string LongDescription => getAppleTagsText(descriptionType);
public string ReleaseDate => getAppleTagsText("rldt");
public string Publisher => getAppleTagsText(publisherType);
private string getAppleTagsText(ByteVector byteVector)
{ {
get string[] text = AppleTags.GetText(byteVector);
{
string[] text = AppleTags.GetText(narratorType);
return text.Length == 0 ? default : text[0]; return text.Length == 0 ? default : text[0];
} }
}
public string LongDescription
{
get
{
string[] text = AppleTags.GetText(descriptionType);
return text.Length == 0 ? default : text[0];
}
}
public string ReleaseDate
{
get
{
string[] text = AppleTags.GetText("rldt");
return text.Length == 0 ? default : text[0];
}
}
public string Publisher
{
get
{
string[] text = AppleTags.GetText(publisherType);
return text.Length == 0 ? default : text[0];
}
}
private string[] _copyright => AppleTags.Copyright?.Replace("©", string.Empty)?.Replace("(P)", string.Empty)?.Split(';');
public AaxcTagLibFile(IFileAbstraction abstraction) public AaxcTagLibFile(IFileAbstraction abstraction)
: base(abstraction, ReadStyle.Average) : base(abstraction, ReadStyle.Average)
{ {
@ -88,78 +71,5 @@ namespace AaxDecrypter
{ {
AppleTags.SetData("covr", coverArt, 0); AppleTags.SetData("covr", coverArt, 0);
} }
/// <summary>
/// Attempts to convert unicode characters to an approximately equal ASCII character.
/// </summary>
private string unicodeToAscii(string unicodeStr)
{
//Accents
unicodeStr = Regex.Replace(unicodeStr, "[éèëêð]", "e");
unicodeStr = Regex.Replace(unicodeStr, "[ÉÈËÊ]", "E");
unicodeStr = Regex.Replace(unicodeStr, "[àâä]", "a");
unicodeStr = Regex.Replace(unicodeStr, "[ÀÁÂÃÄÅ]", "A");
unicodeStr = Regex.Replace(unicodeStr, "[àáâãäå]", "a");
unicodeStr = Regex.Replace(unicodeStr, "[ÙÚÛÜ]", "U");
unicodeStr = Regex.Replace(unicodeStr, "[ùúûüµ]", "u");
unicodeStr = Regex.Replace(unicodeStr, "[òóôõöø]", "o");
unicodeStr = Regex.Replace(unicodeStr, "[ÒÓÔÕÖØ]", "O");
unicodeStr = Regex.Replace(unicodeStr, "[ìíîï]", "i");
unicodeStr = Regex.Replace(unicodeStr, "[ÌÍÎÏ]", "I");
unicodeStr = Regex.Replace(unicodeStr, "[š]", "s");
unicodeStr = Regex.Replace(unicodeStr, "[Š]", "S");
unicodeStr = Regex.Replace(unicodeStr, "[ñ]", "n");
unicodeStr = Regex.Replace(unicodeStr, "[Ñ]", "N");
unicodeStr = Regex.Replace(unicodeStr, "[ç]", "c");
unicodeStr = Regex.Replace(unicodeStr, "[Ç]", "C");
unicodeStr = Regex.Replace(unicodeStr, "[ÿ]", "y");
unicodeStr = Regex.Replace(unicodeStr, "[Ÿ]", "Y");
unicodeStr = Regex.Replace(unicodeStr, "[ž]", "z");
unicodeStr = Regex.Replace(unicodeStr, "[Ž]", "Z");
unicodeStr = Regex.Replace(unicodeStr, "[Ð]", "D");
//Ligatures
unicodeStr = Regex.Replace(unicodeStr, "[œ]", "oe");
unicodeStr = Regex.Replace(unicodeStr, "[Œ]", "Oe");
unicodeStr = Regex.Replace(unicodeStr, "[ꜳ]", "aa");
unicodeStr = Regex.Replace(unicodeStr, "[Ꜳ]", "AA");
unicodeStr = Regex.Replace(unicodeStr, "[æ]", "ae");
unicodeStr = Regex.Replace(unicodeStr, "[Æ]", "AE");
unicodeStr = Regex.Replace(unicodeStr, "[ꜵ]", "ao");
unicodeStr = Regex.Replace(unicodeStr, "[Ꜵ]", "AO");
unicodeStr = Regex.Replace(unicodeStr, "[ꜷ]", "au");
unicodeStr = Regex.Replace(unicodeStr, "[Ꜷ]", "AU");
unicodeStr = Regex.Replace(unicodeStr, "[«»ꜹꜻ]", "av");
unicodeStr = Regex.Replace(unicodeStr, "[«»ꜸꜺ]", "AV");
unicodeStr = Regex.Replace(unicodeStr, "[🙰]", "et");
unicodeStr = Regex.Replace(unicodeStr, "[ff]", "ff");
unicodeStr = Regex.Replace(unicodeStr, "[ffi]", "ffi");
unicodeStr = Regex.Replace(unicodeStr, "[ffl]", "ffl");
unicodeStr = Regex.Replace(unicodeStr, "[fi]", "fi");
unicodeStr = Regex.Replace(unicodeStr, "[fl]", "fl");
unicodeStr = Regex.Replace(unicodeStr, "[ƕ]", "hv");
unicodeStr = Regex.Replace(unicodeStr, "[Ƕ]", "Hv");
unicodeStr = Regex.Replace(unicodeStr, "[℔]", "lb");
unicodeStr = Regex.Replace(unicodeStr, "[ꝏ]", "oo");
unicodeStr = Regex.Replace(unicodeStr, "[Ꝏ]", "OO");
unicodeStr = Regex.Replace(unicodeStr, "[st]", "st");
unicodeStr = Regex.Replace(unicodeStr, "[ꜩ]", "tz");
unicodeStr = Regex.Replace(unicodeStr, "[Ꜩ]", "TZ");
unicodeStr = Regex.Replace(unicodeStr, "[ᵫ]", "ue");
unicodeStr = Regex.Replace(unicodeStr, "[ꭣ]", "uo");
//Punctuation
unicodeStr = Regex.Replace(unicodeStr, "[«»\u2018\u2019\u201A\u201B\u2032\u2035]", "\'");
unicodeStr = Regex.Replace(unicodeStr, "[«»\u201C\u201D\u201E\u201F\u2033\u2036]", "\"");
unicodeStr = Regex.Replace(unicodeStr, "[\u2026]", "...");
unicodeStr = Regex.Replace(unicodeStr, "[\u1680]", "-");
//Spaces
unicodeStr = Regex.Replace(unicodeStr, "[«»\u00A0\u2000\u2002\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u200F\u205F\u3000]", " ");
unicodeStr = Regex.Replace(unicodeStr, "[«»\u2001\u2003]", " ");
unicodeStr = Regex.Replace(unicodeStr, "[«»\u180E\u200B\uFEFF]", "");
return unicodeStr;
}
} }
} }

View File

@ -34,9 +34,9 @@ namespace AaxDecrypter
public string FFMpegDownloaderStandardError => downloaderError.ToString(); public string FFMpegDownloaderStandardError => downloaderError.ToString();
private StringBuilder remuxerError = new StringBuilder(); private StringBuilder remuxerError { get; } = new StringBuilder();
private StringBuilder downloaderError = new StringBuilder(); private StringBuilder downloaderError { get; } = new StringBuilder();
private static Regex processedTimeRegex = new Regex("time=(\\d{2}):(\\d{2}):(\\d{2}).\\d{2}.*speed=\\s{0,1}([0-9]*[.]?[0-9]+)(?:e\\+([0-9]+)){0,1}", RegexOptions.IgnoreCase | RegexOptions.Compiled); private static Regex processedTimeRegex { get; } = new Regex("time=(\\d{2}):(\\d{2}):(\\d{2}).\\d{2}.*speed=\\s{0,1}([0-9]*[.]?[0-9]+)(?:e\\+([0-9]+)){0,1}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private Process downloader; private Process downloader;
private Process remuxer; private Process remuxer;
private bool isCanceled = false; private bool isCanceled = false;