Skip to content

Commit

Permalink
Translate Novel's Description (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnotcode committed Nov 29, 2019
1 parent fcc134c commit 7afebb9
Show file tree
Hide file tree
Showing 13 changed files with 439 additions and 45 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
NCodeParser/bin
NCodeParser/obj
packages
TestProject/bin
TestProject/obj
6 changes: 6 additions & 0 deletions NCodeParser.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.28803.156
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NCodeParser", "NCodeParser\NCodeParser.csproj", "{1C1BC9C4-6184-4201-A4D1-0C37D789C46B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestProject", "TestProject\TestProject.csproj", "{103A8868-118F-41D4-8B16-AF18F637B6BE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{1C1BC9C4-6184-4201-A4D1-0C37D789C46B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1C1BC9C4-6184-4201-A4D1-0C37D789C46B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1C1BC9C4-6184-4201-A4D1-0C37D789C46B}.Release|Any CPU.Build.0 = Release|Any CPU
{103A8868-118F-41D4-8B16-AF18F637B6BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{103A8868-118F-41D4-8B16-AF18F637B6BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{103A8868-118F-41D4-8B16-AF18F637B6BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{103A8868-118F-41D4-8B16-AF18F637B6BE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
14 changes: 14 additions & 0 deletions NCodeParser/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace NCodeParser
{
public static class Config
{
public static string ApplicationName = Assembly.GetEntryAssembly().GetName().Name;
}
}
13 changes: 13 additions & 0 deletions NCodeParser/Interface/ITranslator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NCodeParser.Interface
{
public interface ITranslator
{
Task<string> Translate(string input);
}
}
24 changes: 24 additions & 0 deletions NCodeParser/NCodeParser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,33 @@
<Reference Include="GalaSoft.MvvmLight.Platform, Version=5.4.1.0, Culture=neutral, PublicKeyToken=5f873c45e98af8a1, processorArchitecture=MSIL">
<HintPath>..\packages\MvvmLightLibs.5.4.1.1\lib\net45\GalaSoft.MvvmLight.Platform.dll</HintPath>
</Reference>
<Reference Include="Google.Apis, Version=1.42.0.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab, processorArchitecture=MSIL">
<HintPath>..\packages\Google.Apis.1.42.0\lib\net45\Google.Apis.dll</HintPath>
</Reference>
<Reference Include="Google.Apis.Auth, Version=1.42.0.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab, processorArchitecture=MSIL">
<HintPath>..\packages\Google.Apis.Auth.1.42.0\lib\net45\Google.Apis.Auth.dll</HintPath>
</Reference>
<Reference Include="Google.Apis.Auth.PlatformServices, Version=1.42.0.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab, processorArchitecture=MSIL">
<HintPath>..\packages\Google.Apis.Auth.1.42.0\lib\net45\Google.Apis.Auth.PlatformServices.dll</HintPath>
</Reference>
<Reference Include="Google.Apis.Core, Version=1.42.0.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab, processorArchitecture=MSIL">
<HintPath>..\packages\Google.Apis.Core.1.42.0\lib\net45\Google.Apis.Core.dll</HintPath>
</Reference>
<Reference Include="Google.Apis.PlatformServices, Version=1.42.0.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab, processorArchitecture=MSIL">
<HintPath>..\packages\Google.Apis.1.42.0\lib\net45\Google.Apis.PlatformServices.dll</HintPath>
</Reference>
<Reference Include="Google.Apis.Sheets.v4, Version=1.42.0.1786, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab, processorArchitecture=MSIL">
<HintPath>..\packages\Google.Apis.Sheets.v4.1.42.0.1786\lib\net45\Google.Apis.Sheets.v4.dll</HintPath>
</Reference>
<Reference Include="HtmlAgilityPack, Version=1.11.17.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<HintPath>..\packages\HtmlAgilityPack.1.11.17\lib\Net45\HtmlAgilityPack.dll</HintPath>
</Reference>
<Reference Include="INIFileParser, Version=2.5.2.0, Culture=neutral, PublicKeyToken=79af7b307b65cf3c, processorArchitecture=MSIL">
<HintPath>..\packages\ini-parser.2.5.2\lib\net20\INIFileParser.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
Expand All @@ -76,9 +97,11 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Config.cs" />
<Compile Include="Control\Converter\VisibilityConverter.cs" />
<Compile Include="Enum.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="Interface\ITranslator.cs" />
<Compile Include="IO\CookieAwareWebClient.cs">
<SubType>Component</SubType>
</Compile>
Expand All @@ -87,6 +110,7 @@
<Compile Include="Model\Episode.cs" />
<Compile Include="Model\Novel.cs" />
<Compile Include="Singleton.cs" />
<Compile Include="Translate\GSheetsTranslator.cs" />
<Compile Include="ViewModel\LicenseViewModel.cs" />
<Compile Include="ViewModel\MainViewModel.cs" />
<Compile Include="ViewModel\SettingViewModel.cs" />
Expand Down
189 changes: 189 additions & 0 deletions NCodeParser/Translate/GSheetsTranslator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Util.Store;
using NCodeParser.Interface;
using static Google.Apis.Sheets.v4.SpreadsheetsResource.ValuesResource.UpdateRequest;

namespace NCodeParser.Translate
{
public class GSheetsTranslator : ITranslator
{
private readonly string[] Scopes = { SheetsService.Scope.Spreadsheets };
private readonly string SheetID = "1JZwDTknNzCIpFZNoDLqaWEZcm5oJYUiyD5kPH9T7VAI";

private readonly bool[] RowUsages = new bool[10];
private readonly SemaphoreSlim MainSemaphore = new SemaphoreSlim(10, 10);
private readonly SemaphoreSlim IDSemaphore = new SemaphoreSlim(1, 1);

public async Task<string> Translate(string input)
{
int rowID = -1;

try
{
await MainSemaphore.WaitAsync();

UserCredential credential;

if (!File.Exists("credentials.json"))
{
return input;
}

using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true));
}

var sheetService = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Config.ApplicationName,
});

rowID = await GetRowID();
if (rowID == -1)
{
return input;
}

string from = "A" + rowID;
string to = "E" + rowID;

IList<IList<object>> list = new List<IList<object>>();
list.Add(new List<object>() { input, "=GOOGLETRANSLATE(" + from + ", \"ja\", \"ko\")" });

var range = new ValueRange();
range.Values = list;

var request = sheetService.Spreadsheets.Values.Update(range, SheetID, $"{from}:{to}");
request.ValueInputOption = ValueInputOptionEnum.USERENTERED;

var response = await request.ExecuteAsync();

var result = await Load(rowID);
if (string.IsNullOrWhiteSpace(result))
{
return input;
}
else
{
return result;
}
}
catch
{

}
finally
{
if (rowID != -1)
{
ReleaseRowID(rowID);
}

MainSemaphore.Release();
}

return input;
}

private async Task<int> GetRowID()
{
try
{
await IDSemaphore.WaitAsync();

while (true)
{
for (int i = 0; i < RowUsages.Length; i++)
{
if (!RowUsages[i])
{
Console.WriteLine(i + 1);

RowUsages[i] = true;

return i + 1;
}
}

await Task.Delay(500);
}
}
catch
{

}
finally
{
IDSemaphore.Release();
}

return -1;
}

private void ReleaseRowID(int rowID)
{
RowUsages[rowID - 1] = false;
}

private async Task<string> Load(int rowID)
{
try
{
UserCredential credential;

using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true));
}

var sheetService = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Config.ApplicationName,
});

