from sqlalchemy import create_engine
import pymysql
import urllib.parse
import pandas as pd

MARKET_CODE_DICT = {
    'kospi': 'stockMkt',
    'kosdaq': 'kosdaqMkt',
    'konex': 'konexMkt'
}
DOWNLOAD_URL = 'kind.krx.co.kr/corpgeneral/corpList.do'

def download_stock_codes(market=None, delisted=False):
    params = {'method': 'download'}
    if market.lower() in MARKET_CODE_DICT:
        params['marketType'] = MARKET_CODE_DICT[market]
    if not delisted:
        params['searchType'] = 13

    params_string = urllib.parse.urlencode(params)
    request_url = urllib.parse.urlunsplit(['http', DOWNLOAD_URL, '', params_string, ''])
    df = pd.read_html(request_url, header=0)[0]
    df.종목코드 = df.종목코드.map('{:06d}'.format)
    return df

pymysql.install_as_MySQLdb()
engine = create_engine("mysql+mysqldb://root:"+"tokki1127"+"@127.0.0.1/stock", encoding='utf-8')
conn = engine.connect()

kosdaq_stocks = download_stock_codes('kosdaq')
kospi_stocks = download_stock_codes('kospi')
konex_stocks = download_stock_codes('konex')
kosdaq_stocks.to_sql(name="company", con=conn, if_exists='append')
kospi_stocks.to_sql(name="company", con=conn, if_exists='append')
konex_stocks.to_sql(name="company", con=conn, if_exists='append')