-
Notifications
You must be signed in to change notification settings - Fork 1
/
fm_mainwindowproxy.cpp
146 lines (129 loc) · 3.95 KB
/
fm_mainwindowproxy.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
#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QFile>
#include <QDebug>
#include <QThread>
#include <QTextCodec>
#include "fm_mainwindowproxy.h"
#include "fm_json.h"
#define ADDNEWS true
#define DELETENEWS false
FM_MainWindowProxy::FM_MainWindowProxy(FM_MainWindow *mw, QObject *parent)
: QObject(parent),
mainwindow(mw)
{
}
void FM_MainWindowProxy::threadStarted(void)
{
qDebug() << "Thread has started...ID is:" << QThread::currentThreadId();
}
void FM_MainWindowProxy::threadFinished(void)
{
qDebug() << "Thread has stopped Id is:" << QThread::currentThreadId();
}
void FM_MainWindowProxy::run()
{
}
void FM_MainWindowProxy::getNews(QString web)
{
QVector<QString> column_str;
QVector<bool> column_bool;
//等待界面
emit wait();
QThread::msleep(300);
mainwindow->settings->get_web_columns(web, column_str, column_bool);
int max_display_news = mainwindow->settings->get_max_display_news();
//获取新闻内容
QJsonObject news = FM_Json::readJson("./" + web + ".json").object();
QJsonObject news_type = news.value(web).toObject();
for (int i = 0; i < column_str.size(); i++)
{
if (column_bool[i])
{
QJsonArray array = news_type.value(column_str[i]).toArray(); //网站新闻
for (int i = 0; i < array.size() && i < max_display_news; i++)
{
QJsonArray array1 = array.at(i).toArray(); //单条新闻
emit addNewsItemToUI(array1.at(0).toString(),
array1.at(1).toString(),
array1.at(2).toString(),
array1.at(3).toString(),
true);
}
}
}
mainwindow->pageState = PageState::OtherPage;
//关闭等待界面
emit stopwait();
}
void FM_MainWindowProxy::getFavorNews()
{
emit clearNewsinUI();
QJsonObject favor = FM_Json::readJson("./favorite.json").object();
QJsonArray array = favor.value("favorite").toArray();
for (int i = 0; i < array.size(); i++)
{
QJsonArray array1 = array.at(i).toArray(); //单条新闻
emit addNewsItemToUI(array1.at(0).toString(),
array1.at(1).toString(),
array1.at(2).toString(),
array1.at(3).toString(),
false);
}
mainwindow->pageState = PageState::FavorPage;
}
void FM_MainWindowProxy::writeFavor(QString title, QString data, QString abstract, QString address, bool type)
{
QJsonArray array;
array.insert(0, title);
array.insert(1, data);
array.insert(2, abstract);
array.insert(3, address);
FM_Json::writeJson("./favorite.json", array, type);
}
void FM_MainWindowProxy::startNewsCrawler()
{
// 获取要刷新的新闻网站
if (mainwindow->now_website == "全部新闻")
mainwindow->settings->get_web_list(crawler_Weblist);
else
crawler_Weblist.append(mainwindow->now_website);
// 分别爬取每个网站
while (crawler_Weblist.size() != 0)
{
QStringList web;
web.append(crawler_Weblist.takeAt(0));
crawler(web);
}
emit pythonEnd();
}
void FM_MainWindowProxy::startNoticeCrawler()
{
QStringList web;
web.append("教务处");
crawler(web);
emit inform_notice();
}
void FM_MainWindowProxy::crawler(QStringList web)
{
process = new QProcess;
process->start("./crawler.exe", web);
if (!process->waitForStarted())
{
qDebug() << "爬虫启动失败";
}
if(process->waitForFinished(-1))
{
QByteArray data = process->readAll();
QString string = QTextCodec::codecForMib(106)->toUnicode(data);//106:UTF-8
if( string != "done\r\n")
{
qDebug() << "爬虫爬取失败";
}
process->close();
delete process;
}
}