-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJQBlacklist.py
More file actions
332 lines (289 loc) · 12 KB
/
JQBlacklist.py
File metadata and controls
332 lines (289 loc) · 12 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
'''
Created on Dec 9, 2016
@Author: Cyanife
'''
import tkinter as tk
from tkinter import filedialog
import tkinter.ttk as ttk
import tkinter.font as tkfont
import json
import re
import webbrowser
import asyncio
import aiohttp
import yarl
class StockDuplicateException(Exception):
def __init__(self, stock):
Exception.__init__(self)
self.stock = stock
class StockEmptyException(Exception):
def __init__(self, stock):
Exception.__init__(self)
self.stock = stock
class BlacklistData(object):
'''
read blacklist.py and edit
'''
grep_detail = re.compile(r'(\w{2}\d+)=([^\s][^,]+?)%s%s' % (r',([\.\d]+)' * 29, r',([-\.\d:]+)' * 2))
stock_api = 'http://hq.sinajs.cn/?format=text&list='
def __init__(self):
self._stocklist = []
self._stockdict = {}
self._path = ''
self._haschanged = False
def load(self, path):
self._path = path
with open(path,'r') as f:
stocklist = json.load(f)
#Convert symbol format
self._stocklist = []
for sym in stocklist:
sym = re.sub(r'(\d+).XSHG', 'sh\g<1>', sym)
sym = re.sub(r'(\d+).XSHE', 'sz\g<1>', sym)
self._stocklist.append(sym)
self.updatestockdict()
def save(self):
#update stocklist from stockdict
self.updatestocklist()
#Convert symbol format to JQ
stocklist = []
for sym in self._stocklist:
sym = re.sub(r'sh(\d+)', '\g<1>.XSHG', sym)
sym = re.sub(r'sz(\d+)', '\g<1>.XSHE', sym)
stocklist.append(sym)
#write json
with open(self._path,'w') as f:
json.dump(stocklist,f)
@property
def stockdict(self):
return self._stockdict
def appendstock(self, symbol):
if symbol in self._stockdict:
raise StockDuplicateException(symbol)
else:
self._stockdict.update(self.getstockinfo([symbol]))
self.updatestocklist()
def removestock(self,symbol):
if symbol not in self._stockdict:
raise StockEmptyException(symbol)
else:
del self._stockdict[symbol]
self.updatestocklist()
def updatestocklist(self):
self._stocklist = list(self._stockdict.keys())
def updatestockdict(self):
self._stockdict = self.getstockinfo(self._stocklist)
def getstockinfo(self,stocklist):
self._session = aiohttp.ClientSession()
coroutines = []
for symbol in stocklist:
coroutine = self.gatherstockinfo(symbol)
coroutines.append(coroutine)
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
res = loop.run_until_complete(asyncio.gather(*coroutines))
self._session.close()
return self.stockinfoformatter(res)
async def gatherstockinfo(self,symbol):
headers = {
'Accept-Encoding': 'gzip, deflate, sdch',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.100 Safari/537.36'
}
url = yarl.URL(self.stock_api + symbol, encoded=True)
try:
async with self._session.get(url, timeout=10, headers=headers) as r:
response_text = await r.text()
return response_text
except asyncio.TimeoutError:
return None
def stockinfoformatter(self,rep_data):
stocks_detail = ''.join(rep_data)
grep_str = self.grep_detail
result = grep_str.finditer(stocks_detail)
stock_dict = dict()
for stock_match_object in result:
stock = stock_match_object.groups()
stock_dict[stock[0]] = dict(
name=stock[1],
open=float(stock[2]),
close=float(stock[3]),
now=float(stock[4]),
high=float(stock[5]),
low=float(stock[6]),
buy=float(stock[7]),
sell=float(stock[8]),
turnover=int(stock[9]),
volume=float(stock[10]),
bid1_volume=int(stock[11]),
bid1=float(stock[12]),
bid2_volume=int(stock[13]),
bid2=float(stock[14]),
bid3_volume=int(stock[15]),
bid3=float(stock[16]),
bid4_volume=int(stock[17]),
bid4=float(stock[18]),
bid5_volume=int(stock[19]),
bid5=float(stock[20]),
ask1_volume=int(stock[21]),
ask1=float(stock[22]),
ask2_volume=int(stock[23]),
ask2=float(stock[24]),
ask3_volume=int(stock[25]),
ask3=float(stock[26]),
ask4_volume=int(stock[27]),
ask4=float(stock[28]),
ask5_volume=int(stock[29]),
ask5=float(stock[30]),
date=stock[31],
time=stock[32],
)
return stock_dict
class BlacklistEditor(tk.Frame):
'''
GUI
'''
xqURL = 'https://xueqiu.com/S/'
def __init__(self, parent):
'''
Constructor
'''
#Inner settings
self._header = ['name','price']
self._filepath = 'blacklist.json'
tk.Frame.__init__(self, parent)
self.parent = parent
self.initialize_user_interface()
self._haschanged = False
def initialize_user_interface(self):
'''
Use treeview
'''
self.parent.title("JQBlacklist")
self.parent.grid_rowconfigure(2,weight=1)#窗口缩放时行列权重
self.parent.grid_columnconfigure(0,weight=1)
self.parent.config(background="lavender")
#Set GUI widgets
self.load_button = tk.Button(self.parent, text = 'Open',width = 10, command = self.load_blacklist)
self.load_button.grid(row = 0, column = 0, sticky = tk.W)
self.save_button = tk.Button(self.parent, text = 'Save', width = 10, command = self.save_blacklist)
self.save_button.grid(row = 1, column = 0, sticky = tk.W)
self.insert_button = tk.Button(self.parent, text = 'Insert', width = 10, command = self.insert_stock)
self.insert_button.grid(row = 0, column = 1, sticky = tk.W)
self.delete_button = tk.Button(self.parent, text = 'Delete', width = 10, command = self.delete_stock)
self.delete_button.grid(row = 0, column = 2, sticky = tk.E)
self.stock_entry = tk.Entry(self.parent,width = 25)
self.stock_entry.grid(row = 1, column=1,columnspan=2)
#Set the treeview
# self.tree = ttk.Treeview(self.parent, columns=self._header,show="headings")
self.tree = ttk.Treeview(self.parent, columns=self._header)
self.vsb = ttk.Scrollbar(orient="vertical",
command=self.tree.yview)
self.hsb = ttk.Scrollbar(orient="horizontal",
command=self.tree.xview)
self.tree.configure(yscrollcommand=self.vsb.set,
xscrollcommand=self.hsb.set)
self.tree.grid(row = 2, columnspan = 3, sticky = 'nsew')
self.vsb.grid(column=3, row=2, sticky='ns')
self.hsb.grid(column=0, row=3,columnspan=3,sticky='ew')
#bind treeview events
self.tree.bind("<<TreeviewSelect>>", self.stockclick)
self.tree.bind("<Double-1>", self.OnDoubleClick)
def OnDoubleClick(self, event):
item = self.tree.identify('item', event.x,event.y)
symbol = self.tree.item(item,'text')
webbrowser.open(self.xqURL+symbol, new=2)
def stockclick(self, event):
currentitems = self.tree.selection()
# print(type(currentitems))
self.stock_entry.delete(0,'end')
if len(currentitems) == 1:
self.stock_entry.insert('end', self.tree.item(currentitems[0], 'text'))
def load_blacklist(self):
ftypes = [('Blacklist files', '*.json'), ('All files', '*')]
opendlg = filedialog.Open(self, filetypes = ftypes)
fload = opendlg.show()
if fload != '':
self._filepath = fload
self._blacklistdata = BlacklistData()
self._blacklistdata.load(self._filepath)
# print(self._blacklistdata.stockdict)
self._build_tree()
def _build_tree(self):
self.tree.heading('#0', text = 'symbol', command = lambda:self.sortsymbol(self.tree,0))
self.tree.column('#0', stretch=tk.YES)
for col in self._header:
self.tree.heading(col, text = col.title(), command = lambda c=col: self.sortby(self.tree,c,0))
#adjust the column's width
# self.tree.column(col, width=tkfont.Font().measure(col.title()))
for symbol, info in self._blacklistdata.stockdict.items():
item = (info['name'],info['now'])
self.tree.insert('','end',text=symbol,values= item)
# adjust column's width
# for ix, val in enumerate(item):
# col_w = tkfont.Font().measure(val)
# if self.tree.column(self._header[ix],width = None) <col_w:
# self.tree.column(self._header[ix], width = col_w)
def sortby(self, tree, col, descending):
"""sort tree contents when a column header is clicked on"""
# grab values to sort
data = [(tree.set(child, col), child) for child in tree.get_children('')]
# now sort the data in place
data.sort(reverse=descending)
for ix, item in enumerate(data):
tree.move(item[1], '', ix)
# switch the heading so it will sort in the opposite direction
tree.heading(col, command=lambda col=col: self.sortby(tree, col, int(not descending)))
def sortsymbol(self, tree, descending):
"""sort tree contents when a column header is clicked on"""
# grab values to sort
data = [(tree.item(child, 'text')[2:], child) for child in tree.get_children()]
# now sort the data in place
data.sort(reverse=descending)
for ix, item in enumerate(data):
tree.move(item[1], '', ix)
# switch the heading so it will sort in the opposite direction
tree.heading('#0', command = lambda: self.sortsymbol(tree, int(not descending)))
def save_blacklist(self):
if self._haschanged:
self._blacklistdata.save()
self._haschanged = False
def insert_stock(self):
symbol = self.stock_entry.get()
try:
self._blacklistdata.appendstock(symbol)
info = self._blacklistdata.stockdict[symbol]
item = (info['name'],info['now'])
self.tree.insert('','end',text=symbol,values= item)
except StockDuplicateException:
pass
self._haschanged = True
def delete_stock(self):
symbol = self.stock_entry.get()
if symbol:
try:
children = self.tree.get_children()
for chd in children:
if self.tree.item(chd,'text') == symbol:
self.tree.delete(chd)
self._blacklistdata.removestock(symbol)
except StockEmptyException:
pass
else:
currentitems = self.tree.selection()
for item in currentitems:
try:
symbol = self.tree.item(item, 'text')
self.tree.delete(item)
self._blacklistdata.removestock(symbol)
except StockEmptyException:
pass
self._haschanged = True
def main():
root=tk.Tk()
Editor=BlacklistEditor(root)
root.mainloop()
if __name__ == "__main__":
main()