pb_api.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import hug
  2. from bottle import route, run
  3. import pdblp
  4. import json
  5. import pandas as pd
  6. hug.API(__name__).http.output_format = hug.output_format.json
  7. @hug.get('/hz_server')
  8. def hello():
  9. return 1
  10. @hug.get('/edbInfo/pb')
  11. def GetEdbDataByPb(EdbCode, StartDate, EndDate):
  12. print("GetEdbDataByPb")
  13. bbg = pdblp.BCon(debug=True, port=8194, timeout=10000)
  14. bbg.start()
  15. try:
  16. rst = bbg.bdh(EdbCode,
  17. # ['PX_LAST', 'PE_RATIO', 'PX_TO_BOOK_RATIO', 'EQY_DVD_YLD_12M', 'TURNOVER', 'CUR_MKT_CAP'],
  18. ['PX_LAST'],
  19. StartDate, EndDate, longdata=True).to_json()
  20. except:
  21. rst = pd.DataFrame().to_json()
  22. print(rst)
  23. print("pd data end")
  24. # jsonArr = json.dumps(rst_list, ensure_ascii=False)
  25. # result = json.loads(json_data)
  26. bbg.stop()
  27. result = json.loads(rst)
  28. return result
  29. @hug.get('/edbInfo/pb/finance')
  30. def GetEdbDataByPbFinance(CompanyCode, EdbCode, StartDate, EndDate):
  31. print("GetEdbDataByPb")
  32. bbg = pdblp.BCon(debug=False, port=8194)
  33. bbg.start()
  34. try:
  35. # rst = bbg.bdh(CompanyCode, EdbCode, StartDate, EndDate, longdata=True).to_json()
  36. print("start get data")
  37. # rst = bbg.bdh(CompanyCode, EdbCode, '20200701', '20221231', longdata=True)
  38. rst = bbg.bdh(CompanyCode, EdbCode, StartDate, EndDate, longdata=True)
  39. print(rst)
  40. print("end get data")
  41. print("pd_finance data end")
  42. # jsonArr = json.dumps(rst_list, ensure_ascii=False)
  43. # result = json.loads(json_data)
  44. bbg.stop()
  45. result = json.loads(rst.to_json())
  46. return result
  47. except Exception as e:
  48. exceptResult = pd.DataFrame().to_json()
  49. print("start except")
  50. print(e)
  51. print(exceptResult)
  52. print("end except")
  53. bbg.stop()
  54. return
  55. if __name__ == "__main__":
  56. app = __hug__.http.server()
  57. run(app=app, reloader=True, host='0.0.0.0', port=7001)