-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathproxy_flask.py
372 lines (279 loc) · 11.3 KB
/
proxy_flask.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/env python
from flask import Flask, request, Response, send_from_directory
import argparse
import os
import random
import sys
import requests
import re
import traceback
import pandas as pd
import json
from urllib.parse import urlparse
import tdxbk as tdxblock
import time
import threading
from pytdx.hq import TdxHq_API
#from sendrequest import handle_task
from hk_eastmoney import get_timeline,get_dayk,get_stock_price_bylist
tdxapi = TdxHq_API()
bridge = ["wss://bridge.duozhutuan.com/cacherelay", "wss://43.136.70.238/cacherelay"]
block_list = tdxblock.block_list
session = requests.Session()
from common.framework import init_stock_list, getstockinfo
code_list = {}
apihost="api.klang.org.cn/v2"
def get_finance(code):
tdxapi.connect('119.147.212.81', 7709)
zone,code = code[:2],code[2:]
if zone == "sh":
zone = 1
else:
zone = 0
ret = tdxapi.get_finance_info(zone, code)
return json.dumps(dict(ret))
def create_clickable_code(code):
code = code_list.get(code,code).replace('.','')
url_template= '''<a href="https://klang.org.cn/kline.html?code={code}" target="_blank"><font color="blue">{code}</font></a>'''.format(code=code)
return url_template
def create_color_hqltgz(hqltsz):
hqltsz = float(hqltsz.split('亿')[0])
if hqltsz >= 200.0:
url_template= '''<font color="#ef4136">{hqltsz}</font></a>'''.format(hqltsz=hqltsz)
else:
url_template = '''{hqltsz}'''.format(hqltsz=hqltsz)
return url_template
def create_color_rise(rise):
try:
rise = float(rise)
except:
rise = 0
if rise >= 0.0:
url_template= '''<font color="#ef4136">+{rise}</font></a>'''.format(rise=rise)
else:
url_template= '''<font color="#41ef36">{rise}</font></a>'''.format(rise=rise)
return url_template
# tdx 板块信息只有 个股code对应板块名
# 因此要获取code和股票名的 对应表
alllist = init_stock_list()
for i in alllist:
code, name, tdxbk, tdxgn = getstockinfo(i)
key = code.split('.')[1]
code_list[key] = code
hostname = 'http://data.10jqka.com.cn'
proxyhost = "https://api.klang.org.cn"
root_path = sys.path[0]
path_map = {}
def set_pathmap(path, target):
global path_map
path_map[path] = target
def get_pathmap(path):
for k in path_map.keys():
if re.search(k, path) is not None:
return path_map[k]
return hostname
before_call = {}
def set_before(path, callback):
global before_call
before_call[path] = callback
def call_before(headers, path):
for k in before_call.keys():
if re.search(k, path) is not None:
return before_call[k](headers)
return headers
after_call = {}
def set_after(path, callback):
global after_call
after_call[path] = callback
def call_after(resp, path):
for k in after_call.keys():
if re.search(k, path) is not None:
return after_call[k](resp)
return resp.content
hexin_v = ""
app = Flask(__name__)
file_paths = ['/gn.html', '/gncookie.html', '/zx.html', '/klinebk.html', '/bk.json', '/etf.html', '/kline.html']
# 定义根目录
root_path = os.path.dirname(os.path.abspath(__file__))
def browser_file(filename):
file_path = os.path.join(root_path, filename)
# 检查文件是否存在
if os.path.exists(file_path) and os.path.isfile(file_path):
return send_from_directory(root_path, filename)
else:
return "File not found", 404
@app.route('/', defaults={'path': ''}, methods=['GET', 'POST'])
@app.route('/<path:path>', methods=['GET', 'POST'])
def proxy(path):
if request.method == 'GET':
return handle_get_request(path)
elif request.method == 'POST':
return handle_post_request(path)
@app.route('/tick/<code>', methods=['GET', 'POST'])
def tickdata(code):
return get_timeline(code)
@app.route('/day/<code>', methods=['GET', 'POST'])
def daydata(code):
return get_dayk(code)
@app.route('/list/<codelist>', methods=['GET', 'POST'])
def listdata(codelist):
codelist = codelist.split(',')
data = get_stock_price_bylist(codelist)
formatted_json = json.dumps(data, ensure_ascii=False, indent=4)
# 使用 Response 对象返回 JSON 数据
return Response(formatted_json, content_type='application/json')
def handle_get_request(path):
headers = dict(request.headers)
if "/" + path in file_paths:
return browser_file(path)
query_params = request.args
# 构建完整的请求路径,包含查询参数
full_path = "/" + path
if query_params:
full_path += '?' + request.query_string.decode('utf-8')
if full_path == "":
return "Path not found", 404
headers = call_before(headers, full_path)
host = get_pathmap(full_path)
if host is None:
return "Path not found", 404
url = '{}/{}'.format(host, path)
headers['Host'] = host.split("//")[1]
headers['Referer'] = url
#print(url,headers,query_params)
resp = session.get(url, headers=headers,params=query_params)
resp_content = call_after(resp, full_path)
response = Response(resp_content,resp.status_code)
for key, value in resp.headers.items():
if key not in ['Content-Encoding', 'Transfer-Encoding', 'content-encoding', 'transfer-encoding', 'content-length',
'Content-Length']:
response.headers[key] = value
response.headers['Content-Length'] = len(resp_content)
try:
referer = request.headers.get('Referer', '')
scheme = urlparse(referer).scheme
netloc = urlparse(referer).netloc
# response.headers['Access-Control-Allow-Origin'] = scheme + "://" + netloc
response.headers['Access-Control-Allow-Origin'] = "*"
response.headers['Access-Control-Allow-Credentials'] = 'true'
except:
response.headers['Access-Control-Allow-Origin'] = "*"
response.headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
return response
def handle_post_request(path):
headers = dict(request.headers)
headers = call_before(headers, path)
target_url = get_pathmap(path)
if target_url is None:
return "Path not found", 404
data = request.get_data()
resp = session.post(target_url, headers=headers, data=data)
resp_content = call_after(resp, path)
response = Response(resp_content)
for key, value in resp.headers.items():
if key not in ['Content-Encoding', 'Transfer-Encoding', 'content-encoding', 'transfer-encoding', 'content-length',
'Content-Length']:
response.headers[key] = value
response.headers['Content-Length'] = len(resp_content)
try:
referer = request.headers.get('Referer', '')
scheme = urlparse(referer).scheme
netloc = urlparse(referer).netloc
# response.headers['Access-Control-Allow-Origin'] = scheme + "://" + netloc
response.headers['Access-Control-Allow-Origin'] = "*"
response.headers['Access-Control-Allow-Credentials'] = 'true'
except:
response.headers['Access-Control-Allow-Origin'] = "*"
response.headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
return response
def parse_args(argv=sys.argv[1:]):
parser = argparse.ArgumentParser(description='Proxy HTTP requests')
parser.add_argument('--port', dest='port', type=int, default=9998,
help='serve HTTP requests on specified port (default: random)')
args = parser.parse_args(argv)
return args
def main(argv=sys.argv[1:]):
args = parse_args(argv)
print('http server is starting on port {}...'.format(args.port))
app.run(host='0.0.0.0', port=args.port)
################################
#
# 需要定制修改的内容
#
################################
def config():
def modify_gn(resp):
path = request.path
print(path, "modify gn code")
content = re.sub("http://q.10jqka.com.cn/gn/detail/code/", proxyhost + "/gn/detail/code/", resp.text,
flags=re.I | re.S)
return content.encode('gbk')
def modify_gnzjl(resp):
path = request.path
print(path, "get gn 50 table ")
content = re.sub("http://q.10jqka.com.cn/gn/detail/code/", proxyhost + "/gn/detail/code/", resp.text,
flags=re.I | re.S)
content1 = re.findall("<table.*?table>", content, re.I | re.S)[0]
return content1.encode('gbk')
def modify_bkcode(resp):
path = request.path
if resp.status_code == 401:
return resp.text.replace("q.10jqka.com.cn",apihost)
print(path, request.headers)
if len(re.findall("<table.*?table>", resp.text, re.I | re.S)) < 1:
return resp.content
content = re.findall("<table.*?table>", resp.text, re.I | re.S)[0]
df = pd.read_html(content, converters={'代码': str}, index_col=0)[0]
df = df.drop(['涨跌', '涨速(%)', '换手(%)', '量比', '振幅(%)', '成交额', '流通股', '市盈率', ], axis=True)
df['流通市值'] = df['流通市值'].apply(create_color_hqltgz)
df['代码'] = df['代码'].apply(create_clickable_code)
df['涨跌幅(%)'] = df['涨跌幅(%)'].apply(create_color_rise)
pages = re.findall(r'page="(\d+)"', resp.text, re.I | re.S)
print(pages)
return df.to_html(escape=False, float_format='%.2f').encode('gbk') # resp.content
def modify_etf(resp):
path = request.path
print(path, "etf")
content = re.findall(r"g\(({.*})\)", resp.text, re.I | re.S)[0]
datas = []
content = json.loads(content).get('data').get('data')
for d in content.keys():
datas.append(content[d])
return json.dumps(datas).encode('gbk')
def modify_sina(reqHeader):
newHeader = reqHeader
headers = {
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
'sec-ch-ua-mobile': '?0',
'Sec-Fetch-Site': 'cross-site',
'Sec-Fetch-Mode': 'no-cors',
'Sec-Fetch-Dest': 'script',
'Referer': 'http://finance.sina.com.cn/realstock/company/sh000001/nc.shtml',
'Host': 'hq.sinajs.cn',
}
for k in headers:
newHeader[k] = headers[k]
return newHeader
def modify_before_gn(reqHeader):
newHeader = reqHeader
if len(hexin_v) > 0:
newHeader["hexin-v"] = hexin_v
return newHeader
set_after(r'/funds/gnzjl/field/tradezdf/order/desc/page/(\d+)/ajax/1/free/1/', modify_gn)
set_after(r'/funds/gnzjl/field/tradezdf/order/desc/ajax/(\d+)/free/1/', modify_gn)
set_after('/funds/gnzjl/$', modify_gnzjl)
set_pathmap('/gn/detail/code', 'https://q.10jqka.com.cn')
set_pathmap('/gn/detail/field/', 'https://q.10jqka.com.cn')
set_before('/gn/detail/field', modify_before_gn)
set_after('/gn/detail/field/', modify_bkcode)
set_pathmap('/data/Net/info/ETF_F009_desc_0_0_1_9999_0_0_0_jsonp_g.html', 'http://fund.ijijin.cn')
set_after('/data/Net/info/ETF_F009_desc_0_0_1_9999_0_0_0_jsonp_g.html', modify_etf)
set_pathmap('list=', 'https://hq.sinajs.cn')
set_before('list=', modify_sina)
set_pathmap('data/index.php', 'https://stock.gtimg.cn')
set_pathmap('cn/api/json_v2.php', 'https://quotes.sina.cn')
set_pathmap('s3/', 'https://smartbox.gtimg.cn')
set_pathmap('/realstock/company', 'https://finance.sina.com.cn')
if __name__ == '__main__':
config()
main()