Skip to content

Commit 22a3db0

Browse files
committed
Convert Newline from LF to CRLF what Downloaded Text.
1 parent 3abf68a commit 22a3db0

File tree

7 files changed

+118
-38
lines changed

7 files changed

+118
-38
lines changed

NCodeParser/App.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
55
xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
StartupUri="View/MainWindow.xaml"
7-
d1p1:Ignorable="d" >
7+
d1p1:Ignorable="d"
8+
Startup="Application_Startup">
89
</Application>

NCodeParser/App.xaml.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Configuration;
44
using System.Data;
55
using System.Linq;
6+
using System.Net;
67
using System.Threading.Tasks;
78
using System.Windows;
89

@@ -12,6 +13,11 @@ namespace NCodeParser
1213
/// App.xaml에 대한 상호 작용 논리
1314
/// </summary>
1415
public partial class App : Application
15-
{
16-
}
16+
{
17+
private void Application_Startup(object sender, StartupEventArgs e)
18+
{
19+
ServicePointManager.Expect100Continue = true;
20+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
21+
}
22+
}
1723
}

NCodeParser/Config.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static void Init()
6666

6767
NovelPath = INIManager.GetNovelPath();
6868
NovelList = INIManager.GetNovels();
69-
TranslatorType = TranslatorType.GSheet; // TODO
69+
TranslatorType = TranslatorType.None; // TODO
7070
TranslateWithSource = true; // TODO
7171
}
7272

