-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathyoudaoapi.cpp
255 lines (208 loc) · 7.91 KB
/
youdaoapi.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* Copyright (C) 2017 ~ 2017 Deepin Technology Co., Ltd.
*
* Author: rekols <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "youdaoapi.h"
#include <QUrlQuery>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>
#include <QDateTime>
static YoudaoAPI *INSTANCE = nullptr;
YoudaoAPI* YoudaoAPI::instance()
{
if (INSTANCE == nullptr) {
INSTANCE = new YoudaoAPI;
}
return INSTANCE;
}
YoudaoAPI::YoudaoAPI(QObject *parent)
: QObject(parent),
m_http(new QNetworkAccessManager(this))
{
}
YoudaoAPI::~YoudaoAPI()
{
delete m_http;
}
void YoudaoAPI::queryWord(const QString &text)
{
QUrl url("http://dict.youdao.com/jsonresult");
QUrlQuery query;
query.addQueryItem("q", text);
query.addQueryItem("type", "1");
query.addQueryItem("client", "deskdict");
query.addQueryItem("keyfrom", "deskdict_deepin");
query.addQueryItem("pos", "-1");
query.addQueryItem("len", "eng");
url.setQuery(query.toString(QUrl::FullyEncoded));
QNetworkRequest request(url);
QNetworkReply *reply = m_http->get(request);
connect(reply, &QNetworkReply::finished, this, &YoudaoAPI::handleQueryWordFinished);
}
void YoudaoAPI::suggest(const QString &text)
{
QUrl url("http://dict.youdao.com/suggest");
QUrlQuery query;
query.addQueryItem("type", "DESKDICT");
query.addQueryItem("client", "deskdict");
query.addQueryItem("keyfrom", "deskdict_deepin");
query.addQueryItem("num", "4");
query.addQueryItem("q", text);
query.addQueryItem("ver", "2.0");
query.addQueryItem("le", "eng");
query.addQueryItem("doctype", "json");
url.setQuery(query.toString(QUrl::FullyEncoded));
QNetworkRequest request(url);
QNetworkReply *reply = m_http->get(request);
connect(reply, &QNetworkReply::finished, this, &YoudaoAPI::handleSuggestFinished);
}
void YoudaoAPI::translate(const QString &text, const QString &type)
{
QUrl url("http://fanyi.youdao.com/translate");
QUrlQuery query;
query.addQueryItem("dogVersion", "1.0");
query.addQueryItem("ue", "utf8");
query.addQueryItem("doctype", "json");
query.addQueryItem("xmlVersion", "1.6");
query.addQueryItem("client", "deskdict_deepin");
query.addQueryItem("id", "92dc50aa4970fb72d");
query.addQueryItem("vendor", "YoudaoDict");
query.addQueryItem("appVer", "1.0.3");
query.addQueryItem("appZengqiang", "0");
query.addQueryItem("abTest", "5");
query.addQueryItem("smartresult", "dict");
query.addQueryItem("smartresult", "rule");
query.addQueryItem("keyfrom", "deskdict.main");
query.addQueryItem("i", text);
query.addQueryItem("type", type);
url.setQuery(query.toString(QUrl::FullyEncoded));
QNetworkRequest request(url);
QNetworkReply *reply = m_http->get(request);
connect(reply, &QNetworkReply::finished, this, &YoudaoAPI::handleTranslateFinished);
}
void YoudaoAPI::queryDaily()
{
QUrl url("http://dict.youdao.com/infoline/style/cardList");
QUrlQuery query;
query.addQueryItem("apiversion", "3.0");
query.addQueryItem("client", "deskdict");
query.addQueryItem("style", "daily");
query.addQueryItem("lastId", "0");
query.addQueryItem("keyfrom", "deskdict.8.1.2.0");
query.addQueryItem("size", "1");
url.setQuery(query.toString(QUrl::FullyEncoded));
QNetworkRequest request(url);
QNetworkReply *reply = m_http->get(request);
connect(reply, &QNetworkReply::finished, this, &YoudaoAPI::handleQueryDailyFinished);
}
void YoudaoAPI::handleQueryWordFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
if (reply->error() != QNetworkReply::NoError) {
return;
}
QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
QJsonObject object = document.object();
if (!document.isEmpty()) {
QString queryWord = object.value("rq").toString();
QString ukPhonetic = object.value("uksm").toString();
QString usPhonetic = object.value("ussm").toString();
QString basicExplains("");
QString webReferences("");
QJsonArray explain = object.value("basic").toArray();
// get the basic data.
for (const QJsonValue &value : explain) {
basicExplains.append(value.toString());
basicExplains.append("<br>");
}
// Access to the web references.
QJsonArray webRefArray = object.value("web").toArray();
if (!webRefArray.isEmpty()) {
for (const QJsonValue &value : webRefArray) {
QJsonObject obj = value.toObject();
QString key = obj.keys().first();
QJsonArray arr = obj.value(key).toArray();
for (const QJsonValue &value : arr) {
webReferences += "<br>";
webReferences += QString("• %1 : %2").arg(key).arg(value.toString());
webReferences += "</br>";
}
}
}
emit searchFinished(std::make_tuple(queryWord, ukPhonetic, usPhonetic, basicExplains, webReferences));
}
}
void YoudaoAPI::handleQueryDailyFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
if (reply->error() != QNetworkReply::NoError) {
return;
}
QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
QJsonObject object = document.array().at(0).toObject();
const QString title = object.value("title").toString();
const QString summary = object.value("summary").toString();
const QString voiceURL = object.value("voice").toString();
const QString imageURL = object.value("gif").toArray().at(0).toString();
const long long startTime = object.value("startTime").toVariant().toLongLong();
QDateTime dateTime;
QString time;
dateTime = QDateTime::fromString(QString::number(startTime), "yyyyMMddhhmm");
time = dateTime.toString("yyyy-MM-dd");
emit dailyFinished(std::make_tuple(title, summary, time, voiceURL, imageURL));
}
void YoudaoAPI::handleTranslateFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
if (reply->error() != QNetworkReply::NoError) {
return;
}
QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
QJsonArray array = document.object().value("translateResult").toArray();
QString text;
for (const QJsonValue &value : array) {
QJsonArray arr = value.toArray();
for (const QJsonValue &value : arr) {
QString ret = value.toObject().value("tgt").toString();
text += ret;
text += "\n";
}
}
emit translateFinished(text);
}
void YoudaoAPI::handleSuggestFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
if (reply->error() != QNetworkReply::NoError) {
return;
}
QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
QJsonObject object = document.object().value("data").toObject();
QJsonArray array = object.value("entries").toArray();
QStringList list;
for (const QJsonValue &value : array) {
QJsonObject obj = value.toObject();
QString entry = obj.value("entry").toString();
const QString explain = obj.value("explain").toString();
entry.push_back(" | ");
entry.push_back(explain);
list << entry;
}
emit suggestFinished(list);
}