1
0

add more sort exception handlings

This commit is contained in:
= 2024-12-30 01:19:50 -05:00
parent 1a486ba9d9
commit e78d05f8b4
3 changed files with 49 additions and 14 deletions

View File

@ -1,9 +1,9 @@
#!/bin/sh
REGISTRY1=code.balsillie.net
REGISTRY2=quay.io
REGISTRY2=registry.balsillie.house
NAMESPACE1=michael/containers
NAMESPACE2=balsillie
# NAMESPACE2=balsillie
IMAGE=$1
TAG1=$(date +%Y-%m-%d_%H-%M-%S)
@ -12,8 +12,8 @@ TAG2=latest
docker buildx build \
--tag $REGISTRY1/$NAMESPACE1/$IMAGE:$TAG1 \
--tag $REGISTRY1/$NAMESPACE1/$IMAGE:$TAG2 \
--tag $REGISTRY2/$NAMESPACE2/$IMAGE:$TAG1 \
--tag $REGISTRY2/$NAMESPACE2/$IMAGE:$TAG2 \
--tag $REGISTRY2/$IMAGE:$TAG1 \
--tag $REGISTRY2/$IMAGE:$TAG2 \
--file ./$IMAGE/Dockerfile \
--push \
./$IMAGE

View File

@ -1,17 +1,25 @@
FROM python:3.12-slim
# Set the working directory
WORKDIR /app
# Create working directory
RUN mkdir -p /app
# Copy the sort.py file to the working directory
COPY sort.py .
COPY requirements.txt .
# Create and set user
RUN useradd -u 1000 -d /app -UM appuser
# Install ffmpeg
RUN apt-get update \
&& apt-get install -y ffmpeg \
&& apt-get clean
# Chown appuser home
RUN chown -R appuser:appuser /app && chmod 750 /app
# Switch to appuser
USER appuser
# Set the working directory
WORKDIR /app
# Ensure pip
RUN python -m ensurepip
@ -19,7 +27,11 @@ RUN python -m ensurepip
RUN pip install --upgrade pip
# Install requirements
RUN pip install -r requirements.txt
COPY requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
# Copy the sort.py file to the working directory
COPY sort.py /app/sort.py
# Set the entry point to sort.py
ENTRYPOINT ["python", "/app/sort.py"]

View File

@ -5,6 +5,7 @@ import signal
import sys
import json
import ffmpeg
import shutil # For moving files
from os import getenv
from pathlib import Path
from PIL import Image, ExifTags, UnidentifiedImageError
@ -115,6 +116,8 @@ def sort_photos(*,
exif = image.getexif()
except OSError:
print(f"File {photo.name} could not be opened (permissions?), skipping.")
ignore_list.append({"name": photo.name, "reason": "Permissions error"})
ignore_count += 1
continue
except UnidentifiedImageError:
print(f"File {photo.name} is not a valid image file. Ignoring.")
@ -123,9 +126,21 @@ def sort_photos(*,
continue
timestamp = None
if ExifTags.Base.DateTimeOriginal in exif.keys():
timestamp = time.strptime(exif[ExifTags.Base.DateTimeOriginal], "%Y:%m:%d %H:%M:%S")
try:
timestamp = time.strptime(exif[ExifTags.Base.DateTimeOriginal], "%Y:%m:%d %H:%M:%S")
except:
print(f"File {photo.name} has invalid timestamp format, ignoring.")
ignore_list.append({"name": photo.name, "reason": "Invalid exif timestamp"})
ignore_count += 1
continue
elif ExifTags.Base.DateTime in exif.keys():
timestamp = time.strptime(exif[ExifTags.Base.DateTime], "%Y:%m:%d %H:%M:%S")
try:
timestamp = time.strptime(exif[ExifTags.Base.DateTime], "%Y:%m:%d %H:%M:%S")
except:
print(f"File {photo.name} has invalid timestamp format, ignoring.")
ignore_list.append({"name": photo.name, "reason": "Invalid exif timestamp"})
ignore_count += 1
continue
if timestamp is None:
print(f"File {photo.name} does not have an exif timestamp, ignoring.")
ignore_list.append({"name": photo.name, "reason": "No exif timestamp"})
@ -155,6 +170,8 @@ def sort_videos(*,
meta_dict: dict = ffmpeg.probe(video)
except OSError:
print(f"File {video.name} could not be opened (permissions?), skipping.")
ignore_list.append({"name": video.name, "reason": "Permissions error"})
ignore_count += 1
continue
except ffmpeg.Error:
print(f"General error, file {video.name} could not be probed by ffmpeg, ignoring.")
@ -165,7 +182,13 @@ def sort_videos(*,
if "format" in meta_dict.keys():
if "tags" in meta_dict["format"].keys():
if "creation_time" in meta_dict["format"]["tags"].keys():
timestamp = time.strptime(meta_dict["format"]["tags"]["creation_time"], "%Y-%m-%dT%H:%M:%S.%fZ")
try:
timestamp = time.strptime(meta_dict["format"]["tags"]["creation_time"], "%Y-%m-%dT%H:%M:%S.%fZ")
except:
print(f"{video.name} has invalid timestamp format, ignoring.")
ignore_list.append({"name": video.name, "reason": "Invalid creation_time format"})
ignore_count += 1
continue
if timestamp is None:
print(f"File {video.name} does not have creation_time metadata, ignoring.")
ignore_list.append({"name": video.name, "reason": "No creation_time metadata"})
@ -207,7 +230,7 @@ def rename_file(*,
# Move the file
if not new_path.exists():
print(f"Moving {file.as_posix()} to {new_path.as_posix()}")
file.rename(new_path)
shutil.move(file, new_path)
sort_count += 1
else:
print(f"File {new_file_name} already exists, ignoring.")