2025-04-04 15:24:02 +08:00
|
|
|
import os
|
|
|
|
import yaml
|
|
|
|
import tushare as ts
|
|
|
|
import pandas as pd
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
2025-04-05 22:48:31 +08:00
|
|
|
from sqlalchemy import create_engine
|
|
|
|
|
2025-04-04 15:24:02 +08:00
|
|
|
|
|
|
|
def load_config():
|
|
|
|
config_path = 'config.yaml'
|
|
|
|
if not os.path.exists(config_path):
|
2025-04-05 01:02:15 +08:00
|
|
|
config = {
|
|
|
|
'tushare_token': 'your_token_here',
|
|
|
|
'mysql': {
|
|
|
|
'user': 'user',
|
|
|
|
'password': 'password',
|
|
|
|
'host': '127.0.0.1',
|
|
|
|
'port': 3306,
|
|
|
|
'database': 'tushare',
|
|
|
|
'charset': 'utf8'
|
|
|
|
}
|
|
|
|
}
|
2025-04-04 15:24:02 +08:00
|
|
|
with open(config_path, 'w') as f:
|
|
|
|
yaml.dump(config, f)
|
2025-04-05 01:02:15 +08:00
|
|
|
print(f"请在 {config_path} 中填入您的 tushare token 和 MySQL 连接信息")
|
2025-04-04 15:24:02 +08:00
|
|
|
exit(1)
|
2025-04-05 01:02:15 +08:00
|
|
|
|
2025-04-04 15:24:02 +08:00
|
|
|
with open(config_path, 'r') as f:
|
|
|
|
config = yaml.safe_load(f)
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
2025-04-05 22:48:31 +08:00
|
|
|
def create_engine_from_config(config):
|
|
|
|
mysql = config['mysql']
|
|
|
|
connection_string = f"mysql+pymysql://{mysql['user']}:{mysql['password']}@{mysql['host']}:{mysql['port']}/{mysql['database']}?charset={mysql['charset']}&use_unicode=1"
|
|
|
|
return create_engine(connection_string)
|