wind_api.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import hug
  2. from bottle import route, run, NORUN
  3. from WindPy import w
  4. import json
  5. import pandas as pd
  6. import os
  7. hug.API(__name__).http.output_format = hug.output_format.json
  8. @hug.get('/hz_server')
  9. def hello():
  10. return 1
  11. # return 'wind true'
  12. @hug.get('/edbInfo/wind')
  13. def GetEdbDataByWind(EdbCode, StartDate, EndDate):
  14. print("GetEdbDataByWind:", EdbCode)
  15. isConnected = w.isconnected()
  16. print("isconnected")
  17. print(isConnected)
  18. if isConnected == False:
  19. print("re isconnected")
  20. w.start()
  21. isConnected = w.isconnected()
  22. if isConnected == False:
  23. return "{'ErrMsg':'启动Wind接口失败'}"
  24. print("getdata")
  25. option = "Fill=Previous"
  26. data = w.edb(EdbCode, StartDate, EndDate, option)
  27. print("wind data")
  28. print(data)
  29. df = pd.DataFrame()
  30. if data.ErrorCode != 0 and data.ErrorCode != -40522017: # corrupted response. 如果不是提示超限,同时也不是正常返回,那么就退出
  31. os._exit(0)
  32. return "c"
  33. df['DT'] = data.Times
  34. df['CLOSE'] = data.Data[0]
  35. df['ErrorCode'] = data.ErrorCode
  36. df = df[['DT', 'CLOSE', 'ErrorCode']]
  37. df = df.dropna()
  38. json_data = df.to_json()
  39. # w.stop()
  40. print("wind data end")
  41. result = json.loads(json_data)
  42. return result
  43. @hug.get('/edbInfo/wind/wsd')
  44. def GetEdbDataWindWsd(StockCode, EdbCode, StartDate, EndDate, Days="", Period="", PriceAdj=""):
  45. print("GetEdbDataByWind:", EdbCode)
  46. isConnected = w.isconnected()
  47. print("isconnected")
  48. print(isConnected)
  49. if isConnected == False:
  50. print("re isconnected")
  51. w.start()
  52. isConnected = w.isconnected()
  53. if isConnected == False:
  54. return "{'ErrMsg':'启动Wind接口失败'}"
  55. options = ["Fill=Previous"]
  56. if Days:
  57. options.append(f"Days={Days}")
  58. if Period:
  59. options.append(f"Period={Period}")
  60. if PriceAdj:
  61. options.append(f"PriceAdj={PriceAdj}")
  62. option = ";".join(options)
  63. print(option)
  64. wsd_data = w.wsd(StockCode, EdbCode, StartDate, EndDate, option)
  65. if wsd_data.ErrorCode != 0: # Internet Timeout 超时退出
  66. os._exit(0)
  67. return "a"
  68. fm = pd.DataFrame(wsd_data.Data, index=wsd_data.Fields, columns=wsd_data.Times)
  69. json_data = fm.to_json()
  70. result = json.loads(json_data)
  71. return result
  72. # def check_wind_status():
  73. # print("check wind login status")
  74. # # wind 登录
  75. # wStart = w.start()
  76. # print(wStart.ErrorCode)
  77. # if wStart.ErrorCode != 0:
  78. # print("启动万得API接口失败")
  79. # os._exit(0)
  80. # scheduler = BackgroundScheduler()
  81. # scheduler.add_job(check_wind_status, 'interval', seconds=1) # 每60秒执行一次任务
  82. # scheduler.start()
  83. # try:
  84. # while True:
  85. # time.sleep(2)
  86. # except (KeyboardInterrupt, SystemExit):
  87. # scheduler.shutdown()
  88. if __name__ == "__main__":
  89. # wind 登录
  90. wStart = w.start(waitTime=60)
  91. print(wStart)
  92. if wStart.ErrorCode != 0:
  93. print("启动万得API接口失败")
  94. os._exit(0)
  95. app = __hug__.http.server()
  96. run(app=app, reloader=True, host='0.0.0.0', port=7000)