import mysql.connector import os import re def find(name, path): for root, dirs, files in os.walk(path): for file in files: full_name = os.path.join(root, file) if full_name.find(name) is not -1: return full_name def find_recording(name, storages): for s in storages: result = find(name, s["path"]) if result is not None: return result try: db = mysql.connector.connect( host="localhost", user="bluecherry", password="bluecherry", database="bluecherry" ) cursor = db.cursor(buffered=True,dictionary=True) cursor_update = db.cursor() query = "select * from Storage" cursor.execute(query) storages = cursor.fetchall() query = "select * from Media" cursor.execute(query) print("foud " + str(cursor.rowcount) + " files") i = 0 j = 0 step = cursor.rowcount / 10 for result in cursor: if not os.path.isfile(result["filepath"]): print("file not found - " + result["filepath"]) file_name = re.search("[0-9]+/[^/]*$", result["filepath"]).group(0) print("searching for " + file_name + " in all storage paths") new_location = find_recording(file_name, storages) if new_location is not None: print("found file: " + new_location) query_u = "update Media set filepath='" + new_location + "' where id='" + str(result["id"]) + "'" print("fixing with: " + query_u) cursor_update.execute(query_u) else: print("couldn't find this file anywhere, sorry") i = i + 1 if i > step: j = j + 1 print("total progress " + str(j) + "0%...") i = 0 if j == 9: print("total progress 100%...") db.commit() except Exception as e: print("error " + e) finally: if db.is_connected(): cursor.close() cursor_update.close() db.close()