Browse Source

PCSG彭博

hsun 8 months ago
parent
commit
216ea65e79
1 changed files with 75 additions and 25 deletions
  1. 75 25
      pscg_bloomberg/main.py

+ 75 - 25
pscg_bloomberg/main.py

@@ -3,13 +3,16 @@ from oauthlib.oauth2 import BackendApplicationClient
 from requests_oauthlib import OAuth2Session
 from dataclasses import dataclass
 from flask import Flask
+from flask import request
 from datetime import datetime
+import pandas as pd
 
 
 @dataclass
 class bbg_schedule_run:
     credentail_file: str
 
+    # 获取BBG现值数据
     def get_sched_data(self, identifier):
         with io.open(self.credentail_file, encoding="utf-8") as credential_file:
             CREDENTIALS = json.load(credential_file)
@@ -32,6 +35,29 @@ class bbg_schedule_run:
         # return (pd.json_normalize(jdata).to_json())
         return jdata
 
+    # 获取BBG历史数据文件
+    def get_sched_history_file(self, identifier, file_name):
+        with io.open(self.credentail_file, encoding="utf-8") as credential_file:
+            CREDENTIALS = json.load(credential_file)
+
+        CLIENT = BackendApplicationClient(client_id=CREDENTIALS['client_id'])
+
+        OAUTH2_ENDPOINT = 'https://bsso.blpprofessional.com/ext/api/as/token.oauth2'
+        SESSION = OAuth2Session(client=CLIENT)
+        SESSION.fetch_token(token_url=OAUTH2_ENDPOINT, client_secret=CREDENTIALS['client_secret'])
+
+        URL = 'https://api.bloomberg.com/eap/catalogs/47929/content/responses/?requestIdentifier=' + identifier
+
+        rep = SESSION.get(URL, headers={'api-version': "2"})
+
+        DATA_URL = 'https://api.bloomberg.com/eap/catalogs/47929/content/responses/' + rep.json()['contains'][0]['key']
+
+        jdata = SESSION.get(DATA_URL, headers={'api-version': "2"}).json()
+        df = pd.json_normalize(jdata)
+        df.to_csv(file_name, index=False)
+
+        return True
+
 
 # hug接口输出为json格式
 app = Flask(__name__)
@@ -43,22 +69,24 @@ server_port = 7110
 ps_credential_path = "ps-credential.txt"  # 鉴权文件地址
 
 # 日度
-# daily_price_sched_key = "IDpcsgDailyType6Price"  # 日度Price任务key(第一版)
-# daily_general_sched_key = "IDpcsgDailyGeneral"  # 日度general任务key(第一版)
-# daily_data_price_dir = "data/day/price"  # 日度Price数据文件夹
-# daily_data_general_dir = "data/day/general"  # 日度Price数据文件夹
-daily_sched_key = "IDpcsgDailyRun2"  # 日度任务key
-daily_data_dir = "data/day2"  # 日度数据文件夹
+daily_sched_key = "IDpcsgDailyRun4"  # 日度任务key
+daily_data_dir = "data/day4"  # 日度数据文件夹
+
+daily_run3_sched_key = "IDpcsgDailyRun3"
+daily_run3_data_dir = "data/day3"
 
 # 周度
 # weekly_sched_key = "IDpcsgWeeklyRunETA223"  # 周度任务key(暂停)
 # weekly_data_dir = "data/week"
 
 # 月度
-# month_sched_key = "IDpcsgMonthRun"  # 月度任务key(第一版)
 month_sched_key = "IDpcsgMonthRun2"  # 月度任务key
 monthly_data_dir = "data/month2"  # 月度数据文件夹
 
+# 历史数据
+daily_history_sched_key = "uNerkpV8A7FN"  # 日度历史数据任务key
+monthly_history_sched_key = "ugE7uE1HhtUj"  # 月度历史数据任务key
+
 
 @app.route('/api/bloomberg/server')
 def bloomberg_server():
@@ -68,24 +96,6 @@ def bloomberg_server():
 @app.route('/api/bloomberg/daily_data', methods=['POST'])
 def get_bloomberg_daily_data():
     try:
-        # bbg_down = bbg_schedule_run(ps_credential_path)
-        # df_daily_price = bbg_down.get_sched_data(daily_price_sched_key)
-        # df_daily_general = bbg_down.get_sched_data(daily_general_sched_key)
-        #
-        # # 写入文件留档
-        # write_data_to_file(df_daily_price, daily_data_price_dir)
-        # write_data_to_file(df_daily_general, daily_data_general_dir)
-        #
-        # resp_data = {
-        #     "code": 200,
-        #     "data": {
-        #         "price_data": df_daily_price,
-        #         "general_data": df_daily_general
-        #     },
-        #     "msg": "获取成功"
-        # }
-        # return json.dumps(resp_data, indent=2)
-
         bbg_down = bbg_schedule_run(ps_credential_path)
         df_daily = bbg_down.get_sched_data(daily_sched_key)
 
@@ -104,6 +114,27 @@ def get_bloomberg_daily_data():
         return {"code": 403, "data": "", "error": err_msg}
 
 
+@app.route('/api/bloomberg/daily_data_run3', methods=['POST'])
+def get_bloomberg_daily_data_run3():
+    try:
+        bbg_down = bbg_schedule_run(ps_credential_path)
+        df_daily = bbg_down.get_sched_data(daily_run3_sched_key)
+
+        # 写入文件留档
+        write_data_to_file(df_daily, daily_run3_data_dir)
+
+        resp_data = {
+            "code": 200,
+            "data": df_daily,
+            "msg": "获取成功"
+        }
+        return json.dumps(resp_data, indent=2)
+    except Exception as e:
+        err_msg = str(e)
+        print(err_msg)
+        return {"code": 403, "data": "", "error": err_msg}
+
+
 # 周度任务暂关闭
 # @app.route('/api/bloomberg/weekly_data', methods=['POST'])
 # def get_bloomberg_weekly_data():
@@ -147,6 +178,25 @@ def get_bloomberg_monthly_data():
         return {"code": 403, "data": "", "error": err_msg}
 
 
+@app.route('/api/bloomberg/down', methods=['GET'])
+def bloomberg_test():
+    task_key = request.args.get('task_key', default="", type=str)
+    if task_key is None or task_key == "":
+        return {"code": 403, "data": "", "error": "task key is none"}
+
+    try:
+        bbg_down = bbg_schedule_run(ps_credential_path)
+        today_date_str = datetime.now().strftime('%Y%m%d')
+        file_name = f"dailyHistory-{task_key}-{today_date_str}.csv"
+        bbg_down.get_sched_history_file(task_key, file_name)
+
+        return {"code": 200, "data": "", "error": ""}
+    except Exception as e:
+        err_msg = str(e)
+        print(err_msg)
+        return {"code": 403, "data": "", "error": err_msg}
+
+
 # write_data_to_file 数据写入文件
 def write_data_to_file(json_data, dir_path=None):
     # 获取当前日期并格式化为YYYY-MM-DD形式