-
Notifications
You must be signed in to change notification settings - Fork 1
/
trans.cpp
130 lines (114 loc) · 3.41 KB
/
trans.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "trans.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QString>
#include <QCryptographicHash>
#include <QEventLoop>
#include <time.h>
#include <QDebug>
QString toMd5(QString str)
{
QString md5;
QByteArray array;
array = QCryptographicHash::hash(str.toUtf8(), QCryptographicHash::Md5);
md5 = array.toHex();
return md5;
}
QString signCreate(QString appid, QString q, int salt, QString key)
{
QString ret = QString("%1%2%3%4").arg(appid).arg(q).arg(salt).arg(key);
return toMd5(ret);
}
class Parser: public QObject
{
Q_OBJECT
public:
bool init()
{
qsrand(time(0));
url = "http://api.fanyi.baidu.com/api/trans/vip/translate";
appid = "20180416000147193";
key = "IpGYAVJ3NZvEWXM_5Ujw";
return true;
}
QPair <bool , QString> trans(const QString & string, const QString & to , const QString & from)
{
QEventLoop event;
salt = qrand() % INT_MAX;
//create postStr
sign = signCreate(appid, string, salt, key);
QString format = QString("q=%1&appid=%4&salt=%5&from=%2&to=%3&sign=%6")
.arg(string)
.arg(from)
.arg(to)
.arg(appid)
.arg(salt)
.arg(sign);
qDebug() << format;
QString postStr = QString("%7?q=%1&appid=%4&salt=%5&from=%2&to=%3&sign=%6")
.arg(string)
.arg(from)
.arg(to)
.arg(appid)
.arg(salt)
.arg(sign)
.arg(url);
QByteArray buf;
QNetworkRequest request = QNetworkRequest(QUrl(postStr));
request.setRawHeader("Content-Type", "application"); //ÇëÇóÍ·
QNetworkReply *reply = NULL;
auto c1 = connect(&himma, &QNetworkAccessManager::finished, [&](){
if (reply) {
buf = reply->readAll();
event.exit(1);
}
});
// const QByteArray array = format.toUtf8();
reply = himma.get(request);
// replay = himma.post(request, format.toUtf8());
event.exec();
disconnect(c1);
if (buf.isEmpty()) {
qDebug() << "empty";
return {false, string};
} else {
QString jason = buf;
qDebug() << jason;
}
QJsonObject data(QJsonDocument::fromJson(buf).object());
if(!data.contains("trans_result")
|| data["trans_result"].toArray().isEmpty()
|| !data["trans_result"].toArray()[0].toObject().contains("dst")) {
qDebug() << "no result";
return { false, QString() };
}
return { true, data["trans_result"].toArray()[0].toObject()["dst"].toString() };
//·µ»Ø¸ñʽ{"from":"en","to":"zh","trans_result":[{"src":"apple","dst":"\u82f9\u679c"}]}
}
private:
QString url;
QString q;
QString from;
QString to;
QString appid;
int salt;
QString key;
QString sign;
QNetworkAccessManager himma;
};
Trans::Trans( QObject *parent) : QObject(parent)
{
parser = new Parser;
parser->init();
}
QPair <bool, QString> Trans::translate(const QString & string, const QString & to , const QString & from )
{
QByteArray a = string.toUtf8();
QString str = a;
return parser->trans(str, to, from);
}
#include "trans.moc"
//