-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwindy_utils_rest.py
194 lines (169 loc) · 7.18 KB
/
windy_utils_rest.py
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
# -*- coding: utf-8 -*-
"""
Created on 2016-12-22
@author: MG
"""
import pandas as pd
import requests
import json
from datetime import datetime, date
STR_FORMAT_DATE = '%Y-%m-%d'
STR_FORMAT_DATETIME_WIND = '%Y-%m-%d %H:%M:%S' # 2017-03-06 00:00:00.005000
UN_AVAILABLE_DATETIME = datetime.strptime('1900-01-01', STR_FORMAT_DATE)
UN_AVAILABLE_DATE = UN_AVAILABLE_DATETIME.date()
def format_2_date_str(dt) -> str:
if dt is None:
return None
dt_type = type(dt)
if dt_type == str:
return dt
elif dt_type == date:
if dt > UN_AVAILABLE_DATE:
return dt.strftime(STR_FORMAT_DATE)
else:
return None
elif dt_type == datetime:
if dt > UN_AVAILABLE_DATETIME:
return dt.strftime(STR_FORMAT_DATE)
else:
return None
else:
return dt
def format_2_datetime_str(dt) -> str:
if dt is None:
return None
dt_type = type(dt)
if dt_type == str:
return dt
elif dt_type == date:
if dt > UN_AVAILABLE_DATE:
return dt.strftime(STR_FORMAT_DATE)
else:
return None
elif dt_type == datetime:
if dt > UN_AVAILABLE_DATETIME:
return dt.strftime(STR_FORMAT_DATETIME_WIND)
else:
return None
else:
return dt
class APIError(Exception):
def __init__(self, status, ret_dic):
self.status = status
self.ret_dic = ret_dic
def __str__(self):
return "APIError:status=POST / {} {}".format(self.status, self.ret_dic)
class WindRest:
def __init__(self, url_str):
self.url = url_str
self.header = {'Content-Type': 'application/json'}
def _url(self, path: str) -> str:
return self.url + path
def public_post(self, path: str, req_data: str) -> list:
# print('self._url(path):', self._url(path))
ret_data = requests.post(self._url(path), data=req_data, headers=self.header)
ret_dic = ret_data.json()
if ret_data.status_code != 200:
raise APIError(ret_data.status_code, ret_dic)
else:
return ret_data.status_code, ret_dic
def wset(self, table_name, options) -> pd.DataFrame:
path = 'wset/'
req_data_dic = {"table_name": table_name, "options": options}
req_data = json.dumps(req_data_dic)
_, json_dic = self.public_post(path, req_data)
df = pd.DataFrame(json_dic).T
return df
def wss(self, codes, fields, options="") -> pd.DataFrame:
path = 'wss/'
req_data_dic = {"codes": codes, "fields": fields, "options": options}
req_data = json.dumps(req_data_dic)
_, json_dic = self.public_post(path, req_data)
df = pd.DataFrame(json_dic).T
return df
def wsd(self, codes, fields, begin_time, end_time, options="") -> pd.DataFrame:
path = 'wsd/'
req_data_dic = {"codes": codes, "fields": fields,
"begin_time": format_2_date_str(begin_time),
"end_time": format_2_date_str(end_time),
"options": options}
req_data = json.dumps(req_data_dic)
_, json_dic = self.public_post(path, req_data)
df = pd.DataFrame(json_dic).T
return df
def wsi(self, codes, fields, begin_time, end_time, options="") -> pd.DataFrame:
path = 'wsi/'
req_data_dic = {"codes": codes, "fields": fields,
"begin_time": format_2_date_str(begin_time),
"end_time": format_2_date_str(end_time),
"options": options}
req_data = json.dumps(req_data_dic)
_, json_dic = self.public_post(path, req_data)
df = pd.DataFrame(json_dic).T
return df
def wst(self, codes, fields, begin_time, end_time, options="") -> pd.DataFrame:
path = 'wst/'
req_data_dic = {"codes": codes, "fields": fields,
"begin_time": format_2_datetime_str(begin_time),
"end_time": format_2_datetime_str(end_time),
"options": options}
req_data = json.dumps(req_data_dic)
_, json_dic = self.public_post(path, req_data)
df = pd.DataFrame(json_dic).T
return df
def wsq(self, codes, fields, options="") -> pd.DataFrame:
path = 'wsq/'
req_data_dic = {"codes": codes, "fields": fields, "options": options}
req_data = json.dumps(req_data_dic)
_, json_dic = self.public_post(path, req_data)
df = pd.DataFrame(json_dic).T
return df
def tdaysoffset(self, offset, begin_time, options="") -> dict:
path = 'tdaysoffset/'
req_data_dic = {"offset": offset,
"begin_time": format_2_date_str(begin_time),
"options": options}
req_data = json.dumps(req_data_dic)
_, json_dic = self.public_post(path, req_data)
date_str = json_dic['Date']
return date_str
def tdays(self, begin_time, end_time, options="") -> dict:
path = 'tdays/'
req_data_dic = {"begin_time": format_2_date_str(begin_time),
"end_time": format_2_date_str(end_time),
"options": options}
req_data = json.dumps(req_data_dic)
_, json_dic = self.public_post(path, req_data)
# df = pd.DataFrame(json_dic)
return json_dic
def edb(self, codes, begin_time, end_time, options) -> pd.DataFrame:
path = 'edb/'
req_data_dic = {"codes": codes,
"begin_time": format_2_date_str(begin_time),
"end_time": format_2_date_str(end_time),
"options": options}
req_data = json.dumps(req_data_dic)
_, json_dic = self.public_post(path, req_data)
df = pd.DataFrame(json_dic).T
return df
if __name__ == "__main__":
# url_str = "http://10.0.5.65:5000/wind/"
url_str = "http://10.0.3.78:5000/wind/"
rest = WindRest(url_str)
# data_df = rest.wset(table_name="sectorconstituent", options="date=2017-03-21;sectorid=1000023121000000")
# data_df = rest.wss(codes="QHZG160525.OF", fields="fund_setupdate,fund_maturitydate,fund_mgrcomp,fund_existingyear,fund_ptmyear,fund_type,fund_fundmanager")
# data_df = rest.wsd("601398.SH", "open,high,low,close,volume", "2017-01-04", "2017-02-28", "PriceAdj=F")
# data_df = rest.tdays(begin_time="2017-01-04", end_time="2017-02-28")
# data_df = rest.wst("600000.SH", "ask1,bid1,asize1,bsize1,volume,amt,pre_close,open,high,low,last", "2017-10-20 09:15:00", "2017-10-20 09:26:00", "")
# data_df = rest.wsi("RU1801.SHF", "open,high,low,close,volume,amt,oi", "2017-12-8 09:00:00", "2017-12-8 11:30:00", "")
try:
# w.wsd("601398.SH", "open,high,low,close,volume", "2017-09-29", "2017-10-28", "")
data_df = rest.wsi("AU1801.SHF", "open,high,low,close,volume,amt,oi", "2017-12-7 2:25:00", "2017-12-8 9:05:00", "")
print(data_df)
except APIError as exp:
if exp.status == 500:
print('APIError.status:', exp.status, exp.ret_dic['message'])
else:
print(exp.ret_dic.setdefault('error_code', ''), exp.ret_dic['message'])
# date_str = rest.tdaysoffset(1, '2017-3-31')
# print(date_str)