update check_kontor.py to call Kontor API
This commit is contained in:
+72
-50
@@ -1,19 +1,18 @@
|
||||
"""
|
||||
Checks the database kontor
|
||||
"""
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum, auto
|
||||
|
||||
import mariadb
|
||||
import requests
|
||||
from pathlib import Path
|
||||
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
|
||||
|
||||
import requests
|
||||
from config import get_logger, get_database_cursors
|
||||
|
||||
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
|
||||
parser.add_argument('--verbose', '-v', action='count', default=0)
|
||||
parser.add_argument('--config', '-c', default='kontor-docker')
|
||||
parser.add_argument('--dir', '-d', default='/media/tpeetz/Media')
|
||||
parser.add_argument('--config', '-c', default='kontor')
|
||||
parser.add_argument('--dir', '-d', default='/data/media')
|
||||
parser.add_argument('--dry-run', '-m', action='store_true')
|
||||
parser.add_argument('--reset-cloud-link', '-r', action='store_true')
|
||||
args = parser.parse_args()
|
||||
@@ -26,93 +25,116 @@ class StatusType(Enum):
|
||||
CLOUD_LINK = auto()
|
||||
CLOUD_LINK_ID = auto()
|
||||
|
||||
@dataclass
|
||||
class FileStatus:
|
||||
id: str
|
||||
status_type: StatusType
|
||||
id: str | None = None
|
||||
status_type: StatusType = StatusType.UNKNOWN
|
||||
|
||||
def get_response(self, response: dict):
|
||||
self.status_type = StatusType.FILE_NAME
|
||||
self.id = response['id']
|
||||
|
||||
|
||||
def get_status_of_file(found_file: Path, log) -> FileStatus:
|
||||
def get_status_of_file(found_file: Path, cursor, 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']
|
||||
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.status_type = StatusType.FILE_NAME
|
||||
status.id = rows[0][0]
|
||||
except mariadb.Error as error:
|
||||
log.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.status_type = StatusType.FILE_ID
|
||||
status.id = rows[0][0]
|
||||
if len(rows) > 1:
|
||||
status.status_type = StatusType.DUPLICATE
|
||||
for row in rows:
|
||||
log.info(f"found {row[0]} with {found_file}")
|
||||
except mariadb.Error as error:
|
||||
log.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:
|
||||
status.id = rows[0][0]
|
||||
if rows[0][0] == found_file.stem:
|
||||
status.status_type = StatusType.CLOUD_LINK_ID
|
||||
else:
|
||||
status.status_type = StatusType.CLOUD_LINK
|
||||
except mariadb.Error as error:
|
||||
log.debug(f'select failed with {error}')
|
||||
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']
|
||||
log.debug(f"Status: {response.status_code}")
|
||||
if response.status_code == 200:
|
||||
status.status_type = StatusType.FILE_ID
|
||||
status.id = response.json()['id']
|
||||
return status
|
||||
|
||||
def rename_files_to_id(media_dir, dry_run, log):
|
||||
def rename_files_to_id(media_dir, dry_run, conn, log):
|
||||
media_path = Path(media_dir)
|
||||
cursor = conn.cursor()
|
||||
for file in media_path.iterdir():
|
||||
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}")
|
||||
status: FileStatus = get_status_of_file(file, cursor, log)
|
||||
file_id = status.id
|
||||
if not file_id:
|
||||
log.info(f"ID of file {file.name} is unknown")
|
||||
continue
|
||||
new_file_path = file.with_name(f"{file_id}{file.suffix}")
|
||||
match status.status_type:
|
||||
case StatusType.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, dry_run)
|
||||
rename_file(file, new_file_path, dry_run, log)
|
||||
update_cloud_link(file_id, new_file_path, conn, dry_run, log)
|
||||
case StatusType.FILE_ID:
|
||||
log.info(f'status of {file.name} is file_id')
|
||||
update_cloud_link(file_id, new_file_path, dry_run)
|
||||
update_cloud_link(file_id, new_file_path, conn, dry_run, log)
|
||||
case StatusType.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, dry_run)
|
||||
rename_file(file, new_file_path, dry_run, log)
|
||||
update_cloud_link(file_id, new_file_path, conn, dry_run, log)
|
||||
case StatusType.CLOUD_LINK_ID:
|
||||
log.debug(f'status of {file.name} is cloud_link_id')
|
||||
update_cloud_link(file_id, new_file_path, dry_run)
|
||||
update_cloud_link(file_id, new_file_path, conn, dry_run, log)
|
||||
case StatusType.DUPLICATE:
|
||||
log.info(f'status of {file.name} is duplicate')
|
||||
case StatusType.UNKNOWN:
|
||||
log.info(f'status of {file.name} is unknown')
|
||||
case _:
|
||||
log.info(f'status of {file.name} is not defined')
|
||||
|
||||
def rename_file(current_file, new_file_path, dry_run):
|
||||
def rename_file(current_file, new_file_path, dry_run, log):
|
||||
if dry_run:
|
||||
logger.info('rename file {} to {}'.format(current_file.name, new_file_path.name))
|
||||
log.info('rename file {} to {}'.format(current_file.name, new_file_path.name))
|
||||
else:
|
||||
current_file.rename(Path(new_file_path))
|
||||
|
||||
def update_cloud_link(file_id, file_path, conn, dry_run):
|
||||
def update_cloud_link(file_id, file_path, conn, dry_run, log):
|
||||
cursor = conn.cursor()
|
||||
logger.debug(f'update entry {file_id} with {file_path.absolute()}')
|
||||
log.debug(f'update entry {file_id} with {file_path.absolute()}')
|
||||
if dry_run:
|
||||
logger.info(f'UPDATE media_file: cloud_link={file_path.absolute()}')
|
||||
log.debug(f'UPDATE media_file: cloud_link={file_path.absolute()}')
|
||||
else:
|
||||
cursor.execute('UPDATE media_file SET cloud_link="{}" WHERE id="{}"'.format(file_path.absolute(), file_id))
|
||||
conn.commit()
|
||||
|
||||
def reset_cloud_link(conn, dry_run):
|
||||
def reset_cloud_link(conn, dry_run, log):
|
||||
cursor = conn.cursor()
|
||||
if dry_run:
|
||||
logger.info('UPDATE media_file SET cloud_link=""')
|
||||
log.info('UPDATE media_file SET cloud_link=""')
|
||||
else:
|
||||
cursor.execute('UPDATE media_file SET cloud_link="" WHERE id is NOT NULL')
|
||||
conn.commit()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
logger = get_logger(args.verbose, args.config)
|
||||
logger.info("kontor.check_kontor started")
|
||||
logger.info("kontor.check_kontor.rename_files_to_id")
|
||||
rename_files_to_id(args.dir, args.dry_run, logger)
|
||||
log = get_logger(args.verbose, args.config)
|
||||
log.info("kontor.check_kontor started")
|
||||
_, m_conn = get_database_cursors(log, args.config)
|
||||
log.info("kontor.check_kontor.rename_files_to_id")
|
||||
rename_files_to_id(args.dir, args.dry_run, m_conn, log)
|
||||
#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")
|
||||
@@ -120,5 +142,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)
|
||||
logger.info("kontor.check_kontor finished")
|
||||
log.info("kontor.check_kontor finished")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user