Work on error handling

This commit is contained in:
niksedk 2023-10-26 09:07:54 +02:00
parent a7b4aba4b0
commit 2e5a098cba
3 changed files with 89 additions and 7 deletions

View File

@ -50,10 +50,13 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
var content = new StringContent(input, Encoding.UTF8);
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var result = _httpClient.PostAsync("translate", content).Result;
result.EnsureSuccessStatusCode();
var bytes = await result.Content.ReadAsByteArrayAsync();
var json = Encoding.UTF8.GetString(bytes).Trim();
if (!result.IsSuccessStatusCode)
{
SeLogger.Error("LibreTranslate failed calling API: Status code=" + result.StatusCode + Environment.NewLine + json);
}
result.EnsureSuccessStatusCode();
var parser = new SeJsonParser();
var resultText = parser.GetFirstObject(json, "translatedText");
if (resultText == null)

View File

@ -0,0 +1,57 @@
namespace Nikse.SubtitleEdit.Core.Common.TextLengthCalculator
{
public class CalcIgnoreThaiCompositeCharacters : ICalcLength
{
/// <summary>
/// Calculate all text excluding Thai composite characters (tags are not counted).
/// Netflix rule: 35 characters per line (excluding all composite characters, i.e. tone marks, top and bottom vowels are not counted.
/// See https://partnerhelp.netflixstudios.com/hc/en-us/articles/220448308-Thai-Timed-Text-Style-Guide
/// </summary>
public decimal CountCharacters(string text, bool forCps)
{
const char zeroWidthSpace = '\u200B';
const char zeroWidthNoBreakSpace = '\uFEFF';
const string thaiCompositeCharacters = "ํู่๊ี้"; //TODO: not correct...
var length = 0;
var ssaTagOn = false;
var htmlTagOn = false;
var max = text.Length;
for (var i = 0; i < max; i++)
{
var ch = text[i];
if (ssaTagOn)
{
if (ch == '}')
{
ssaTagOn = false;
}
}
else if (htmlTagOn)
{
if (ch == '>')
{
htmlTagOn = false;
}
}
else if (ch == '{' && i < text.Length - 1 && text[i + 1] == '\\')
{
ssaTagOn = true;
}
else if (ch == '<' && i < text.Length - 1 && (text[i + 1] == '/' || char.IsLetter(text[i + 1])) &&
text.IndexOf('>', i) > 0 && TextLengthHelper.IsKnownHtmlTag(text, i))
{
htmlTagOn = true;
}
else if (!char.IsControl(ch) &&
ch != zeroWidthSpace &&
ch != zeroWidthNoBreakSpace &&
!thaiCompositeCharacters.Contains(ch))
{
length++;
}
}
return length;
}
}
}

View File

@ -120,11 +120,33 @@ namespace Nikse.SubtitleEdit.Forms.BeautifyTimeCodes
Configuration.Settings.BeautifyTimeCodes.Profile.ConnectedSubtitlesRightRedZone = redZone;
Configuration.Settings.BeautifyTimeCodes.Profile.ConnectedSubtitlesRightGreenZone = greenZone;
SeLogger.Error("BeautifyTimeCodesProfileSimple.buttonOK_Click: Gap is " + gap);
SeLogger.Error("BeautifyTimeCodesProfileSimple.buttonOK_Click: FrameRate is " + _frameRate);
var treadConnectedMs = Math.Round(SubtitleFormat.FramesToMilliseconds(gap, _frameRate) * 1.5);
SeLogger.Error("BeautifyTimeCodesProfileSimple.buttonOK_Click: treadConnectedMs is " + treadConnectedMs);
Configuration.Settings.BeautifyTimeCodes.Profile.ConnectedSubtitlesTreatConnected = Convert.ToInt32(treadConnectedMs);
double treadConnectedMs = 0;
try
{
treadConnectedMs = Math.Round(SubtitleFormat.FramesToMilliseconds(gap, _frameRate) * 1.5);
}
catch (Exception exception)
{
SeLogger.Error(exception, "Error when executing: treadConnectedMs = Math.Round(SubtitleFormat.FramesToMilliseconds(gap, _frameRate) * 1.5); );");
SeLogger.Error("BeautifyTimeCodesProfileSimple.buttonOK_Click: Gap is " + gap);
SeLogger.Error("BeautifyTimeCodesProfileSimple.buttonOK_Click: FrameRate is " + _frameRate);
SeLogger.Error("BeautifyTimeCodesProfileSimple.buttonOK_Click: treadConnectedMs is " + treadConnectedMs);
throw;
}
try
{
Configuration.Settings.BeautifyTimeCodes.Profile.ConnectedSubtitlesTreatConnected = Convert.ToInt32(treadConnectedMs);
}
catch (Exception exception)
{
SeLogger.Error(exception, "Error when executing: Configuration.Settings.BeautifyTimeCodes.Profile.ConnectedSubtitlesTreatConnected = Convert.ToInt32(treadConnectedMs);");
SeLogger.Error("BeautifyTimeCodesProfileSimple.buttonOK_Click: Gap is " + gap);
SeLogger.Error("BeautifyTimeCodesProfileSimple.buttonOK_Click: FrameRate is " + _frameRate);
SeLogger.Error("BeautifyTimeCodesProfileSimple.buttonOK_Click: treadConnectedMs is " + treadConnectedMs);
throw;
}
Configuration.Settings.BeautifyTimeCodes.Profile.ChainingGeneralUseZones = false;
Configuration.Settings.BeautifyTimeCodes.Profile.ChainingGeneralMaxGap = Convert.ToInt32(numericUpDownChainingGap.Value);