import requests, datetime
from bs4 import BeautifulSoup
import openpyxl, os

def get_board():
    url = "http://isangjeong.icems.kr/boardCnts/list.do?searchType=S&page=1&boardID=33523&prntBoardID=0&prntBoardSeq=0&prntLev=0&m=0601&s=isangjeong"

    response = requests.get(url)

    soup = BeautifulSoup(response.text, "html.parser")

    element = soup.select_one("html body div:nth-of-type(2) div:nth-of-type(3) div div:nth-of-type(2) section:nth-of-type(2) div:nth-of-type(2) div:nth-of-type(2) div:nth-of-type(2) div form table tbody tr:nth-of-type(1) td:nth-of-type(2) a")
    onclick_value = element.get("onclick")

    values = str(onclick_value[18:-1].replace("'", "")).replace(" ", "").split(",")
#    for i in values:
#        print(i)

    return f"http://isangjeong.icems.kr/boardCnts/updateCnt.do?boardID={values[0]}&viewBoardID={values[1]}&boardSeq={values[2]}&lev={values[3]}"

path = f'{os.getenv('ROOT')}temp/downloaded_file.xlsx'

def download(url:str):
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")
    element = soup.select_one("html > body > div:nth-of-type(2) > div:nth-of-type(3) > div > div:nth-of-type(2) > section:nth-of-type(2) > div:nth-of-type(2) > div:nth-of-type(2) > div:nth-of-type(2) > div > form > table > tbody > tr:nth-of-type(2) > td > p > a:nth-of-type(1)")
    herf_value = element.get("href")

    response = requests.get("http://isangjeong.icems.kr"+herf_value)
    with open(path, "wb") as file:
        file.write(response.content)

def get_vts():
    #download(get_board())
    ws = openpyxl.load_workbook(path).active

    vts_list = []

    for row in ws.iter_rows(min_row=5, max_row=5, min_col=7, max_col=14):
        for cell in row:
            if cell.fill.start_color.index == 'FFFFFF00':
                value = str(cell.value).split('(')[0].strip()\
                    .replace(" ", "")\
                    .replace("월", "")\
                    .replace("일", "")
                vts_list.append(f"{datetime.datetime.today().year}{value}")
    return vts_list

def get_vts_true_or_false():
    vts_list = get_vts()
    today = datetime.datetime.today().strftime("%Y%m%d")
    if today in vts_list:
        return True
    else:
        return False
    return False

