# 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)
# 
# def download_sectors(
#         w,
#         trading_days,
#         root_path,
#         sector_name="中信证券一级行业指数"):
#     """
#     指定行业列表后，下载其中的数据，带子目录
#     :param w:
#     :param trading_days:
#     :param sector_name:
#     :param root_path:
#     :return:
#     """
#     # 下载板块数据
#     path = os.path.join(root_path, '%s.csv' % sector_name)
#     sectors = pd.read_csv(path, encoding='utf-8-sig')
# 
#     for i in range(0, len(sectors)):
#         print(sectors.iloc[i, :])
# 
#         wind_code = sectors.ix[i, 'wind_code']
#         sec_name = sectors.ix[i, 'sec_name']
# 
#         foldpath = os.path.join(root_path, sector_name, sec_name)
#         try:
#             os.mkdir(foldpath)
#         except:
#             pass
# 
#         df = trading_days
#         df['date_str'] = trading_days['date'].astype(str)
#         file_download_constituent(w, df['date_str'], foldpath, '.csv',
#                                   sector=None, windcode=wind_code, field='wind_code', is_indexconstituent=False)
#         # 移除多余的数据文件
#         dst_path = os.path.join(root_path, "%s_move" % sector_name, sec_name)
#         if not os.path.exists(dst_path):
#             os.makedirs(dst_path)
#         move_constituent(foldpath, dst_path)
# 
# Path: kquant_data/config.py
# __CONFIG_H5_STK_SECTOR_DIR__ = os.path.join(__CONFIG_H5_STK_DIR__, 'sectorconstituent')
# 
# __CONFIG_TDAYS_SSE_FILE__ = os.path.join(__CONFIG_H5_STK_DIR__, 'tdays', 'SSE.csv')


import sys
from WindPy import w
from datetime import datetime
from kquant_data.wind.tdays import read_tdays
from kquant_data.wind_resume.wset import download_sector, download_sectors
from kquant_data.config import __CONFIG_H5_STK_SECTOR_DIR__, __CONFIG_TDAYS_SSE_FILE__
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
下载行业分类相关信息
"""


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

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

    date_str = datetime.today().strftime('%Y-%m-%d')

    trading_days = read_tdays(__CONFIG_TDAYS_SSE_FILE__)
    trading_days = trading_days['2004-06-01':date_str]

    # 按频率来看数据是稀疏的，但需要每天下载一次
    if True:
        download_sectors(w, trading_days, sector_name="中信证券一级行业指数", root_path=__CONFIG_H5_STK_SECTOR_DIR__)
    if False:

# 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)
# 
# def download_sectors(
#         w,
#         trading_days,
#         root_path,
#         sector_name="中信证券一级行业指数"):
#     """
#     指定行业列表后，下载其中的数据，带子目录
#     :param w:
#     :param trading_days:
#     :param sector_name:
#     :param root_path:
#     :return:
#     """
#     # 下载板块数据
#     path = os.path.join(root_path, '%s.csv' % sector_name)
#     sectors = pd.read_csv(path, encoding='utf-8-sig')
# 
#     for i in range(0, len(sectors)):
#         print(sectors.iloc[i, :])
# 
#         wind_code = sectors.ix[i, 'wind_code']
#         sec_name = sectors.ix[i, 'sec_name']
# 
#         foldpath = os.path.join(root_path, sector_name, sec_name)
#         try:
#             os.mkdir(foldpath)
#         except:
#             pass
# 
#         df = trading_days
#         df['date_str'] = trading_days['date'].astype(str)
#         file_download_constituent(w, df['date_str'], foldpath, '.csv',
#                                   sector=None, windcode=wind_code, field='wind_code', is_indexconstituent=False)
#         # 移除多余的数据文件
#         dst_path = os.path.join(root_path, "%s_move" % sector_name, sec_name)
#         if not os.path.exists(dst_path):
#             os.makedirs(dst_path)
#         move_constituent(foldpath, dst_path)
# 
# Path: kquant_data/config.py
# __CONFIG_H5_STK_SECTOR_DIR__ = os.path.join(__CONFIG_H5_STK_DIR__, 'sectorconstituent')
# 
# __CONFIG_TDAYS_SSE_FILE__ = os.path.join(__CONFIG_H5_STK_DIR__, 'tdays', 'SSE.csv')


# Path: demo_stock/A_1day/B04_download_sectors_st.py
import sys
from WindPy import w
from datetime import datetime
from kquant_data.wind.tdays import read_tdays
from kquant_data.wind_resume.wset import download_sector, download_sectors
from kquant_data.config import __CONFIG_H5_STK_SECTOR_DIR__, __CONFIG_TDAYS_SSE_FILE__

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
下载行业分类相关信息
"""


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

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

    date_str = datetime.today().strftime('%Y-%m-%d')

    trading_days = read_tdays(__CONFIG_TDAYS_SSE_FILE__)
    trading_days = trading_days['2004-06-01':date_str]

    # 按频率来看数据是稀疏的，但需要每天下载一次
    if True:
        download_sectors(w, trading_days, sector_name="中信证券一级行业指数", root_path=__CONFIG_H5_STK_SECTOR_DIR__)
    if False:

        download_sector(w, trading_days, sector_name="风险警示股票", root_path=__CONFIG_H5_STK_SECTOR_DIR__)