# Path: kquant_data/config.py
# __CONFIG_H5_FUT_SECTOR_DIR__ = os.path.join(__CONFIG_H5_FUT_DIR__, 'sectorconstituent')
# 
# __CONFIG_TDAYS_SHFE_FILE__ = os.path.join(__CONFIG_H5_FUT_DIR__, 'tdays', 'SHFE.csv')
# 
# Path: kquant_data/wind/tdays.py
# def read_tdays(path):
#     try:
#         df = pd.read_csv(path, parse_dates=True)
#     except:
#         return None
# 
#     df['date'] = pd.to_datetime(df['date'])
#     df.index = df['date']
#     return df
# 
# Path: kquant_data/wind_resume/wset.py
# def download_sector(
#         w,
#         trading_days,
#         root_path,
#         sector_name="风险警示股票"):
#     """
#     下载ST股票的信息，在已有的文件中补数据，这种不会多下载
#     :param w:
#     :param trading_days:
#     :param sector_name:
#     :param root_path:
#     :return:
#     """
#     df = trading_days
#     df['date_str'] = trading_days['date'].astype(str)
# 
#     foldpath = os.path.join(root_path, sector_name)
#     file_download_constituent(w, df['date_str'], foldpath, '.csv',
#                               sector=sector_name, windcode=None, field='wind_code', is_indexconstituent=False)
# 
#     dst_path = os.path.join(root_path, "%s_move" % sector_name)
#     if not os.path.exists(dst_path):
#         os.makedirs(dst_path)
#     move_constituent(foldpath, dst_path)


import sys
from datetime import datetime, timedelta
from WindPy import w
from kquant_data.config import __CONFIG_H5_FUT_SECTOR_DIR__, __CONFIG_TDAYS_SHFE_FILE__
from kquant_data.wind.tdays import read_tdays
from kquant_data.wind_resume.wset import download_sector
下载版块相关信息
"""



# 解决Python 3.6的pandas不支持中文路径的问题
print(sys.getfilesystemencoding())  # 查看修改前的
try:
    sys._enablelegacywindowsfsencoding()  # 修改
    print(sys.getfilesystemencoding())  # 查看修改后的
except:
    pass

if __name__ == '__main__':
    w.start()

    trading_days = read_tdays(__CONFIG_TDAYS_SHFE_FILE__)

    # 移动到下一个交易日
    date_str = (datetime.today() + timedelta(days=0)).strftime('%Y-%m-%d')
    new_trading_days = trading_days[date_str:]
    date_str = (new_trading_days.iloc[1, 0]).strftime('%Y-%m-%d')

    new_trading_days = trading_days['1999-01-04':date_str]
    download_sector(w, new_trading_days, sector_name="大商所全部品种", root_path=__CONFIG_H5_FUT_SECTOR_DIR__)
    new_trading_days = trading_days['1995-04-17':date_str]
    download_sector(w, new_trading_days, sector_name="上期所全部品种", root_path=__CONFIG_H5_FUT_SECTOR_DIR__)
    new_trading_days = trading_days['1999-01-04':date_str]
    download_sector(w, new_trading_days, sector_name="郑商所全部品种", root_path=__CONFIG_H5_FUT_SECTOR_DIR__)
    new_trading_days = trading_days['2010-04-19':date_str]

# Path: kquant_data/config.py
# __CONFIG_H5_FUT_SECTOR_DIR__ = os.path.join(__CONFIG_H5_FUT_DIR__, 'sectorconstituent')
# 
# __CONFIG_TDAYS_SHFE_FILE__ = os.path.join(__CONFIG_H5_FUT_DIR__, 'tdays', 'SHFE.csv')
# 
# Path: kquant_data/wind/tdays.py
# def read_tdays(path):
#     try:
#         df = pd.read_csv(path, parse_dates=True)
#     except:
#         return None
# 
#     df['date'] = pd.to_datetime(df['date'])
#     df.index = df['date']
#     return df
# 
# Path: kquant_data/wind_resume/wset.py
# def download_sector(
#         w,
#         trading_days,
#         root_path,
#         sector_name="风险警示股票"):
#     """
#     下载ST股票的信息，在已有的文件中补数据，这种不会多下载
#     :param w:
#     :param trading_days:
#     :param sector_name:
#     :param root_path:
#     :return:
#     """
#     df = trading_days
#     df['date_str'] = trading_days['date'].astype(str)
# 
#     foldpath = os.path.join(root_path, sector_name)
#     file_download_constituent(w, df['date_str'], foldpath, '.csv',
#                               sector=sector_name, windcode=None, field='wind_code', is_indexconstituent=False)
# 
#     dst_path = os.path.join(root_path, "%s_move" % sector_name)
#     if not os.path.exists(dst_path):
#         os.makedirs(dst_path)
#     move_constituent(foldpath, dst_path)


# Path: demo_future/C01_download_sectors.py
import sys
from datetime import datetime, timedelta
from WindPy import w
from kquant_data.config import __CONFIG_H5_FUT_SECTOR_DIR__, __CONFIG_TDAYS_SHFE_FILE__
from kquant_data.wind.tdays import read_tdays
from kquant_data.wind_resume.wset import download_sector

下载版块相关信息
"""



# 解决Python 3.6的pandas不支持中文路径的问题
print(sys.getfilesystemencoding())  # 查看修改前的
try:
    sys._enablelegacywindowsfsencoding()  # 修改
    print(sys.getfilesystemencoding())  # 查看修改后的
except:
    pass

if __name__ == '__main__':
    w.start()

    trading_days = read_tdays(__CONFIG_TDAYS_SHFE_FILE__)

    # 移动到下一个交易日
    date_str = (datetime.today() + timedelta(days=0)).strftime('%Y-%m-%d')
    new_trading_days = trading_days[date_str:]
    date_str = (new_trading_days.iloc[1, 0]).strftime('%Y-%m-%d')

    new_trading_days = trading_days['1999-01-04':date_str]
    download_sector(w, new_trading_days, sector_name="大商所全部品种", root_path=__CONFIG_H5_FUT_SECTOR_DIR__)
    new_trading_days = trading_days['1995-04-17':date_str]
    download_sector(w, new_trading_days, sector_name="上期所全部品种", root_path=__CONFIG_H5_FUT_SECTOR_DIR__)
    new_trading_days = trading_days['1999-01-04':date_str]
    download_sector(w, new_trading_days, sector_name="郑商所全部品种", root_path=__CONFIG_H5_FUT_SECTOR_DIR__)
    new_trading_days = trading_days['2010-04-19':date_str]

    download_sector(w, new_trading_days, sector_name="中金所全部品种", root_path=__CONFIG_H5_FUT_SECTOR_DIR__)