Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Haceau-Zoac committed Aug 24, 2020
0 parents commit 679b021
Show file tree
Hide file tree
Showing 68 changed files with 3,153 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 辰落火辉Haceau

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Haceau-Calculator
=================
![Language](https://img.shields.io/badge/Language-C%23-blue.svg?style=flat-square) ![.Net Core](https://img.shields.io/badge/.Net Core-3.1-blue.svg?style=flat-square)

[![Gitee](https://img.shields.io/badge/Gitee-辰落火辉Haceau-red.svg?style=flat-square)](https://gitee.com/haceau/Haceau.Log)
[![Github](https://img.shields.io/badge/Github-HaceauZoac-blue.svg?style=flat-square)](https://github.com/Haceau-Zoac/Haceau.Log)

简介
---
该项目为适用于C#的日志输出类库。当前版本为v1.0.0。开源协议为MIT。保持更新中。

---
Log:日志类。

使用
---
1. 创建实例
* 无参数:Log() : base(Environment.CurrentDirectory + $@"\log\", DateTime.Now.ToString("G").Replace('/', '-').Replace(':', '-') + ".txt")
* 有一个参数:Log(string fileName) : base(Environment.CurrentDirectory + $@"\log\", fileName)
* 有两个参数:Log(string src, string fileName)
2. 打印日志
* Write:Write(string type, string message)
* Debug:Debug(string message)
* Warning:Warning(string message)
* Info:Info(string message)
* Error:Error(string message)
* Fatal:Fatal(string message)


待办清单
------
|完成版本|内容|
|---|---|
|-|长期维护|
Binary file not shown.
Binary file added src/.vs/Haceau.Google.Translate/v16/.suo
Binary file not shown.
Binary file not shown.
Binary file not shown.
41 changes: 41 additions & 0 deletions src/Haceau.Google.Translate.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30406.217
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Haceau.Google.Translate", "Haceau.Google.Translate\Haceau.Google.Translate.csproj", "{E2A5C114-3DEF-49D7-AA3B-2DA0F85300FE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{9FE4BAA5-0693-45EA-86A4-1A4C97FBD840}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E2A5C114-3DEF-49D7-AA3B-2DA0F85300FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E2A5C114-3DEF-49D7-AA3B-2DA0F85300FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E2A5C114-3DEF-49D7-AA3B-2DA0F85300FE}.Debug|x86.ActiveCfg = Debug|x86
{E2A5C114-3DEF-49D7-AA3B-2DA0F85300FE}.Debug|x86.Build.0 = Debug|x86
{E2A5C114-3DEF-49D7-AA3B-2DA0F85300FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E2A5C114-3DEF-49D7-AA3B-2DA0F85300FE}.Release|Any CPU.Build.0 = Release|Any CPU
{E2A5C114-3DEF-49D7-AA3B-2DA0F85300FE}.Release|x86.ActiveCfg = Release|x86
{E2A5C114-3DEF-49D7-AA3B-2DA0F85300FE}.Release|x86.Build.0 = Release|x86
{9FE4BAA5-0693-45EA-86A4-1A4C97FBD840}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9FE4BAA5-0693-45EA-86A4-1A4C97FBD840}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9FE4BAA5-0693-45EA-86A4-1A4C97FBD840}.Debug|x86.ActiveCfg = Debug|Any CPU
{9FE4BAA5-0693-45EA-86A4-1A4C97FBD840}.Debug|x86.Build.0 = Debug|Any CPU
{9FE4BAA5-0693-45EA-86A4-1A4C97FBD840}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9FE4BAA5-0693-45EA-86A4-1A4C97FBD840}.Release|Any CPU.Build.0 = Release|Any CPU
{9FE4BAA5-0693-45EA-86A4-1A4C97FBD840}.Release|x86.ActiveCfg = Release|Any CPU
{9FE4BAA5-0693-45EA-86A4-1A4C97FBD840}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {15F50258-9CBC-4E2A-AB73-CF2C6703AE86}
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions src/Haceau.Google.Translate/Haceau.Google.Translate.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>Preview</LangVersion>
<Platforms>AnyCPU;x86</Platforms>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Jurassic" Version="3.1.0" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions src/Haceau.Google.Translate/Script.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Jurassic;

namespace Haceau.Google.Translate
{
/// <summary>
/// 脚本
/// </summary>
internal class Script
{
/// <summary>
/// 引擎
/// </summary>
ScriptEngine engine = new ScriptEngine();

/// <summary>
/// 表达式求值
/// </summary>
/// <param name="code">表达式</param>
/// <returns>值</returns>
public object Eval(string code) =>
engine.Evaluate(code);
}
}
161 changes: 161 additions & 0 deletions src/Haceau.Google.Translate/Translate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
using System;
using System.Net.Http;
using System.Text.RegularExpressions;

namespace Haceau.Google.Translate
{
/// <summary>
/// 翻译接口
/// </summary>
public interface ITranslation
{
/// <summary>
/// 源语言
/// </summary>
public string SourceLanguage
{
get;
set;
}

/// <summary>
/// 目标语言
/// </summary>
public string TargetLanguage
{
get;
set;
}

/// <summary>
/// 翻译
/// </summary>
/// <param name="message">翻译文本</param>
/// <returns>翻译结果</returns>
public string Translate(string message);
}


public class Translation : ITranslation
{
public HttpClient Client
{
get;
set;
}

/// <summary>
/// 源语言
/// </summary>
public string SourceLanguage
{
get;
set;
} = "zh-cn";

/// <summary>
/// 目标语言
/// </summary>
public string TargetLanguage
{
get;
set;
} = "en";

public string Tkk
{
get
{
Web web = new Web("https://translate.google.cn/", Client);
return Regex.Match(web.SendGetRequest(), @"(?<=TKK=')(.*?)(?=')").Value;
}
}

/// <summary>
/// 获取请求URL TK值的JAVASCRIPT函数代码
/// </summary>
private readonly string GET_TK_JAVASCRIPT_CODE = @"function token(a) {
var k = """";
var b = 406644;
var b1 = 3293161072;
var jd = ""."";
var sb = ""+-a^+6"";
var Zb = ""+-3^+b+-f"";
for (var e = [], f = 0, g = 0; g < a.length; g++) {
var m = a.charCodeAt(g);
128 > m ? e[f++] = m : (2048 > m ? e[f++] = m >> 6 | 192 : (55296 == (m & 64512) && g + 1 < a.length && 56320 == (a.charCodeAt(g + 1) & 64512) ? (m = 65536 + ((m & 1023) << 10) + (a.charCodeAt(++g) & 1023),
e[f++] = m >> 18 | 240,
e[f++] = m >> 12 & 63 | 128) : e[f++] = m >> 12 | 224,
e[f++] = m >> 6 & 63 | 128),
e[f++] = m & 63 | 128)
}
a = b;
for (f = 0; f < e.length; f++)
a += e[f],
a = RL(a, sb);
a = RL(a, Zb);
a ^= b1 || 0;
0 > a && (a = (a & 2147483647) + 2147483648);
a %= 1E6;
return a.toString() + jd + (a ^ b)
};
function RL(a, b) {
var t = ""a"";
var Yb = ""+"";
for (var c = 0; c < b.length - 2; c += 3) {
var d = b.charAt(c + 2),
d = d >= t ? d.charCodeAt(0) - 87 : Number(d),
d = b.charAt(c + 1) == Yb ? a >>> d : a << d;
a = b.charAt(c) == Yb ? a + d & 4294967295 : a ^ d
}
return a
}";

/// <summary>
/// 默认初始化
/// </summary>
public Translation(HttpClient client) =>
Client = client;

/// <summary>
/// 使用语言进行初始化
/// </summary>
/// <param name="sourceLanguage">源语言</param>
/// <param name="targetLanguage">目标语言</param>
public Translation(string sourceLanguage, string targetLanguage, HttpClient client)
{
SourceLanguage = sourceLanguage;
TargetLanguage = targetLanguage;
Client = client;
}

/// <summary>
/// 翻译
/// </summary>
/// <param name="message">翻译文本</param>
/// <returns>翻译结果</returns>
public string Translate(string message)
{
if (SourceLanguage == null || TargetLanguage == null)
throw new Exception("属性SourceLanguage或TargetLanguage为空。");
Web web = new Web(
$"https://translate.google.cn/translate_a/single?client=webapp&sl={SourceLanguage}&tl={TargetLanguage}&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=sos&dt=ss&dt=t&dt=gt&source=bh&ssel=0&tsel=0&xid=45662847&kc=1&tk={GetTk(message)}&q={message}",
Client);
string result = web.SendGetRequest();
return result.Substring(4, (result.IndexOf("\"", 4 + 1) - 4));
}

/// <summary>
/// 获取Tk值
/// </summary>
/// <param name="message">信息</param>
/// <returns>Tk值</returns>
public object GetTk(object message)
{
Script script = new Script();
return script.Eval(GET_TK_JAVASCRIPT_CODE + $"token('{message}')");
}
}
}
62 changes: 62 additions & 0 deletions src/Haceau.Google.Translate/Web.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;

namespace Haceau.Google.Translate
{
/// <summary>
/// 网络
/// </summary>
internal class Web
{
/// <summary>
/// 网址
/// </summary>
private string url;

/// <summary>
/// 网址
/// </summary>
public string Url
{
get => url;
set
{
if (value.Substring(0, 7) != "http://" && value.Substring(0, 8) != "https://")
url = "https://" + value;
else
url = value;
}
}

public HttpClient Client
{
get;
set;
}

/// <summary>
/// 使用网址进行初始化
/// </summary>
/// <param name="url">网址</param>
public Web(string url, HttpClient client)
{
Url = url;
Client = client;
}

/// <summary>
/// 发送GET请求
/// </summary>
/// <returns>请求结果</returns>
public string SendGetRequest()
{
if (Url == null)
throw new Exception("属性Url不能为空。");
HttpResponseMessage response = Client.GetAsync(Url).Result;
return response.Content.ReadAsStringAsync().Result;
}
}
}
Loading

0 comments on commit 679b021

Please # to comment.