forked from bnufree/Hq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
qeastmoneystockinfothread.cpp
248 lines (225 loc) · 8.77 KB
/
qeastmoneystockinfothread.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "qeastmoneystockinfothread.h"
#include <QDateTime>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QEventLoop>
#include <QDebug>
#include "stkmktcap.h"
#include "stockdata.h"
#include "stkinfofilemanage.h"
qeastmoneystockinfothread::qeastmoneystockinfothread(const QString& code, QObject *parent) : mCode(code), QThread(parent)
{
mCode = code;
if(mCode.length() != 6)
{
mCode = mCode.right(6);
}
}
qeastmoneystockinfothread::~qeastmoneystockinfothread()
{
}
void qeastmoneystockinfothread::updateHistoryStkInfo()
{
#if 0
//先检查是否需要更新,取得最近更新的日期
//下载前30天的数据,如果数据
QNetworkAccessManager *mgr = new QNetworkAccessManager;
foreach (QString code, mStockcodeList) {
emit sendUpdateProgress(1, 1);
bool update = true;
StkInfoFileManage *filemgr = new StkInfoFileManage(code.right(6));
QStringList secs = filemgr->getAllSections();
//检查旧的时间格式,然后删除
foreach (QString sec, secs) {
QRegExp exp("[0-9]{4}-[0-9]{2}-[0-9]{2}");
if(exp.exactMatch(sec))
{
filemgr->removeGroup(sec);
}
}
//取得上次更新的时间
secs = filemgr->subkeys("Dates");
if(secs.length())
{
secs.sort();
//qDebug()<<"Dates:"<<secs;
QString dateStre = secs.last();
QDateTime lastDate = QDateTime::fromString(dateStre, "yyyy-MM-dd");
if(lastDate.date() == /*QDateTime::currentDateTime().addDays(-1).date()*/lastActiveDay())
{
update = false;
}
}
if(update == false) continue;
//开始更新
filemgr->removeGroup("Dates");
QDateTime start = QDateTime::currentDateTime().addDays(-11);
QDateTime end = QDateTime::currentDateTime().addDays(-1);
code = code.right(6);
if(code.left(1) == "6" || code.left(1) == "5")
{
code = "0" + code;
} else
{
code = "1" + code;
}
//http://quotes.money.163.com/service/chddata.html?code=1000002&start=19910129&end=20161006&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP
QString wkURL = QString("http://quotes.money.163.com/service/chddata.html?code=%1&start=%2&end=%3&fields=TCLOSE;LCLOSE;VOTURNOVER;VATURNOVER;TCAP;MCAP;PCHG")
.arg(code).arg(start.toString("yyyyMMdd")).arg(end.toString("yyyyMMdd"));
//qDebug()<<"url:"<<wkURL;
QNetworkReply *reply = mgr->get(QNetworkRequest(wkURL));
if(!reply)
{
continue;
}
QEventLoop loop; // 使用事件循环使得网络通讯同步进行
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec(); // 进入事件循环, 直到reply的finished()信号发出, 这个语句才能退出
if(reply->error())
{
reply->deleteLater();
continue;
}
#endif
}
void qeastmoneystockinfothread::run()
{
#if 0
//下载前30天的数据,如果数据
QNetworkAccessManager *mgr = new QNetworkAccessManager;
foreach (QString code, mStockcodeList) {
emit sendUpdateProgress(1, 1);
bool update = true;
StkInfoFileManage *filemgr = new StkInfoFileManage(code.right(6));
QStringList secs = filemgr->getAllSections();
//检查旧的时间格式,然后删除
foreach (QString sec, secs) {
QRegExp exp("[0-9]{4}-[0-9]{2}-[0-9]{2}");
if(exp.exactMatch(sec))
{
filemgr->removeGroup(sec);
}
}
//取得上次更新的时间
secs = filemgr->subkeys("Dates");
if(secs.length())
{
secs.sort();
//qDebug()<<"Dates:"<<secs;
QString dateStre = secs.last();
QDateTime lastDate = QDateTime::fromString(dateStre, "yyyy-MM-dd");
if(lastDate.date() == /*QDateTime::currentDateTime().addDays(-1).date()*/lastActiveDay())
{
update = false;
}
}
if(update == false) continue;
//开始更新
filemgr->removeGroup("Dates");
QDateTime start = QDateTime::currentDateTime().addDays(-11);
QDateTime end = QDateTime::currentDateTime().addDays(-1);
code = code.right(6);
if(code.left(1) == "6" || code.left(1) == "5")
{
code = "0" + code;
} else
{
code = "1" + code;
}
//http://quotes.money.163.com/service/chddata.html?code=1000002&start=19910129&end=20161006&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP
QString wkURL = QString("http://quotes.money.163.com/service/chddata.html?code=%1&start=%2&end=%3&fields=TCLOSE;LCLOSE;VOTURNOVER;VATURNOVER;TCAP;MCAP;PCHG")
.arg(code).arg(start.toString("yyyyMMdd")).arg(end.toString("yyyyMMdd"));
//qDebug()<<"url:"<<wkURL;
QNetworkReply *reply = mgr->get(QNetworkRequest(wkURL));
if(!reply)
{
continue;
}
QEventLoop loop; // 使用事件循环使得网络通讯同步进行
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec(); // 进入事件循环, 直到reply的finished()信号发出, 这个语句才能退出
if(reply->error())
{
reply->deleteLater();
continue;
}
//开始解析数据
int index = -1;
QByteArray bytes;
int k=0;
double l3 = 0.0, l5 = 0.0;
while((bytes = reply->readLine()).length())
{
index++;
if(index == 0) continue;
QString result = QString::fromLocal8Bit(bytes.data());
if( !result.isEmpty() )
{
QStringList blockInfoList = result.split(QRegExp("[,\"'\s\r\t]"), QString::SkipEmptyParts);
if(blockInfoList.length() > 9)
{
if(k<3) l3 += blockInfoList[9].toDouble();
if(k<5) l5 += blockInfoList[9].toDouble();
k++;
QString wkDate = blockInfoList[0];
QStringList wklist = blockInfoList.mid(6, 3);
filemgr->setValue("Dates", wkDate, wklist);
}
}
}
//计算3日涨幅
filemgr->setValue("Chg", "L3", l3);
filemgr->setValue("Chg", "L5", l5);
//qDebug()<<"code:"<<code<<" L3:"<<l3<<" L5:"<<l5;
reply->deleteLater();
//开始取得股本数据
code = code.right(6);
if(code.left(1) == "6" || code.left(1) == "5")
{
code += "1";
} else
{
code += "2";
}
wkURL = QString("http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=P.[(x)]|%1&sty=MPICT&st=z&sr=&p=&ps=&cb=callback&js=&token=aaf32a88de888ea7b0ea63e017ecb049&_=%2")
.arg(code).arg(QDateTime::currentMSecsSinceEpoch());
reply = mgr->get(QNetworkRequest(wkURL));
if(!reply)
{
continue;
}
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec(); // 进入事件循环, 直到reply的finished()信号发出, 这个语句才能退出
if(reply->error())
{
reply->deleteLater();
continue;
}
//开始解析数据
bytes = reply->readAll();
QString result = QString::fromUtf8(bytes.data());
int startindex = -1, endindex = -1;
startindex = result.indexOf("[[") + 2;
endindex = result.indexOf("]]");
if(startindex < 0 || endindex < 0) continue;
QString hqStr = result.mid(startindex, endindex - startindex);
//qDebug()<<"info:"<<hqStr;
if( !hqStr.isEmpty() )
{
QStringList blockInfoList = hqStr.split(QRegExp(","), QString::SkipEmptyParts);
//qDebug()<<"sections:"<<blockInfoList[1];
if(blockInfoList.length() > 24)
{
qint64 totalA = blockInfoList[20].toLongLong();
qint64 ltA = blockInfoList[22].toLongLong();
filemgr->setValue("Chg", "mutable", ltA);
filemgr->setValue("Chg", "total", totalA);
}
}
reply->deleteLater();
filemgr->deleteLater();
}
qDebug()<<"stk info update finished";
mgr->deleteLater();
#endif
}