-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkgquery.cpp
238 lines (188 loc) · 7.01 KB
/
pkgquery.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
#include "pkgquery.h"
PkgQuery::PkgQuery(const QString &pkgname, QObject *parent)
: QObject(parent), m_pkgname(pkgname)
{
}
// 查询包
PkgInfo PkgQuery::queryPkg()
{
PkgInfo _info;
if(m_pkgname.isEmpty())
return _info;
qDebug()<<"start query";
// 首先查询官方仓库
_info = queryPkgInOfficial();
if(_info.pkgname.isEmpty())
{
// 在AUR仓库中查询
_info = queryPkgInAUR();
}
return _info;
}
// 在官方仓库中查询
PkgInfo PkgQuery::queryPkgInOfficial()
{
PkgInfo _info;
// 利用事件循环
QEventLoop eventLoop;
QNetworkRequest request;
// Url编码
QByteArray sUrl = QUrl::toPercentEncoding(m_pkgname.toUtf8());
QString url = QString::fromUtf8(QString("https://www.archlinux.org/packages/search/json/?name=").toUtf8() + sUrl);
//request.setUrl(QUrl("https://www.archlinux.org/packages/search/json/?name=" + m_pkgname));
request.setUrl(url);
QNetworkAccessManager manager;
QObject::connect(&manager, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
QNetworkReply *reply = manager.get(request);
eventLoop.exec();
qDebug()<<"Official query finished!";
// 获取所有返回
QByteArray buf = reply->readAll();
// 转换为JSON
QJsonParseError err;
QJsonDocument document = QJsonDocument::fromJson(buf, &err);
if(err.error != QJsonParseError::NoError)
{
qDebug()<<"转换JSON失败";
}
else
{
// "results" Array
QJsonArray resultsArr = document.object().value("results").toArray();
int x = 0; // 记录要取第几个值
for(int i = 0; i<resultsArr.count(); ++i)
{
if(resultsArr[i].toObject().value("pkgname").toString() == m_pkgname)
{
x = i;
break;
}
}
// 仅获取第一个符合的包
QJsonObject pkgObj = resultsArr[x].toObject();
// 获取JSON值
QString pkgname = pkgObj.value("pkgname").toString();
QString pkgdesc = pkgObj.value("pkgdesc").toString();
QString pkgver = pkgObj.value("pkgver").toString() + "-" + pkgObj.value("pkgrel").toString();
QString url = pkgObj.value("url").toString();
QString last_update = pkgObj.value("last_update").toString();
QString flag_date = pkgObj.value("flag_date").toString();
QString repo = pkgObj.value("repo").toString();
QString maintainers;
QJsonValue maintainersValue = pkgObj.value("maintainers");
if(maintainersValue.isArray())
{
QJsonArray maintainersArr = maintainersValue.toArray();
for(auto maintainerItem : maintainersArr)
{
if(maintainers.isEmpty())
maintainers += maintainerItem.toString();
else
maintainers += " " + maintainerItem.toString();
}
}
// LastUpdate时间格式转换
QDateTime dateTime = QDateTime::fromString(last_update, "yyyy-MM-ddThh:mm:ss.zzzZ");
last_update = dateTime.toString("yyyy年MM月dd日 hh:mm:ss");
// flag_date时间格式转换
dateTime = QDateTime::fromString(flag_date, "yyyy-MM-ddThh:mm:ss.zzzZ");
flag_date = dateTime.toString("yyyy年MM月dd日 hh:mm:ss");
// 赋值
_info.pkgname = pkgname;
_info.pkgdesc = pkgdesc;
_info.pkgver = pkgver;
_info.url = url;
_info.last_update = last_update;
_info.flag_date = flag_date;
_info.maintainers = maintainers;
_info.isAUR = false;
_info.repo = repo;
}
return _info;
}
// 在AUR中查询
PkgInfo PkgQuery::queryPkgInAUR()
{
PkgInfo _info;
// 利用事件循环
QEventLoop eventLoop;
QNetworkRequest request;
// Url编码
QByteArray sUrl = QUrl::toPercentEncoding(m_pkgname.toUtf8());
QString url = QString::fromUtf8(QString(aurPrefix_tuna + "/rpc/?v=5&type=search&arg=").toUtf8() + sUrl);
//request.setUrl(QUrl("https://aur.archlinux.org/rpc/?v=5&type=search&arg=" + _pkgname));
//request.setUrl(QUrl(aurPrefix_tuna + "/rpc/?v=5&type=search&arg=" + m_pkgname)); // Tuna反代
request.setUrl(url);
QNetworkAccessManager manager;
QObject::connect(&manager, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
QNetworkReply *reply = manager.get(request);
eventLoop.exec();
qDebug()<<"AUR query finished!";
// 获取所有返回
QByteArray buf = reply->readAll();
// 转换为JSON
QJsonParseError err;
QJsonDocument document = QJsonDocument::fromJson(buf, &err);
if(err.error != QJsonParseError::NoError)
{
qDebug()<<"转换JSON失败";
}
else
{
// 首先判断 "resultcount" 字段
if(document.object().value("resultcount").toInt() <= 0)
return _info; // 结果为空
// "results" Array
QJsonArray resultsArr = document.object().value("results").toArray();
int x = 0; // 记录要取第几个值
for(int i = 0; i<resultsArr.count(); ++i)
{
if(resultsArr[i].toObject().value("Name").toString() == m_pkgname)
{
x = i;
break;
}
}
// 先循环一遍
// 仅获取第一个符合的包
QJsonObject pkgObj = resultsArr[x].toObject();
// 获取JSON值
QString pkgname = pkgObj.value("Name").toString();
QString pkgdesc = pkgObj.value("Description").toString();
QString pkgver = pkgObj.value("Version").toString();
QString url = pkgObj.value("URL").toString();
// outofdate 以时间戳形式,需要特殊处理
QString outofdate;
int outofdateInt = pkgObj.value("OutOfDate").toInt();
if(outofdateInt != 0)
{
//qDebug()<<"outofdateInt:"<<outofdateInt;
QDateTime dateTime = QDateTime::fromSecsSinceEpoch(outofdateInt);
outofdate = dateTime.toString("yyyy年MM月dd日 hh:mm:ss");
}
// lastModified 以时间戳形式,需要特殊处理
QString lastModified;
int lastModifiedInt = pkgObj.value("LastModified").toInt();
if(lastModifiedInt != 0)
{
QDateTime dateTime = QDateTime::fromSecsSinceEpoch(lastModifiedInt);
lastModified = dateTime.toString("yyyy年MM月dd日 hh:mm:ss");
}
QString maintainers = pkgObj.value("Maintainer").toString();
int numvotes = pkgObj.value("NumVotes").toInt();
QString urlPath = pkgObj.value("URLPath").toString();
// 赋值
_info.pkgname = pkgname;
_info.pkgdesc = pkgdesc;
_info.pkgver = pkgver;
_info.outofdate = outofdate;
_info.lastModified =lastModified;
_info.url = url;
_info.maintainers = maintainers;
_info.numvotes = numvotes;
_info.isAUR = true;
//_info.urlPath = aurPrefix + pkgname;
_info.urlPath = aurPrefix_tuna + "/packages/" + pkgname;
}
return _info;
}