This repository has been archived by the owner on Dec 29, 2023. It is now read-only.
/
mtoken.cpp
102 lines (90 loc) · 2.71 KB
/
mtoken.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
#include "mtoken.h"
MToken::MToken() {
manager = new QNetworkAccessManager;
managerT = new QNetworkAccessManager;
tokent = new QTimer();
getToken();
connect(tokent, SIGNAL(timeout()), this, SLOT(upd()));
tokent->start(30000);
}
QByteArray MToken::GET(QUrl r) {
QNetworkReply* reply = manager->get(QNetworkRequest (r));
QEventLoop wait;
connect(manager, SIGNAL(finished(QNetworkReply*)),&wait, SLOT(quit()));
QTimer::singleShot(10000, &wait,SLOT(quit()));
wait.exec();
QByteArray answer = reply->readAll();
reply->deleteLater();
return answer;
}
QByteArray MToken::GETt(QUrl r) {
QNetworkReply* reply = managerT->get(QNetworkRequest (r));
QEventLoop wait;
connect(managerT, SIGNAL(finished(QNetworkReply*)),&wait, SLOT(quit()));
QTimer::singleShot(10000, &wait,SLOT(quit()));
wait.exec();
QByteArray answer = reply->readAll();
reply->deleteLater();
return answer;
}
void MToken::getToken() {
QUrl current("https://www.deviantart.com/oauth2/token");
QUrlQuery par;
par.addQueryItem("grant_type", "client_credentials");
// Add your id and secret to run
par.addQueryItem("client_id", "4146");
par.addQueryItem("client_secret", "74324cd51181766352ee8246178cda69");
current.setQuery(par);
token = QJsonDocument::fromJson(GETt(current)).object().value("access_token").toString();
qDebug() << "Token: " << token;
}
bool MToken::testToken() {
QUrl current("https://www.deviantart.com/api/v1/oauth2/placebo");
QUrlQuery par;
par.addQueryItem("access_token", token);
current.setQuery(par);
QByteArray o = GETt(current);
qDebug() << "Status:" << o;
return QJsonDocument::fromJson(o).object().value("status").toString() == "success";
}
void MToken::upd() { if(!testToken()) getToken();}
void loadFile(QString f, QStringList& list){
QFile i(f);QString t;
i.open(QFile::ReadOnly | QFile::Text);
while(!i.atEnd()){
t = i.readLine();
t = QStringRef(&t, 0, t.length()-1).toString();
list << t;
}
i.close();
}
void loadMFile(QString f, QMap<QString, QString>& map){
QFile i(f);QString t, n;
i.open(QFile::ReadOnly | QFile::Text);
while(!i.atEnd()){
t = i.readLine();
t = QStringRef(&t, 0, t.length()-1).toString();
n = QStringRef(&t, t.indexOf(" ")+1, t.length()-t.indexOf(" ")).toString();
t = QStringRef(&t, 0, t.indexOf(" ")-1).toString();
map.insert(t, n);
}
i.close();
}
void loadFile(QString f, QStringList& list, QStringList check){
QFile i(f);QString t;
i.open(QFile::ReadOnly | QFile::Text);
while(!i.atEnd()){
t = i.readLine();
t = QStringRef(&t, 0, t.length()-1).toString();
if(!check.contains(t))
list << t;
}
i.close();
}
void saveFile(QString f, QStringList list){
QFile i(f);
i.open(QFile::WriteOnly | QFile::Text);
for(QString s : list)
i.write(s.toUtf8() + "\n");
i.close();
}