string from = "A" + rowID;
string to = "E" + rowID;

var request = sheetService.Spreadsheets.Values.BatchGet(SheetID);
request.Ranges = $"{from}:{to}";

var response = await request.ExecuteAsync();

return response.ValueRanges[0].Values[0][1].ToString();
}
catch
{

}

return "";
}
}
}
15 changes: 9 additions & 6 deletions NCodeParser/View/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
<MenuItem Header="Exit" />
</MenuItem>

<MenuItem Header="Settings" Command="{Binding SettingCommand}" />
<MenuItem Header="Tools">
<MenuItem Header="Options..." Command="{Binding SettingCommand}" />
</MenuItem>

<MenuItem Header="Help">
<MenuItem Header="View License" />
Expand All @@ -77,6 +79,7 @@
SelectedItem="{Binding SelectedNovel}"
ColumnHeaderStyle="{StaticResource ColumnHeaderStyle}"
CellStyle="{StaticResource DataGridCellStyle}"
SelectionMode="Single"
RowHeight="26">
<DataGrid.Columns>
<DataGridTextColumn Width="80" Header="Code" IsReadOnly="True" Binding="{Binding Code, UpdateSourceTrigger=PropertyChanged}" />
Expand Down Expand Up @@ -151,7 +154,7 @@
<ComboBox VerticalContentAlignment="Center"
Margin="0,8,0,0"
Height="26"
IsEnabled="{Binding Downloadable}"
IsEnabled="{Binding SelectedNovel.Downloadable}"
ItemsSource="{Binding SelectedNovel.Episodes}"
SelectedIndex="{Binding SelectedNovel.EpisodeStartIndex}" />

Expand All @@ -161,7 +164,7 @@

<ComboBox VerticalContentAlignment="Center"
Height="26"
IsEnabled="{Binding Downloadable}"
IsEnabled="{Binding SelectedNovel.Downloadable}"
ItemsSource="{Binding SelectedNovel.Episodes}"
SelectedIndex="{Binding SelectedNovel.EpisodeEndIndex}" />

Expand All @@ -179,9 +182,9 @@
<RowDefinition />
</Grid.RowDefinitions>

<CheckBox Grid.Row="0" Margin="0,4,0,4" Content="Intro 받기" IsEnabled="{Binding Downloadable}" />
<CheckBox Grid.Row="1" Margin="0,4,0,4" Content="작가 코멘트" IsEnabled="{Binding Downloadable}" />
<CheckBox Grid.Row="2" Margin="0,4,0,4" Content="단일파일 기록" IsEnabled="{Binding Downloadable}" IsChecked="{Binding SelectedNovel.Merging}" />
<CheckBox Grid.Row="0" Margin="0,4,0,4" Content="Intro 받기" IsEnabled="{Binding SelectedNovel.Downloadable}" />
<CheckBox Grid.Row="1" Margin="0,4,0,4" Content="작가 코멘트" IsEnabled="{Binding SelectedNovel.Downloadable}" />
<CheckBox Grid.Row="2" Margin="0,4,0,4" Content="단일파일 기록" IsEnabled="{Binding SelectedNovel.Downloadable}" IsChecked="{Binding SelectedNovel.Merging}" />
</Grid>
</Grid>
</StackPanel>
Expand Down
Loading

0 comments on commit 7afebb9

Please sign in to comment.