NCodeParser/IO/NovelDownloader.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ private async Task DownloadNovel(Novel novel, Episode episode, bool createFile,
346346
document.LoadHtml(input);
347347

348348
var result = document.GetElementbyId("novel_color").InnerText;
349+
if (!result.Contains(Environment.NewLine))
350+
{
351+
result = result.Replace("\n", Environment.NewLine);
352+
}
353+
349354
result = result.Replace("&nbsp;", "");
350355
result = result.Replace("<ruby>", "");
351356
result = result.Replace("</ruby>", "");
@@ -379,6 +384,11 @@ private async Task DownloadNovel(Novel novel, Episode episode, bool createFile,
379384
document.LoadHtml(input);
380385

381386
var result = document.GetElementbyId("contentMain-inner").InnerText;
387+
if (!result.Contains(Environment.NewLine))
388+
{
389+
result = result.Replace("\n", Environment.NewLine);
390+
}
391+
382392
result = result.Replace("&nbsp;", "");
383393
result = result.Replace("<em class=\"emphasisDots\">", "");
384394
result = result.Replace("</em>", "");
@@ -392,7 +402,7 @@ private async Task DownloadNovel(Novel novel, Episode episode, bool createFile,
392402
result = result.Replace("</rb>", "");
393403
result = result.Replace("<rt>", "");
394404
result = result.Replace("</rt>", "");
395-
result = result.Replace("<br />", "\r\n");
405+
result = result.Replace("<br />", Environment.NewLine);
396406

397407
episode.SourceText = result;
398408
}

NCodeParser/IO/UpdateHelper.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Text;
33
using System.Threading.Tasks;
4-
4+
using System.Windows;
55
using Newtonsoft.Json.Linq;
66

77
namespace NCodeParser.IO
@@ -10,27 +10,34 @@ public static class UpdateHelper
1010
{
1111
private static readonly string LatestReleaseURL = "https://api.github.com/repos/imnotcode/NCodeParser/releases/latest";
1212

13-
public static async Task<bool> CheckUpdate()
13+
public static async Task<string> GetLatestVersion()
1414
{
15-
using (var client = new CookieAwareWebClient())
15+
try
1616
{
17-
client.Headers.Add("User-Agent: Other");
18-
client.UseDefaultCredentials = true;
19-
20-
string URL = LatestReleaseURL;
17+
using (var client = new CookieAwareWebClient())
18+
{
19+
client.Headers.Add("User-Agent: Other");
20+
client.UseDefaultCredentials = true;
2121

22-
var bytes = await client.DownloadDataTaskAsync(new Uri(URL)).ConfigureAwait(false);
23-
var downloadedText = Encoding.UTF8.GetString(bytes);
22+
string URL = LatestReleaseURL;
2423

25-
var jObject = JObject.Parse(downloadedText);
24+
var bytes = await client.DownloadDataTaskAsync(new Uri(URL)).ConfigureAwait(false);
25+
var downloadedText = Encoding.UTF8.GetString(bytes);
2626

27-
string tagVersion = jObject["tag_name"].ToString();
28-
tagVersion = tagVersion.Replace("v", "");
27+
var jObject = JObject.Parse(downloadedText);
2928

30-
// TODO
29+
string tagVersion = jObject["tag_name"].ToString();
30+
tagVersion = tagVersion.Replace("v", "");
3131

32-
return false;
32+
return tagVersion;
33+
}
3334
}
35+
catch (Exception e)
36+
{
37+
MessageBox.Show(e.ToString());
38+
}
39+
40+
return "";
3441
}
3542
}
3643
}

NCodeParser/Translate/GSheetsTranslator.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
using System.Text;
55
using System.Threading;
66
using System.Threading.Tasks;
7+
using Google;
78
using Google.Apis.Auth.OAuth2;
9+
using Google.Apis.Requests;
810
using Google.Apis.Services;
911
using Google.Apis.Sheets.v4;
1012
using Google.Apis.Sheets.v4.Data;
@@ -89,14 +91,22 @@ public override async Task<string> Translate(string input)
8991

9092
if (sourceList.Count != splitTexts.Length)
9193
{
92-
94+
for (int i = 0; i < sourceList.Count; i++)
95+
{
96+
97+
}
9398
}
9499

95100
builder.Clear();
96101
for (int i = 0, j = 0; i < dividedTexts.Length; i++)
97102
{
98103
if (!string.IsNullOrWhiteSpace(dividedTexts[i]))
99104
{
105+
if (splitTexts.Length <= j)
106+
{
107+
108+
}
109+
100110
var translatedText = splitTexts[j++].Trim();
101111

102112
if (Config.TranslateWithSource && dividedTexts[i] != translatedText)
@@ -136,11 +146,15 @@ protected override async Task<string> TranslateOneLine(string input)
136146
string from = "A" + rowID;
137147
string to = "E" + rowID;
138148

139-
IList<IList<object>> list = new List<IList<object>>();
140-
list.Add(new List<object>() { input, "=GOOGLETRANSLATE(" + from + ", \"ja\", \"ko\")" });
149+
IList<IList<object>> list = new List<IList<object>>
150+
{
151+
new List<object>() { input, "=GOOGLETRANSLATE(" + from + ", \"ja\", \"ko\")" }
152+
};
141153

142-
var range = new ValueRange();
143-
range.Values = list;
154+
var range = new ValueRange
155+
{
156+
Values = list
157+
};
144158

145159
var request = Service.Spreadsheets.Values.Update(range, SheetID, $"{from}:{to}");
146160
request.ValueInputOption = ValueInputOptionEnum.USERENTERED;
@@ -164,9 +178,16 @@ protected override async Task<string> TranslateOneLine(string input)
164178
return result;
165179
}
166180
}
181+
catch (GoogleApiException e)
182+
{
183+
if (e.ToString().Contains("user per 100 seconds"))
184+
{
185+
// TODO 100초당 500번
186+
}
187+
}
167188
catch
168189
{
169-
190+
170191
}
171192
finally
172193
{

NCodeParser/ViewModel/MainViewModel.cs

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ public string TitleText
204204
{
205205
get
206206
{
207+
if (NovelList == null)
208+
{
209+
return "NCodeParser";
210+
}
211+
207212
return $"NCodeParser ({UpdateCount}/{NovelList.Count})";
208213
}
209214
}
@@ -223,7 +228,7 @@ public MainViewModel()
223228
InitControls();
224229
}
225230

226-
private async Task InitInstance()
231+
private void InitInstance()
227232
{
228233
LoadedCommand = new RelayCommand(OnLoaded);
229234
AddCommand1 = new RelayCommand(OnAdd1, CanAdd1);
@@ -265,15 +270,27 @@ private async void InitControls()
265270

266271
private async void OnLoaded()
267272
{
268-
bool canUpdate = await UpdateHelper.CheckUpdate().ConfigureAwait(false);
269-
if (canUpdate)
273+
string version = await UpdateHelper.GetLatestVersion().ConfigureAwait(false);
274+
if (string.IsNullOrWhiteSpace(version))
270275
{
271-
// TODO
276+
return;
277+
}
278+
279+
string currentVersion = Config.ApplicationVersion;
280+
if (false) // TODO Compare Version
281+
{
282+
var result = MessageBox.Show("TODO");
272283
}
273284
}
274285

275286
private async Task SetTranslator()
276287
{
288+
if (Downloader == null)
289+
{
290+
Debug.Assert(false);
291+
return;
292+
}
293+
277294
if (Config.TranslatorType == TranslatorType.GSheet)
278295
{
279296
var translator = new GSheetsTranslator();
@@ -315,6 +332,12 @@ private async Task SetTranslator()
315332

316333
private async Task SelectNovel(Novel novel)
317334
{
335+
if (Downloader == null)
336+
{
337+
Debug.Assert(false);
338+
return;
339+
}
340+
318341
if (novel == null)
319342
{
320343
return;
@@ -332,7 +355,7 @@ private async Task SelectNovel(Novel novel)
332355
novel.Episodes.Clear();
333356
novel.Episodes.AddAll(Episodes);
334357

335-
await App.Current?.Dispatcher.InvokeAsync(() =>
358+
await App.Current?.Dispatcher?.InvokeAsync(() =>
336359
{
337360
novel.UIEpisodes.Clear();
338361
novel.UIEpisodes.AddAll(novel.Episodes);
@@ -369,6 +392,12 @@ private void CheckAllUpdate()
369392

370393
private void CheckUpdate(Novel novel)
371394
{
395+
if (Downloader == null)
396+
{
397+
Debug.Assert(false);
398+
return;
399+
}
400+
372401
if (novel == null)
373402
{
374403
return;
@@ -393,7 +422,7 @@ private void CheckUpdate(Novel novel)
393422
novel.Episodes.Clear();
394423
novel.Episodes.AddAll(Episodes);
395424

396-
App.Current?.Dispatcher.InvokeAsync(() =>
425+
App.Current?.Dispatcher?.InvokeAsync(() =>
397426
{
398427
novel.UIEpisodes.Clear();
399428
novel.UIEpisodes.AddAll(novel.Episodes);
@@ -573,31 +602,37 @@ private bool CanAdd3()
573602

574603
private void OnSelectAll()
575604
{
576-
if (SelectedNovel == null)
605+
var novel = SelectedNovel;
606+
if (novel == null)
577607
{
578608
MessageBox.Show("선택된 소설이 없습니다.");
579609
return;
580610
}
581611

582-
SelectedNovel.EpisodeStartIndex = 0;
583-
SelectedNovel.EpisodeEndIndex = SelectedNovel.Episodes.Count - 1;
612+
if (novel.Episodes.Count == 0)
613+
{
614+
return;
615+
}
616+
617+
novel.EpisodeStartIndex = 0;
618+
novel.EpisodeEndIndex = novel.Episodes.Count - 1;
584619
}
585620

586621
private async void OnDownload()
587622
{
588-
if (SelectedNovel == null)
623+
var novel = SelectedNovel;
624+
if (novel == null)
589625
{
590626
MessageBox.Show("선택된 소설이 없습니다.");
591627
return;
592628
}
593629

594-
if (SelectedNovel.Episodes.Count == 0)
630+
if (novel.Episodes.Count == 0)
595631
{
596632
MessageBox.Show("다운로드할 수 없습니다.");
597633
return;
598634
}
599635

600-
var novel = SelectedNovel;
601636
int startIndex = novel.EpisodeStartIndex;
602637
int endIndex = novel.EpisodeEndIndex;
603638

@@ -621,7 +656,7 @@ private void OnExplorer()
621656

622657
private void OnExit()
623658
{
624-
App.Current.MainWindow.Close();
659+
App.Current?.MainWindow?.Close();
625660
}
626661

627662
private void OnShowLicense()

0 commit comments

Comments
 (0)