-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexternalpageparser.cpp
116 lines (109 loc) · 3.77 KB
/
externalpageparser.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
#include "externalpageparser.h"
ExternalPageParser::ExternalPageParser(const Config& c, QObject *parent) :
QObject(parent)
{
config = c;
manager = new QNetworkAccessManager(this);
}
Promise* ExternalPageParser::fetchFileInfo(const QStringList& list)
{
QList<Promise*> ps;
int ind = 0;
for(QString name : list){
if(config.contains(name)){
auto& course = config[name];
for(auto it = course.constBegin(); it != course.constEnd(); it++){
ps.push_back(process(name, it.key(), it.value()));
}
}
}
return Promise::some(ps, this)->pipe([=](const QVariant& datas){
QVariantList list = datas.toList();
QMap<QString, QMap<QString, QMap<QString, QVariantMap>>> aggregate;
QVariantMap top;
for(auto d : list){
if(d.isValid()){
auto map = d.toMap();
QString course = map["course"].toString();
QString dir = map["dir"].toString();
QVariantMap files = map["result"].toMap();
if(dir == QString(".")){
top[course] = files;
}else{
aggregate[course]["folders"][dir]["files"] = files;
}
}
}
QVariantMap map;
for(auto it = aggregate.constBegin(); it != aggregate.constEnd(); ++it){
auto& mm = it.value();
QVariantMap nm;
for(auto itt = mm.constBegin(); itt != mm.constEnd(); ++itt){
auto& mmm = itt.value();
QVariantMap m;
for(auto ittt = mmm.constBegin(); ittt != mmm.constEnd(); ++ittt){
m[ittt.key()] = ittt.value();
}
nm[itt.key()] = m;
}
map[it.key()] = nm;
}
for(auto it = top.constBegin(); it != top.constEnd(); ++it){
auto c = map[it.key()].toMap();
c["files"] = it.value();
map[it.key()] = c;
}
return (new Promise(this))->resolve(QVariant(map));
});
}
void ExternalPageParser::setConfig(const Config& c)
{
config = c;
}
const QVariantMap ExternalPageParser::resultListToMap(const QList<QString>& result, const QUrl& base)
{
QVariantMap m;
for(auto r : result){
auto filename = r.right(r.size() - r.lastIndexOf("/") - 1);
if(filename.isEmpty()){
r = r.left(r.size() - 1);
filename = r.right(r.size() - r.lastIndexOf("/") - 1);
}
if(filename.indexOf(".") == -1){
filename += QString(".html");
}
QVariantMap file;
file["name"] = filename;
m[base.resolved(QUrl(r)).toString()] = file;
}
return m;
}
Promise* ExternalPageParser::process(const QString& course, const QString& dir, const QPair<QUrl, QString>& info)
{
Promise* p = new Promise(this);
auto r = manager->get(QNetworkRequest(info.first));
connect(r, &QNetworkReply::finished, [=](){
if (r->error() == QNetworkReply::NoError){
QByteArray content = r->readAll();
QWebPage* page = new QWebPage(this);
page->mainFrame()->setHtml(content);
auto mf = page->mainFrame();
auto els = mf->findAllElements(info.second);
qDebug()<<"selector"<<info.second<<els.count();
QList<QString> result;
for(const auto& el : els){
result.push_back(el.attribute("href"));
}
QVariantMap data;
data["course"] = course;
data["dir"] = dir;
data["result"] = resultListToMap(result, info.first);
p->resolve(data);
page->deleteLater();
}else{
p->reject(QVariant());
}
r->deleteLater();
});
return p;
}