From ec611f4deafca43e07424e569111340e1e4a4c71 Mon Sep 17 00:00:00 2001 From: "vladimir.buzalka" Date: Tue, 30 Dec 2025 10:55:03 +0100 Subject: [PATCH] z230 --- TestduplicitSyncThing.py | 153 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 TestduplicitSyncThing.py diff --git a/TestduplicitSyncThing.py b/TestduplicitSyncThing.py new file mode 100644 index 0000000..b4dc75a --- /dev/null +++ b/TestduplicitSyncThing.py @@ -0,0 +1,153 @@ +import os +import pymysql +import sys + +# ============================== +# ⚙️ NASTAVENÍ +# ============================== +LOCAL_ROOT = r"U:\#Syncthing" + +# DB Připojení +DB_HOST = "192.168.1.76" +DB_PORT = 3307 # Port pro tvou DB +DB_USER = "root" +DB_PASS = "Vlado9674+" # Tvé heslo +DB_NAME = "torrents" +DB_TABLE = "file_md5_index" +REMOTE_SHARE_KEY = "#ColdData" + + +# ============================== + +def get_db_connection(): + try: + return pymysql.connect( + host=DB_HOST, + port=DB_PORT, + user=DB_USER, + password=DB_PASS, + database=DB_NAME, + cursorclass=pymysql.cursors.DictCursor + ) + except Exception as e: + print(f"❌ CHYBA: Nelze se připojit k MySQL na {DB_HOST}:{DB_PORT}.") + print(f" Detail: {e}") + sys.exit(1) + + +def normalize_path(path): + """ + 1. Převede Windows '\\' na Linux '/' + 2. Převede vše na malá písmena (aby 'Foto.jpg' == 'foto.JPG') + """ + return path.replace("\\", "/").lower() + + +def get_local_files(root_path): + print(f"📂 Skenuji lokální disk: {root_path}") + local_files = set() + + if not os.path.exists(root_path): + print(f"❌ Chyba: Cesta {root_path} neexistuje.") + return local_files + + count = 0 + for root, dirs, files in os.walk(root_path): + # Ignorovat systémové složky Syncthingu + if ".stfolder" in dirs: dirs.remove(".stfolder") + if ".stversions" in dirs: dirs.remove(".stversions") + + for file in files: + full_path = os.path.join(root, file) + + try: + rel_path = os.path.relpath(full_path, root_path) + except ValueError: + continue + + # NORMALIZACE (Lomítka + Malá písmena) + clean_path = normalize_path(rel_path) + + local_files.add(clean_path) + count += 1 + + if count % 1000 == 0: + print(f" ... načteno {count} lokálních souborů ...", end="\r") + + print(f"✅ Lokálně nalezeno: {len(local_files):,} souborů.") + return local_files + + +def get_remote_files_from_db(): + print(f"📡 Připojuji se k DB '{DB_NAME}' a stahuji seznam...") + conn = get_db_connection() + remote_files = set() + + try: + with conn.cursor() as cursor: + sql = f"SELECT full_path FROM {DB_TABLE} WHERE full_path LIKE %s" + cursor.execute(sql, (f"%/{REMOTE_SHARE_KEY}/%",)) + + rows = cursor.fetchall() + print(f" ... DB vrátila {len(rows):,} řádků. Zpracovávám...") + + for row in rows: + full_path_db = row['full_path'] + + if f"/{REMOTE_SHARE_KEY}/" in full_path_db: + parts = full_path_db.split(f"/{REMOTE_SHARE_KEY}/") + if len(parts) > 1: + rel_path = parts[1] + + # NORMALIZACE (Lomítka + Malá písmena) + clean_path = normalize_path(rel_path) + remote_files.add(clean_path) + + finally: + conn.close() + + print(f"✅ V DB nalezeno: {len(remote_files):,} unikátních cest.") + return remote_files + + +def main(): + print("🚀 HLEDÁNÍ DUPLIKÁTŮ (Case Insensitive)") + print("=======================================") + + local_set = get_local_files(LOCAL_ROOT) + remote_set = get_remote_files_from_db() + + print("\n🔍 Porovnávám (všechna písmena ignorují velikost)...") + + # Průnik + duplicates = local_set.intersection(remote_set) + only_local = local_set - remote_set + + print("\n📊 VÝSLEDKY") + print("=======================================") + print(f"🏠 Celkem lokálně: {len(local_set):,}") + print(f"☁️ Celkem v DB: {len(remote_set):,}") + print("---------------------------------------") + print(f"👯 SHODA (DUPLIKÁTY): {len(duplicates):,}") + print(f"🆕 UNIKÁTNÍ (JEN DOMA): {len(only_local):,}") + print("=======================================\n") + + if duplicates: + file_dup = "report_DUPLIKATY.txt" + with open(file_dup, "w", encoding="utf-8") as f: + for item in sorted(duplicates): + f.write(f"{item}\n") + print(f"💾 Seznam duplikátů (malá písmena) uložen do: {file_dup}") + + if only_local: + file_new = "report_NOVE_SOUBORY.txt" + with open(file_new, "w", encoding="utf-8") as f: + for item in sorted(only_local): + f.write(f"{item}\n") + print(f"💾 Seznam nových souborů uložen do: {file_new}") + + input("\nStiskni ENTER pro ukončení...") + + +if __name__ == "__main__": + main() \ No newline at end of file