forked from bnufree/Hq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
qetfscalethread.cpp
140 lines (129 loc) · 5.64 KB
/
qetfscalethread.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
#include "qetfscalethread.h"
#include "utils/qhttpget.h"
#include <QJsonDocument>
#include <QJsonValue>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonParseError>
#include "utils/profiles.h"
#include "utils/comdatadefines.h"
#include <QDir>
#include "date/shareworkingdatetime.h"
#define PROFILES_SEC "ETF"
#define PROFILES_KEY "UpdateDate"
QEtfScaleThread::QEtfScaleThread(QObject *parent) : QThread(parent)
{
qRegisterMetaType<QList<EtfScaleData>>("const QList<EtfScaleData>&");
PROFILES_INS->setDefault(PROFILES_SEC, PROFILES_KEY, QDate::currentDate().addDays(-30).toString("yyyy-MM-dd"));
QDir dir(HQ_ETF_DIR);
if(!dir.exists()) dir.mkpath(HQ_ETF_DIR);
}
QEtfScaleThread::~QEtfScaleThread()
{
qDebug()<<__FUNCTION__;
}
void QEtfScaleThread::run()
{
QDate last_update = QDate::fromString(PROFILES_INS->value(PROFILES_SEC, PROFILES_KEY).toString(), "yyyy-MM-dd").addDays(1);
while (last_update <= TradeDateMgr::instance()->currentTradeDay()) {
mEtfVolMap.clear();
parseShEtf(last_update);
if(mEtfVolMap.size() > 0)
{
parseSzEtf(last_update);
}
if(mEtfVolMap.size() > 0)
{
//write fo file
QString fileName = QString("%1/%2.txt").arg(HQ_ETF_DIR).arg(last_update.toString("yyyyMMdd"));
QFile file(fileName);
if(file.open(QIODevice::WriteOnly | QIODevice::Text))
{
foreach (EtfScaleData data, mEtfVolMap) {
QStringList list;
list.append(data.mCode);
list.append(data.mName);
list.append(data.mDate.toString("yyyy-MM-dd"));
list.append(QString::number(data.mScale, 'f', 2));
list.append("\n");
file.write(list.join(",").toUtf8());
}
file.close();
PROFILES_INS->setValue(PROFILES_SEC, PROFILES_KEY, last_update.toString("yyyy-MM-dd"));
}
}
last_update = last_update.addDays(1);
}
}
void QEtfScaleThread::parseShEtf(const QDate& date)
{
QList<QNetworkCookie> list;
list.append(QNetworkCookie("Referer", "http://www.sse.com.cn/market/funddata/volumn/etfvolumn/"));
list.append(QNetworkCookie("Cookie", "yfx_c_g_u_id_10000042=_ck20072115125217721926968350309; yfx_f_l_v_t_10000042=f_t_1595315572764__r_t_1595579117057__v_t_1595579117057__r_c_1; VISITED_MENU=%5B%228864%22%2C%228528%22%2C%2210025%22%2C%228547%22%2C%228491%22%5D"));
QByteArray recv = QHttpGet::getContentOfURL(QString("http://query.sse.com.cn/commonQuery.do?&jsonCallBack=&isPagination=true&sqlId=COMMON_SSE_ZQPZ_ETFZL_XXPL_ETFGM_SEARCH_L&pageHelp.pageSize=10000&STAT_DATE=%1&_=%2")
.arg(date.toString("yyyy-MM-dd")).arg(QDateTime::currentDateTime().toMSecsSinceEpoch()), list);
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(recv, &error);
if(!doc.isObject()) return;
QJsonArray dataArray = doc.object().value("pageHelp").toObject().value("data").toArray();
foreach (QJsonValue val, dataArray) {
QJsonObject obj = val.toObject();
EtfScaleData data;
data.mCode = obj.value("SEC_CODE").toString();
data.mName = obj.value("SEC_NAME").toString();
data.mScale = obj.value("TOT_VOL").toString().toDouble();
data.mDate = date;
mEtfVolMap[data.mName] = data;
}
}
void QEtfScaleThread::parseSzEtf(const QDate& date)
{
int page_num = 1;
int page_size = 10000;
int page_count = 1;
int total_record_count = 0;
while (page_num <= page_count) {
QString url = QString("http://www.szse.cn/api/report/ShowReport/data?SHOWTYPE=JSON&CATALOGID=1945&tab1PAGENO=%1&tab1PAGESIZE=%2&random=0.48925979447422674")
.arg(page_num)
.arg(page_size);
QByteArray recv = QHttpGet::getContentOfURL(url);
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(recv, &error);
if(!doc.isArray()) return;
QJsonArray array = doc.array();
QJsonObject metaObj = array[0].toObject().value("metadata").toObject();
if(metaObj.isEmpty()) break;
QDate curDate = QDate::fromString(metaObj.value("subname").toString().trimmed(), "yyyy-MM-dd");
if(!curDate.isValid()) break;
if(curDate != ShareTradeDateTime(date).nextTradeDay()) break;
page_count = metaObj.value("pagecount").toInt();
if(page_count <= 0) break;
total_record_count = metaObj.value("recordcount").toInt();
QJsonArray dataArray = array[0].toObject().value("data").toArray();
QRegExp codeExp("<u>(\\d{6})</u>");
QRegExp nameEXp("<u>(.{1,})</u>");
foreach (QJsonValue val, dataArray) {
QJsonObject obj = val.toObject();
QString code = obj.value("sys_key").toString();
int index = codeExp.indexIn(code);
if(index > 0)
{
code = codeExp.cap(1);
}
QString name = obj.value("kzjcurl").toString();
index = nameEXp.indexIn(name);
if(index > 0)
{
name = nameEXp.cap(1);
}
double vol = obj.value("dqgm").toString().remove(",").toDouble();
EtfScaleData data;
data.mCode = code;
data.mName = name;
data.mScale = vol;
data.mDate = date;
mEtfVolMap[data.mName] = data;
}
page_num++;
}
}