import os
import shutil
import time
from datetime import datetime

from stock.crawler.FnGuideCrawler import FnGuideCrawler
from stock.crawler.MetaCrawler import MetaCrawler
from stock.crawler.StockCrawler import StockCrawler
from stock.analysis.AnalyzerSqlite import AnalyzerSqlite

today = datetime.now().strftime("%Y-%m-%d")

# DB Browser for SQLite: http://hleecaster.com/python-sqlite3/

PROJECT_HOME = os.getcwd()

START_DATE = "1900.01.01"
start = time.time()
stockFileName = PROJECT_HOME + '/resources/stock.db'

week = datetime.today().weekday()

if week in (0, 1, 2, 3, 4):

    # 재무제표는 3개월마다 다운로드를 한다.
    fnGuideCrawler = FnGuideCrawler(START_DATE)
    print("[KOSPI 상장기업 재무제표 다운로드]")
    fnGuideCrawler.crawl_fnguide(stockFileName)

    metaCrawler = MetaCrawler(START_DATE)
    print("\n[증시자금동향 (신용잔고, 펀드자금 잔고)]")
    metaCrawler.crawl_money_trend(stockFileName)

    print("\n[국내 시장금리]")
    metaCrawler.crawl_interest_rates(stockFileName)

    print("\n[투자자별 매매동향(Trading_Trend)]")
    metaCrawler.crawl_trading_trend(stockFileName)

    print("\n[환율 (USD, JPY, EUR, CNY)]")
    metaCrawler.crawl_exchange(stockFileName)

    print("\n[원유 (WTI), 국제금, COPPER, NATURALGAS, CORN, SOYBEAN]")
    metaCrawler.crawl_meterials(stockFileName)


    print("\n[종목 다운로드]")
    stockCrawler = StockCrawler(START_DATE)
    stockCrawler.crawl_etf_stocks(stockFileName)
    stockCrawler.crawl_stocks(stockFileName)
    #stockCrawler.crawl_special_stocks(stockFileName)

    print("\n[종목 분석]")
    # S: 분석까지 진행
    inFileName = PROJECT_HOME + '/resources/stock.db'
    analyzerSqlite = AnalyzerSqlite(PROJECT_HOME, stockFileName)

    analyzerSqlite.analyzeDaily()
    analyzerSqlite.analyzeGrouping("weekly")
    analyzerSqlite.analyzeGrouping("monthly")

    analyzerSqlite = AnalyzerSqlite(PROJECT_HOME, stockFileName)
    print("\n[종목 결정]")
    # HTML 출력
    outPath = os.path.join(PROJECT_HOME, "resources", "analysis")
    if not os.path.isdir(outPath):
        os.mkdir(outPath)
    day = datetime.today().strftime("%Y%m%d")
    outPath = os.path.join(outPath, day)
    if os.path.isdir(outPath):
        shutil.rmtree(outPath)
    os.mkdir(outPath)
    print("print to Html...")
    analyzerSqlite.findCandidate(outPath)


print("time : %6.2f 초", (time.time() - start))

print ("done...")
