wind_api.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. df['DT'] = data.Times
  36. df['CLOSE'] = data.Data[0]
  37. df['ErrorCode'] = data.ErrorCode
  38. df = df[['DT', 'CLOSE', 'ErrorCode']]
  39. df = df.dropna()
  40. json_data = df.to_json()
  41. # w.stop()
  42. print("wind data end")
  43. result = json.loads(json_data)
  44. return result
  45. @hug.get('/edbInfo/wind/wsd')
  46. def GetEdbDataWindWsd(StockCode,EdbCode, StartDate, EndDate):
  47. print("GetEdbDataByWind:", EdbCode)
  48. isConnected = w.isconnected()
  49. print("isconnected")
  50. print(isConnected)
  51. if isConnected == False:
  52. print("re isconnected")
  53. w.start()
  54. isConnected = w.isconnected()
  55. if isConnected == False:
  56. return "{'ErrMsg':'启动Wind接口失败'}"
  57. option = "Fill=Previous"
  58. wsd_data = w.wsd(StockCode,EdbCode, StartDate, EndDate, option)
  59. if wsd_data.ErrorCode == -40521010: # Internet Timeout 超时退出
  60. os._exit(0)
  61. return "a"
  62. fm=pd.DataFrame(wsd_data.Data,index=wsd_data.Fields,columns=wsd_data.Times)
  63. json_data=fm.to_json()
  64. result = json.loads(json_data)
  65. return result
  66. if __name__ == "__main__":
  67. # wind 登录
  68. wStart = w.start()
  69. if wStart.ErrorCode != 0:
  70. print("启动万得API接口失败")
  71. print(wStart)
  72. app = __hug__.http.server()
  73. run(app=app, reloader=True,host='0.0.0.0', port=7000)