wind_api.py 3.0 KB

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