wind_api.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. hug.API(__name__).http.output_format = hug.output_format.json
  10. @hug.get('/hz_server')
  11. def hello():
  12. return 1
  13. # return 'wind true'
  14. @hug.get('/edbInfo/wind')
  15. def GetEdbDataByWind(EdbCode, StartDate, EndDate):
  16. print("GetEdbDataByWind:", EdbCode)
  17. isConnected = w.isconnected()
  18. print("isconnected")
  19. print(isConnected)
  20. if isConnected == False:
  21. print("re isconnected")
  22. w.start()
  23. isConnected = w.isconnected()
  24. if isConnected == False:
  25. return "{'ErrMsg':'启动Wind接口失败'}"
  26. print("getdata")
  27. option = "Fill=Previous"
  28. data = w.edb(EdbCode, StartDate, EndDate, option)
  29. print("wind data")
  30. print(data)
  31. df = pd.DataFrame()
  32. if data.ErrorCode == -40521010: # Internet Timeout 超时退出
  33. os._exit(0)
  34. return "a"
  35. if data.ErrorCode == -40521009: # corrupted response. 错误响应也重启
  36. os._exit(0)
  37. return "b"
  38. df['DT'] = data.Times
  39. df['CLOSE'] = data.Data[0]
  40. df['ErrorCode'] = data.ErrorCode
  41. df = df[['DT', 'CLOSE', 'ErrorCode']]
  42. df = df.dropna()
  43. json_data = df.to_json()
  44. # w.stop()
  45. print("wind data end")
  46. result = json.loads(json_data)
  47. return result
  48. @hug.get('/edbInfo/wind/wsd')
  49. def GetEdbDataWindWsd(StockCode, EdbCode, StartDate, EndDate):
  50. print("GetEdbDataByWind:", EdbCode)
  51. isConnected = w.isconnected()
  52. print("isconnected")
  53. print(isConnected)
  54. if isConnected == False:
  55. print("re isconnected")
  56. w.start()
  57. isConnected = w.isconnected()
  58. if isConnected == False:
  59. return "{'ErrMsg':'启动Wind接口失败'}"
  60. option = "Fill=Previous"
  61. wsd_data = w.wsd(StockCode, EdbCode, StartDate, EndDate, option)
  62. if wsd_data.ErrorCode == -40521010: # Internet Timeout 超时退出
  63. os._exit(0)
  64. return "a"
  65. fm = pd.DataFrame(wsd_data.Data, index=wsd_data.Fields, columns=wsd_data.Times)
  66. json_data = fm.to_json()
  67. result = json.loads(json_data)
  68. return result
  69. if __name__ == "__main__":
  70. # wind 登录
  71. wStart = w.start()
  72. if wStart.ErrorCode != 0:
  73. print("启动万得API接口失败")
  74. print(wStart)
  75. app = __hug__.http.server()
  76. run(app=app, reloader=True, host='0.0.0.0', port=7000)