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)
  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. StartDate, EndDate, longdata=True).to_json()
  19. except:
  20. rst = pd.DataFrame().to_json()
  21. print(rst)
  22. print("pd data end")
  23. # jsonArr = json.dumps(rst_list, ensure_ascii=False)
  24. # result = json.loads(json_data)
  25. bbg.stop()
  26. result = json.loads(rst)
  27. return result
  28. @hug.get('/edbInfo/pb/finance')
  29. def GetEdbDataByPbFinance(CompanyCode, EdbCode, StartDate, EndDate):
  30. print("GetEdbDataByPb")
  31. bbg = pdblp.BCon(debug=False, port=8194)
  32. bbg.start()
  33. try:
  34. # rst = bbg.bdh(CompanyCode, EdbCode, StartDate, EndDate, longdata=True).to_json()
  35. print("start get data")
  36. # rst = bbg.bdh(CompanyCode, EdbCode, '20200701', '20221231', longdata=True)
  37. rst = bbg.bdh(CompanyCode, EdbCode, StartDate, EndDate, longdata=True)
  38. print(rst)
  39. print("end get data")
  40. print("pd_finance data end")
  41. # jsonArr = json.dumps(rst_list, ensure_ascii=False)
  42. # result = json.loads(json_data)
  43. bbg.stop()
  44. result = json.loads(rst.to_json())
  45. return result
  46. except Exception as e:
  47. exceptResult = pd.DataFrame().to_json()
  48. print("start except")
  49. print(e)
  50. print(exceptResult)
  51. print("end except")
  52. bbg.stop()
  53. return
  54. if __name__ == "__main__":
  55. app = __hug__.http.server()
  56. run(app=app, reloader=True,host='0.0.0.0', port=7001)