import os, datetime

def get_folders(path):
    folders=[]
    for root,dirs,files in os.walk(path):
        for dir in dirs:
            moddatetime=datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(root,dir)))
            folders.append((os.path.join(root,dir),moddatetime))
    return folders







import pymysql
from pymysql.cursors import DictCursor
from GetFunctions import *
conn = pymysql.connect(host='192.168.1.76', user="root", password="Vlado9674+", database="Duplicates", port=3307,cursorclass=DictCursor,init_command="SET SESSION sql_mode='NO_BACKSLASH_ESCAPES'")
cur1 = conn.cursor()
# set_sql_mode_no_escape(cur1)

cpath=r"U:\a"
folds=get_folders(cpath)

print("folds",folds)
search_pattern=cpath
# print(f"{search_pattern}%")

cur1.execute("select folder,modified from tfolders where folder like %s",[f"{search_pattern}%"])
vtabulce=([(row["folder"],row["modified"]) for row in cur1.fetchall()])
# vtabulce.append((row["folder"],row["modified"]) for row in cur1.fetchall())
print("vtabulce",vtabulce)
print(type(vtabulce))

new=list(set(folds) - set(vtabulce))
todelete=list(set(vtabulce) - set(folds))
print("MISSING",new)
print("TODELETE",todelete)

#zde vložení nových
cur1.executemany("insert into tfolders (folder,modified) values (%s,%s)",new)
conn.commit()

#zde smazání neexistujících
cur1.executemany("delete from tfolders where folder=%s and modified=%s",todelete)
conn.commit()
