def get_daily_data(ts_code): table_name = daily_data_table_name(ts_code) if table_exists(table_name): df = read_df_from_db(table_name, index_col='date') df.index = pd.to_datetime(df.index) return df else: return None
def get_hist_data(code): ''' 获取数据,并且存在数据库中 ''' table_name = "%s_hist" % (code) if table_exists(table_name): return read_df_from_db(table_name, index_col='date') else: df = ts.get_hist_data(code) df_to_db(table_name, df) return df
def get_daily_start_date(ts_code): table_name = daily_data_table_name(ts_code) if table_exists(table_name): old_df = get_daily_data(ts_code) if old_df is None or len(old_df) == 0: start_date = daily_start else: old_df = old_df.sort_index(ascending=True) start_date = (convert_date(old_df.index[-1]) + timedelta(days=1)).strftime("%Y%m%d") else: start_date = daily_start return start_date
def initialize_db(): """bootstrap_endpoint Bootstraps the database using the models.initialize_db() method. Returns: dict, 201 response code: success dict, 500 response code: failure """ secret_key = request.get_json() if secret_key["key"] == SECRET: models.initialize_db() exists = models.db_exists(models.DB_NAME) routes_table_exists = models.table_exists("routes") route_lengths_exists = models.table_exists("route_lengths") assert exists == routes_table_exists == route_lengths_exists APP.logger.info("PostGres DB with tables is online.") return ( json.dumps({"Success!": "PostGres DB with postgis extensions is created."}), 201, ) APP.logger.warn("Error! Failed to initialize the db.") return json.dumps({"Error": "Failed to initialize the db"}), 500
def get_growth_data(year, quarter): ''' 抓取,可以存在数据库 ''' k = "%s-%s" % (year, quarter) table_name = "%s_growth" % (k) if table_exists(table_name): df = read_df_from_db(table_name, index_col='code') df['code'] = df.index else: try: df = ts.get_growth_data(year, quarter) df_to_db(table_name, df) except OSError as e: df = None return df
def fetch_daily_data_to_db(ts_code, end_date=None): ''' 抓日数据 ''' if end_date is None: end_date = datetime.now().strftime("%Y%m%d") if not isinstance(end_date, str): end_date = convert_date(end_date).strftime("%Y%m%d") start_date = get_daily_start_date(ts_code) table_name = daily_data_table_name(ts_code) df = pro.daily(ts_code=ts_code, start_date=start_date, end_date=end_date) df.index = pd.to_datetime(df['trade_date']) df.index.name = 'date' if table_exists(table_name): df_to_db(table_name, df, if_exists='append') else: df_to_db(table_name, df)
def get_h_data(code): ''' 获取数据,并且存在数据库中 ''' if code in ['600656', '600832', '600806', '601268', '600222']: return None table_name = "%s_hlong" % (code) if table_exists(table_name): return read_df_from_db(table_name, index_col='date') else: time.sleep(60 * 3) try: df = ts.get_h_data(code, start=start_d) df_to_db(table_name, df) return df except Exception as inst: print(inst) print("%s\n" % (code)) return None