|
@@ -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
|
|
|
|
|
|
+
|
|
|
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 jdata
|
|
|
|
|
|
+
|
|
|
+ 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
|
|
|
+
|
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
@@ -43,22 +69,24 @@ server_port = 7110
|
|
|
ps_credential_path = "ps-credential.txt"
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-daily_sched_key = "IDpcsgDailyRun2"
|
|
|
-daily_data_dir = "data/day2"
|
|
|
+daily_sched_key = "IDpcsgDailyRun4"
|
|
|
+daily_data_dir = "data/day4"
|
|
|
+
|
|
|
+daily_run3_sched_key = "IDpcsgDailyRun3"
|
|
|
+daily_run3_data_dir = "data/day3"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
month_sched_key = "IDpcsgMonthRun2"
|
|
|
monthly_data_dir = "data/month2"
|
|
|
|
|
|
+
|
|
|
+daily_history_sched_key = "uNerkpV8A7FN"
|
|
|
+monthly_history_sched_key = "ugE7uE1HhtUj"
|
|
|
+
|
|
|
|
|
|
@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 = 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}
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -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}
|
|
|
+
|
|
|
+
|
|
|
|
|
|
def write_data_to_file(json_data, dir_path=None):
|
|
|
|