Browse Source

新增彭博接口

tuoling805 1 year ago
parent
commit
aef63b98cc
1 changed files with 72 additions and 0 deletions
  1. 72 0
      pb_api.py

+ 72 - 0
pb_api.py

@@ -0,0 +1,72 @@
+import hug
+from bottle import route, run
+import pdblp
+import json
+import pandas as pd
+
+hug.API(__name__).http.output_format = hug.output_format.json
+
+
+@hug.get('/hz_server')
+def hello():
+    return 1
+
+
+@hug.get('/edbInfo/pb')
+def GetEdbDataByPb(EdbCode, StartDate, EndDate):
+    print("GetEdbDataByPb")
+    bbg = pdblp.BCon(debug=True, port=8194)
+    bbg.start()
+    try:
+        rst = bbg.bdh(EdbCode,
+                      ['PX_LAST', 'PE_RATIO', 'PX_TO_BOOK_RATIO', 'EQY_DVD_YLD_12M', 'TURNOVER', 'CUR_MKT_CAP'],
+                      StartDate, EndDate, longdata=True).to_json()
+    except:
+        rst = pd.DataFrame().to_json()
+
+    print(rst)
+    print("pd data end")
+
+    # jsonArr = json.dumps(rst_list, ensure_ascii=False)
+    # result = json.loads(json_data)
+    bbg.stop()
+
+    result = json.loads(rst)
+    return result
+
+
+@hug.get('/edbInfo/pb/finance')
+def GetEdbDataByPbFinance(CompanyCode, EdbCode, StartDate, EndDate):
+    print("GetEdbDataByPb")
+    bbg = pdblp.BCon(debug=False, port=8194)
+    bbg.start()
+    try:
+        # rst = bbg.bdh(CompanyCode, EdbCode, StartDate, EndDate, longdata=True).to_json()
+        print("start get data")
+       # rst = bbg.bdh(CompanyCode, EdbCode, '20200701', '20221231', longdata=True)
+        rst = bbg.bdh(CompanyCode, EdbCode, StartDate, EndDate, longdata=True)
+        print(rst)
+        print("end get data")
+
+        print("pd_finance data end")
+
+        # jsonArr = json.dumps(rst_list, ensure_ascii=False)
+        # result = json.loads(json_data)
+        bbg.stop()
+
+        result = json.loads(rst.to_json())
+        return result
+    except  Exception as e:
+        exceptResult = pd.DataFrame().to_json()
+        print("start except")
+        print(e)
+        print(exceptResult)
+        print("end except")
+        bbg.stop()
+        return
+
+
+
+if __name__ == "__main__":
+    app = __hug__.http.server()
+    run(app=app, reloader=True,host='0.0.0.0', port=7001)