-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetuquery.cpp
127 lines (100 loc) · 3.34 KB
/
setuquery.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
#include "setuquery.h"
SetuQuery::SetuQuery(const QString& keyword, QObject *parent)
: QObject(parent), m_keyword(keyword)
{
}
SetuInfo SetuQuery::querySetu()
{
SetuInfo _info;
// 利用事件循环
QEventLoop eventLoop;
QNetworkRequest request;
QUrl url;
if(m_keyword.isEmpty())
url.setUrl("https://api.lolicon.app/setu/?apikey=" + APIkey + "&" + "r18=1&num=1&size1200=true"); // 无关键词
else
url.setUrl("https://api.lolicon.app/setu/?apikey=" + APIkey + "&keyword=" + QUrl::toPercentEncoding(m_keyword) + "&" + "r18=1&num=1&size1200=true"); // 含有关键词
request.setUrl(url);
QNetworkAccessManager manager;
QObject::connect(&manager, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
QNetworkReply *reply = manager.get(request);
eventLoop.exec();
qDebug()<<"Setu query finished!";
// 获取所有返回
QByteArray buf = reply->readAll();
// 转换为JSON
QJsonParseError err;
QJsonDocument document = QJsonDocument::fromJson(buf, &err);
if(err.error != QJsonParseError::NoError)
{
qDebug()<<"转换JSON失败";
}
else
{
int code = document.object().value("code").toInt();
int quota = document.object().value("quota").toInt();
int quota_min_ttl = document.object().value("quota_min_ttl").toInt();
// "data" Array
QJsonArray dataArr = document.object().value("data").toArray();
// 仅获取第一个图
QJsonObject setuObj = dataArr[0].toObject();
// 获取JSON值
QString url = setuObj.value("url").toString();
// 赋值
_info.url = url.mid(url.lastIndexOf("/")+1);
_info.code = code;
_info.quota = quota;
_info.quota_min_ttl = quota_min_ttl;
// 下载图片
if(!url.isEmpty())
downloadPhoto(url);
}
return _info;
}
void SetuQuery::downloadPhoto(const QString &url)
{
// 检查目录下图片缓存目录 pics 是否存在
QDir dir = QDir::currentPath();
if(!dir.exists("/pics"))
{
dir.mkdir("pics");
}
downloadFile = new QFile("pics/"+url.mid(url.lastIndexOf("/")+1));
if(!downloadFile->open(QIODevice::WriteOnly))
{
qDebug()<<"图片打开失败";
delete downloadFile;
downloadFile = 0;
}
QEventLoop evenLoop;
// 开始下载
QNetworkRequest request;
request.setUrl(url);
downloadReply = downloadManager.get(request);
// 下载完成后
connect(downloadReply, SIGNAL(finished()), this, SLOT(downloadFinished()));
connect(downloadReply,&QNetworkReply::finished, &evenLoop, &QEventLoop::quit);
// 有可用数据
connect(downloadReply, SIGNAL(readyRead()), this, SLOT(downloadReady()));
// 下载进度更新
connect(downloadReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateDataReadProgress(qint64, qint64)));
evenLoop.exec();
}
void SetuQuery::downloadFinished()
{
downloadFile->flush();
downloadFile->close();
downloadReply->deleteLater();
downloadReply = 0;
delete downloadFile;
downloadFile = 0;
}
void SetuQuery::downloadReady()
{
if(downloadFile)
downloadFile->write(downloadReply->readAll());
}
void SetuQuery::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
{
qDebug()<<bytesRead<<":"<<totalBytes;
}