This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
forked from pinkierton/harkach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainApp.cpp
190 lines (150 loc) · 7.18 KB
/
MainApp.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
#include "MainApp.h"
#include <QDebug>
#include <QHttpMultiPart>
#include <QUrlQuery>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QJsonArray>
#include <QStringBuilder>
#include <QDateTime>
#include <QFile>
#include "Board.h"
#include <QEventLoop>
#include <QQmlListProperty>
const QUrl MainApp::postUrl(QStringLiteral("https://2ch.hk/makaba/posting.fcgi?json=1"));
const QUrl MainApp::getBoardsUrl(QStringLiteral("https://2ch.hk/makaba/mobile.fcgi?task=get_boards"));
MainApp::MainApp(QObject *parent): QObject(parent)
{
QSettings settings;
int size = settings.beginReadArray("boards");
if (size > 0) {
QList<Board*> boards;
boards.reserve(size);
for (int i = 0; i < size; ++i) {
settings.setArrayIndex(i);
QString id = settings.value(QLatin1String("id")).toString();
QString name = settings.value(QLatin1String("name")).toString();
QString category = settings.value(QLatin1String("category")).toString();
QString defaultName = settings.value(QLatin1String("defaultName")).toString();
int bumpLimit = settings.value(QLatin1String("bumpLimit")).toInt();
int pages = settings.value(QLatin1String("pages")).toInt();
bool enableLikes = settings.value(QLatin1String("enableLikes")).toBool();
bool enablePosting = settings.value(QLatin1String("enablePosting")).toBool();
bool enableThreadTags = settings.value(QLatin1String("enableThreadTags")).toBool();
bool sage = settings.value(QLatin1String("sage")).toBool();
bool tripcodes = settings.value(QLatin1String("tripcodes")).toBool();
Board *board = new Board(id, name, category, defaultName, bumpLimit, pages, enableLikes, enablePosting, enableThreadTags, sage, tripcodes);
boards << board;
}
mBoardModel.setBoards(std::move(boards));
}
else {
requestBoards();
}
settings.endArray();
}
BoardModel *MainApp::getBoardModel() const {
return const_cast<BoardModel* const>(&mBoardModel);
}
void MainApp::requestBoards() {
QNetworkRequest request(getBoardsUrl);
QNetworkReply *reply = manager.get(request);
connect(reply, &QNetworkReply::finished, this, &MainApp::processBoards);
}
void MainApp::processBoards() {
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
auto error = reply->error();
if (error) {
qDebug() << Q_FUNC_INFO << error;
} else {
QByteArray data = reply->readAll();
QJsonParseError parseError;
QJsonDocument doc = QJsonDocument::fromJson(data, &parseError);
if (parseError.error) {
qWarning() << parseError.error << parseError.errorString();
} else {
QList<Board*> boards;
boards.reserve(256);
auto obj = doc.object();
for (QJsonObject::Iterator iterCat = obj.begin(); iterCat!=obj.end(); ++iterCat) {
QJsonArray boardArr = iterCat.value().toArray();
for (QJsonArray::Iterator iterBoard = boardArr.begin(); iterBoard!=boardArr.end(); ++iterBoard) {
QJsonObject boardObj = iterBoard->toObject();
boards << new Board(boardObj, &mBoardModel);
}
}
QSettings settings;
settings.beginWriteArray("boards");
int i = 0;
for (Board* board : qAsConst(boards)) {
settings.setArrayIndex(i++);
settings.setValue(QLatin1String("id"), board->id());
settings.setValue(QLatin1String("name"), board->name());
settings.setValue(QLatin1String("category"), board->category());
settings.setValue(QLatin1String("defaultName"), board->defaultName());
settings.setValue(QLatin1String("bumpLimit"), board->bumpLimit());
settings.setValue(QLatin1String("pages"), board->pages());
settings.setValue(QLatin1String("enableLikes"), board->enableLikes());
settings.setValue(QLatin1String("enablePosting"), board->enablePosting());
settings.setValue(QLatin1String("enableThreadTags"), board->enableThreadTags());
settings.setValue(QLatin1String("sage"), board->sage());
settings.setValue(QLatin1String("tripcodes"), board->tripcodes());
}
settings.endArray();
mBoardModel.setBoards(std::move(boards));
}
}
reply->deleteLater();
}
void MainApp::sendPost(const QString &board, const QString &thread, const QString &post, const QString &id, const QString &captcha) {
//qDebug() << Q_FUNC_INFO << board << thread << post << id << captcha;
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart taskPart;
taskPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"task\""));
taskPart.setBody("post");
QHttpPart boardPart;
boardPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"board\""));
boardPart.setBody(board.toUtf8());
QHttpPart threadPart;
threadPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"thread\""));
threadPart.setBody(thread.toUtf8());
QHttpPart commentPart;
commentPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"comment\""));
commentPart.setBody(post.toUtf8());
QHttpPart captchaTypePart;
captchaTypePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"captcha_type\""));
captchaTypePart.setBody("2chaptcha");
QHttpPart captchaIdPart;
captchaIdPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"2chaptcha_id\""));
captchaIdPart.setBody(id.toUtf8());
QHttpPart captchaValuePart;
captchaValuePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"2chaptcha_value\""));
captchaValuePart.setBody(captcha.toUtf8());
multiPart->append(taskPart);
multiPart->append(boardPart);
multiPart->append(threadPart);
multiPart->append(commentPart);
multiPart->append(captchaTypePart);
multiPart->append(captchaIdPart);
multiPart->append(captchaValuePart);
QNetworkRequest request(postUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("multipart/form-data; boundary=").append(multiPart->boundary()));
//request.setHeader(QNetworkRequest::UserAgentHeader, "Abuloh/5.0 (windows; DOS x86_64) AppleWebKit/538.1 (KHTML, like Gecko) abupidor/0.0.1 abupidor/538.1");
QNetworkReply *reply = manager.post(request, multiPart);
multiPart->setParent(reply);
connect(reply, &QNetworkReply::finished, [reply](){
auto error = reply->error();
if (error) {
qDebug() << error;
} else {
QByteArray data = reply->readAll();
QString stringData(data);
qDebug() << stringData;
}
reply->deleteLater();
});
}
ThreadNum MainApp::makeThreadNum(const QString &board, int thread, const QString &subj) const noexcept {
return ThreadNum{board, thread, subj};
}