|
@@ -3,18 +3,21 @@ using Newtonsoft.Json.Linq;
|
|
|
using PDF_Master.Properties;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.Diagnostics;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Net;
|
|
|
+using System.Net.Http;
|
|
|
using System.Text;
|
|
|
using System.Text.RegularExpressions;
|
|
|
using System.Threading.Tasks;
|
|
|
+using static Dropbox.Api.Files.SearchMatchType;
|
|
|
|
|
|
namespace PDF_Master.Helper
|
|
|
{
|
|
|
public static class ChatGTPAIHelper
|
|
|
{
|
|
|
- private static string host = "http://api-us-east-1.compdf.com:8082";
|
|
|
+ private static string host = "https://ai.compdf.com";
|
|
|
|
|
|
private static string translate = "http://101.132.103.13:8030";
|
|
|
|
|
@@ -64,19 +67,97 @@ namespace PDF_Master.Helper
|
|
|
/// <param name="content"></param>
|
|
|
/// <param name="language">1=中文 2=英文 3=法语 4=汉语</param>
|
|
|
/// <returns></returns>
|
|
|
- public async static Task<string> fileKeyTranslate(string content, int language)
|
|
|
+ public async static Task<string> fileKeyTranslate(string content,string filename)
|
|
|
{
|
|
|
- System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection();
|
|
|
- //需要翻译的内容
|
|
|
- namevalue["file"] = content;
|
|
|
- //1=中文 2=英文 3=法语 4=汉语
|
|
|
- namevalue["projectId"] = "2";
|
|
|
- namevalue["version"] = "1.0.1";
|
|
|
+
|
|
|
+ HttpWebResponse response = null;
|
|
|
+ ServicePointManager.DefaultConnectionLimit = 200;
|
|
|
+ HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_fileKeytranslate);
|
|
|
+ request.Method = "Post";
|
|
|
+
|
|
|
+ var formData = new MultipartFormDataContent();
|
|
|
+ byte[] data = File.ReadAllBytes(@content);
|
|
|
+ MemoryStream stream = new MemoryStream(data);
|
|
|
+ formData.Add(new ByteArrayContent(data), "file", filename);
|
|
|
+ formData.Add(new StringContent("2"), "projectId");
|
|
|
+ formData.Add(new StringContent("1.0.1"), "version");
|
|
|
if (!string.IsNullOrEmpty(Settings.Default.UserDate.id))
|
|
|
{
|
|
|
- namevalue["userId"] = Settings.Default.UserDate.id;
|
|
|
+ formData.Add(new StringContent(Settings.Default.UserDate.id), "userId");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ formData.Add(new StringContent("1"), "userId");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置请求体格式
|
|
|
+ request.ContentType = formData.Headers.ContentType.ToString();
|
|
|
+ request.ContentLength = formData.Headers.ContentLength.Value;
|
|
|
+
|
|
|
+ // 将FormData写入请求体
|
|
|
+ using (var requestStream = request.GetRequestStream())
|
|
|
+ {
|
|
|
+ await formData.CopyToAsync(requestStream);
|
|
|
+ }
|
|
|
+ try
|
|
|
+ {
|
|
|
+
|
|
|
+ // 获取HTTP响应
|
|
|
+ HttpWebResponse response2 = (HttpWebResponse)request.GetResponse();
|
|
|
+ using (StreamReader reader = new StreamReader(response2.GetResponseStream()))
|
|
|
+ {
|
|
|
+ string responseData = reader.ReadToEnd();
|
|
|
+ Console.WriteLine(responseData);
|
|
|
+ reader.Close();
|
|
|
+ JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
|
|
|
+ if (response2 != null)
|
|
|
+ {
|
|
|
+ response2.Close();
|
|
|
+ }
|
|
|
+ if (request != null)
|
|
|
+ {
|
|
|
+ request.Abort();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (jobject["code"].ToObject<string>().ToLower() == "200")
|
|
|
+ {
|
|
|
+ translate = jobject["data"]["fileKey"].ToObject<string>().ToLower();
|
|
|
+ }
|
|
|
+ //return jobject["code"].ToObject<string>().ToLower();
|
|
|
+
|
|
|
+ return translate;
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ catch (WebException ex)
|
|
|
+ {
|
|
|
+ Console.WriteLine("HTTP异常:" + ex.Message);
|
|
|
+ return "";
|
|
|
}
|
|
|
- return await PostString(Uri_fileKeytranslate, namevalue);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static string HttpDownloadFile(string url, string path)
|
|
|
+ {
|
|
|
+ // 设置参数
|
|
|
+ HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
|
|
|
+ //发送请求并获取相应回应数据
|
|
|
+ HttpWebResponse response = request.GetResponse() as HttpWebResponse;
|
|
|
+ //直到request.GetResponse()程序才开始向目标网页发送Post请求
|
|
|
+ Stream responseStream = response.GetResponseStream();
|
|
|
+
|
|
|
+ //创建本地文件写入流
|
|
|
+ Stream stream = new FileStream(path, FileMode.Create);
|
|
|
+
|
|
|
+ byte[] bArr = new byte[1024];
|
|
|
+ int size = responseStream.Read(bArr, 0, (int)bArr.Length);
|
|
|
+ while (size > 0)
|
|
|
+ {
|
|
|
+ stream.Write(bArr, 0, size);
|
|
|
+ size = responseStream.Read(bArr, 0, (int)bArr.Length);
|
|
|
+ }
|
|
|
+ stream.Close();
|
|
|
+ responseStream.Close();
|
|
|
+ return path;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -86,9 +167,11 @@ namespace PDF_Master.Helper
|
|
|
/// <param name="fromlanguage">文本语言</param>
|
|
|
/// <param name="tolanguage">需要翻译的语言</param>
|
|
|
/// <returns></returns>
|
|
|
- public static String fileTranslate(string fileKey, string fromlanguage, string tolanguage)
|
|
|
+ public static async Task<string> fileTranslate(string content, string fromlanguage, string tolanguage)
|
|
|
{
|
|
|
|
|
|
+ FileInfo file = new FileInfo(content);
|
|
|
+ string fileKey = await fileKeyTranslate(content, file.Name);
|
|
|
HttpWebResponse response = null;
|
|
|
ServicePointManager.DefaultConnectionLimit = 200;
|
|
|
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_filetranslate);
|
|
@@ -143,10 +226,28 @@ namespace PDF_Master.Helper
|
|
|
|
|
|
if (jobject["code"].ToObject<string>().ToLower() == "200")
|
|
|
{
|
|
|
-
|
|
|
+ translate = jobject["data"]["ossDownUrl"].ToObject<string>().ToLower();
|
|
|
}
|
|
|
//return jobject["code"].ToObject<string>().ToLower();
|
|
|
-
|
|
|
+ using (var client = new WebClient())
|
|
|
+ {
|
|
|
+ string folderPath = Path.Combine(App.CurrentPath, "DownloadFile");
|
|
|
+ //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
|
|
|
+ if (File.Exists(folderPath))
|
|
|
+ {
|
|
|
+ File.Delete(folderPath);
|
|
|
+ }
|
|
|
+ DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
|
|
|
+ if (!tempfolder.Exists)
|
|
|
+ {
|
|
|
+ tempfolder.Create();
|
|
|
+ }
|
|
|
+ client.DownloadProgressChanged += (sender, e) =>
|
|
|
+ {
|
|
|
+
|
|
|
+ };
|
|
|
+ client.DownloadFile(translate, folderPath+"\\" +file.Name);
|
|
|
+ }
|
|
|
return "200";
|
|
|
}
|
|
|
}
|
|
@@ -165,12 +266,12 @@ namespace PDF_Master.Helper
|
|
|
/// <param name="fromlanguage">文本语言</param>
|
|
|
/// <param name="tolanguage">需要翻译的语言</param>
|
|
|
/// <returns></returns>
|
|
|
- public static String textTranslate(string content, string fromlanguage, string tolanguage)
|
|
|
+ public static String textTranslate(string content, string fromlanguage, string tolanguage,ref string translate)
|
|
|
{
|
|
|
|
|
|
HttpWebResponse response = null;
|
|
|
ServicePointManager.DefaultConnectionLimit = 200;
|
|
|
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_filetranslate);
|
|
|
+ HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_texttranslate);
|
|
|
request.Method = "Post";
|
|
|
request.ContentType = "application/json";
|
|
|
//request.Accept = "application/vnd.api+json;version=1";
|
|
@@ -222,7 +323,7 @@ namespace PDF_Master.Helper
|
|
|
|
|
|
if (jobject["code"].ToObject<string>().ToLower() == "200")
|
|
|
{
|
|
|
-
|
|
|
+ translate = jobject["data"]["dst"].ToObject<string>().ToLower();
|
|
|
}
|
|
|
//return jobject["code"].ToObject<string>().ToLower();
|
|
|
|
|
@@ -236,26 +337,6 @@ namespace PDF_Master.Helper
|
|
|
|
|
|
}
|
|
|
|
|
|
- public async static Task<string> textTranslate(string content, int language)
|
|
|
- {
|
|
|
- System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection();
|
|
|
- //需要翻译的内容
|
|
|
- namevalue["q"] = content;
|
|
|
- //1=中文 2=英文 3=法语 4=汉语
|
|
|
- namevalue["from"] = "auto";
|
|
|
- namevalue["to"] = "en";
|
|
|
- namevalue["projectId"] = "2";
|
|
|
- namevalue["version"] = "1.0.1";
|
|
|
- if (!string.IsNullOrEmpty(Settings.Default.UserDate.id))
|
|
|
- {
|
|
|
- namevalue["userId"] = Settings.Default.UserDate.id;
|
|
|
- }
|
|
|
- return await PostString(Uri_filetranslate, namevalue);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// 纠错
|
|
|
/// </summary>
|
|
@@ -265,14 +346,17 @@ namespace PDF_Master.Helper
|
|
|
{
|
|
|
System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection();
|
|
|
//需要纠错的内容
|
|
|
- namevalue["project_id"] = "3";
|
|
|
+ namevalue["project_id"] = "2";
|
|
|
namevalue["version"] = "1.0.1";
|
|
|
if (!string.IsNullOrEmpty(Settings.Default.UserDate.id))
|
|
|
{
|
|
|
namevalue["user_id"] = Settings.Default.UserDate.id;
|
|
|
}
|
|
|
+ else {
|
|
|
+ namevalue["user_id"] = "3";
|
|
|
+ }
|
|
|
namevalue["content"] = content;
|
|
|
- return await PostString(Uri_Correction, namevalue);
|
|
|
+ return await PostString(Uri_Correction, namevalue, "content");
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -284,14 +368,18 @@ namespace PDF_Master.Helper
|
|
|
{
|
|
|
System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection();
|
|
|
//需要纠错的内容
|
|
|
- namevalue["project_id"] = "3";
|
|
|
+ namevalue["project_id"] = "2";
|
|
|
namevalue["version"] = "1.0.1";
|
|
|
if (!string.IsNullOrEmpty(Settings.Default.UserDate.id))
|
|
|
{
|
|
|
namevalue["user_id"] = Settings.Default.UserDate.id;
|
|
|
}
|
|
|
+ else
|
|
|
+ {
|
|
|
+ namevalue["user_id"] = "3";
|
|
|
+ }
|
|
|
namevalue["content"] = content;
|
|
|
- return await PostString(Uri_Rewrite, namevalue);
|
|
|
+ return await PostString(Uri_Rewrite, namevalue, "content");
|
|
|
}
|
|
|
|
|
|
|
|
@@ -351,19 +439,15 @@ namespace PDF_Master.Helper
|
|
|
//转换成字符串类型
|
|
|
var json = Encoding.Default.GetString(bytes);
|
|
|
//将Json格式字符串转换成键值对
|
|
|
- var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
|
|
|
-
|
|
|
+ //var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
|
|
|
+ JObject jobject = (JObject)JsonConvert.DeserializeObject(json);
|
|
|
string unicode = "";
|
|
|
// 遍历字典对象输出键值对
|
|
|
- foreach (KeyValuePair<string, string> item in values)
|
|
|
- {
|
|
|
- if (item.Key == key)
|
|
|
- {
|
|
|
- unicode = item.Value;
|
|
|
- }
|
|
|
- }
|
|
|
+ unicode = jobject["data"][key].ToObject<string>().ToLower();
|
|
|
+
|
|
|
//将Unicode格式转换成String
|
|
|
- repsonseData = Regex.Unescape(unicode);
|
|
|
+ //repsonseData = Regex.Unescape(unicode);
|
|
|
+ repsonseData = unicode;
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|