refactor scripts to work wit api
This commit is contained in:
+48
-61
@@ -1,12 +1,13 @@
|
||||
"""
|
||||
Checks the database kontor
|
||||
"""
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum, auto
|
||||
|
||||
import mariadb
|
||||
from pathlib import Path
|
||||
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
|
||||
|
||||
import requests
|
||||
from config import get_logger, get_database_cursors
|
||||
|
||||
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
|
||||
@@ -25,71 +26,63 @@ class StatusType(Enum):
|
||||
CLOUD_LINK = auto()
|
||||
CLOUD_LINK_ID = auto()
|
||||
|
||||
@dataclass
|
||||
class FileStatus:
|
||||
id: str
|
||||
status_type: StatusType
|
||||
|
||||
def get_status_of_file(found_file, cursor):
|
||||
status = StatusType.UNKNOWN
|
||||
file_id = ''
|
||||
try:
|
||||
cursor.execute(f'SELECT id, cloud_link FROM media_file WHERE file_name="{found_file.name}"')
|
||||
rows = cursor.fetchall()
|
||||
if len(rows) == 1:
|
||||
status = StatusType.FILE_NAME
|
||||
file_id = rows[0][0]
|
||||
except mariadb.Error as error:
|
||||
logger.debug(f'select failed with {error}')
|
||||
try:
|
||||
cursor.execute(f'SELECT id FROM media_file WHERE id="{found_file.stem}"')
|
||||
rows = cursor.fetchall()
|
||||
if len(rows) == 1:
|
||||
status = StatusType.FILE_ID
|
||||
file_id = rows[0][0]
|
||||
if len(rows) > 1:
|
||||
status = StatusType.DUPLICATE
|
||||
for row in rows:
|
||||
logger.info(f"found {row[0]} with {found_file}")
|
||||
except mariadb.Error as error:
|
||||
logger.debug(f'select failed with {error}')
|
||||
try:
|
||||
cursor.execute(f'SELECT id FROM media_file WHERE cloud_link LIKE "%{found_file.stem}%"')
|
||||
rows = cursor.fetchall()
|
||||
if len(rows) == 1:
|
||||
file_id = rows[0][0]
|
||||
if rows[0][0] == found_file.stem:
|
||||
status = StatusType.CLOUD_LINK_ID
|
||||
else:
|
||||
status = StatusType.CLOUD_LINK
|
||||
except mariadb.Error as error:
|
||||
logger.debug(f'select failed with {error}')
|
||||
return status, file_id
|
||||
|
||||
def rename_files_to_id(media_dir, conn, dry_run):
|
||||
def get_status_of_file(found_file: Path, log) -> FileStatus:
|
||||
status = FileStatus()
|
||||
response = requests.post("http://127.0.0.1:8800/media/search")
|
||||
log.info(f"Status: {response.status_code}")
|
||||
data = response.json()
|
||||
status.import(data)
|
||||
if len(data) == 1:
|
||||
status = StatusType.FILE_NAME
|
||||
status.id = data['id']
|
||||
response = requests.get(f"http://127.0.0.1:8800/media/files/{found_file.stem}")
|
||||
log.info(f"Status: {response.status_code}")
|
||||
data = response.json()
|
||||
if len(data) == 1:
|
||||
status = StatusType.FILE_ID
|
||||
file_id = data['id']
|
||||
response = requests.get(f"http://127.0.0.1:8800/media/files?cloud_link=true")
|
||||
log.info(f"Status: {response.status_code}")
|
||||
data = response.json()
|
||||
if len(data) == 1:
|
||||
status = StatusType.CLOUD_LINK_ID
|
||||
file_id = data['id']
|
||||
return status
|
||||
|
||||
def rename_files_to_id(media_dir, dry_run, log):
|
||||
media_path = Path(media_dir)
|
||||
cursor = conn.cursor()
|
||||
for file in media_path.iterdir():
|
||||
logger.debug('found file: {}'.format(file.name))
|
||||
(status, file_id) = get_status_of_file(file, cursor)
|
||||
new_file_path = file.with_name(f"{file_id}{file.suffix}")
|
||||
match status:
|
||||
log.debug('found file: {}'.format(file.name))
|
||||
status = get_status_of_file(file, log)
|
||||
new_file_path = file.with_name(f"{status.id}{file.suffix}")
|
||||
file_id = status.id
|
||||
match status.status_type:
|
||||
case StatusType.FILE_NAME:
|
||||
logger.info(f'status of {file.name} is file_name')
|
||||
log.info(f'status of {file.name} is file_name')
|
||||
rename_file(file, new_file_path, dry_run)
|
||||
update_cloud_link(file_id, new_file_path, conn, dry_run)
|
||||
update_cloud_link(file_id, new_file_path, dry_run)
|
||||
case StatusType.FILE_ID:
|
||||
logger.info(f'status of {file.name} is file_id')
|
||||
update_cloud_link(file_id, new_file_path, conn, dry_run)
|
||||
log.info(f'status of {file.name} is file_id')
|
||||
update_cloud_link(file_id, new_file_path, dry_run)
|
||||
case StatusType.CLOUD_LINK:
|
||||
logger.info(f'status of {file.name} is cloud_link')
|
||||
log.info(f'status of {file.name} is cloud_link')
|
||||
rename_file(file, new_file_path, dry_run)
|
||||
update_cloud_link(file_id, new_file_path, conn, dry_run)
|
||||
update_cloud_link(file_id, new_file_path, dry_run)
|
||||
case StatusType.CLOUD_LINK_ID:
|
||||
logger.debug(f'status of {file.name} is cloud_link_id')
|
||||
update_cloud_link(file_id, new_file_path, conn, dry_run)
|
||||
log.debug(f'status of {file.name} is cloud_link_id')
|
||||
update_cloud_link(file_id, new_file_path, dry_run)
|
||||
case StatusType.DUPLICATE:
|
||||
logger.info(f'status of {file.name} is duplicate')
|
||||
log.info(f'status of {file.name} is duplicate')
|
||||
case StatusType.UNKNOWN:
|
||||
logger.info(f'status of {file.name} is unknown')
|
||||
log.info(f'status of {file.name} is unknown')
|
||||
case _:
|
||||
logger.info(f'status of {file.name} is not defined')
|
||||
log.info(f'status of {file.name} is not defined')
|
||||
|
||||
def rename_file(current_file, new_file_path, dry_run):
|
||||
if dry_run:
|
||||
@@ -118,14 +111,8 @@ def reset_cloud_link(conn, dry_run):
|
||||
if __name__ == '__main__':
|
||||
logger = get_logger(args.verbose, args.config)
|
||||
logger.info("kontor.check_kontor started")
|
||||
_, mariadb_conn = get_database_cursors(logger, args.config)
|
||||
mariadb_cursor = mariadb_conn.cursor()
|
||||
if args.reset_cloud_link:
|
||||
reset_cloud_link(mariadb_conn, args.dry_run)
|
||||
link_list = []
|
||||
data_dir = args.dir
|
||||
logger.info("kontor.check_kontor.rename_files_to_id")
|
||||
rename_files_to_id(data_dir, mariadb_conn, args.dry_run)
|
||||
rename_files_to_id(args.dir, args.dry_run, logger)
|
||||
#logger.info("kontor.check_kontor.update_cloud_link_with_found_files")
|
||||
#update_cloud_link_with_found_files(data_dir, mariadb_conn, args.dry_run)
|
||||
#logger.info("kontor.check_kontor.get_ids_from_column_cloud_link")
|
||||
@@ -133,5 +120,5 @@ if __name__ == '__main__':
|
||||
#logger.info('found {} ids in column cloud_link'.format(len(link_list)))
|
||||
#logger.info("kontor.check_kontor.checking_ids_from_cloud_link")
|
||||
#checking_ids_from_cloud_link(link_list, mariadb_cursor)
|
||||
mariadb_conn.close()
|
||||
logger.info("kontor.check_kontor finished")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user