Kometa-Team / ImageMaid

Python 3 Script for Cleaning Up Images in Plex
MIT License
297 stars 16 forks source link

ImageMaid

GitHub release (latest by date) Docker Image Version (latest semver) Docker Pulls Develop GitHub commits since latest stable release (by SemVer)

Discord Reddit Wiki GitHub Sponsors Sponsor or Donate

Your Plex directories are growing out of control. You use overlays from Kometa or upload lots of custom art from Title Card Maker (TCM) that you no longer want to use or need to eliminate. You don't want to perform the plex dance if you can avoid it. This script can free up gigs of space....

As well as being able to clean the PhotoTranscoder Directory and running the Plex operations Empty Trash, Clean Bundles, and Optimize DB.

Special Thanks to bullmoose20 for the original Plex Bloat Fix (PBF) Script this is based on.

This image shows which photos would be removed. Red is removed, Green is kept because it is the actively selected poster. The other two come standard from Plex when the posters are retrieved so the script will not touch those either:

Installing ImageMaid

Generally, ImageMaid can be installed in one of two ways:

  1. Running on a system as a Python script [we will refer to this as a "local" install]
  2. Running as a Docker container

GENERALLY SPEAKING, running as a Docker container is simpler, as you won't have to be concerned about installing Python, or support libraries, or any possible system conflicts generated by those actions.

For this reason, it's generally recommended that you install via Docker rather than directly on the host.

If you have some specific reason to avoid Docker, or you prefer running it as a Python script for some particular reason, then this general recommendation is not aimed at you. It's aimed at someone who doesn't have an existing compelling reason to choose one over the other.

Install Walkthroughs

There are no detailed walkthroughs specifically for ImageMaid but the process is extremely similar to how you would do it with Kometa.

Local Install Overview

ImageMaid requires Python 3.11 or later. Later versions may function but are untested.

These are high-level steps which assume the user has knowledge of python and pip, and the general ability to troubleshoot issues.

  1. Clone or download and unzip the repo.
git clone https://github.com/Kometa-Team/ImageMaid
  1. move into the ImageMaid directory.
cd ImageMaid
  1. Install dependencies (it is recommended to do this is a virtual environment):
pip install -r requirements.txt
  1. If the above command fails, run the following command:
pip install -r requirements.txt --ignore-installed

At this point ImageMaid has been installed, and you can verify installation by running:

python imagemaid.py

Docker Install Overview

Docker Run:

docker run -v <PATH_TO_CONFIG>:/config:rw -v <PATH_TO_PLEX>:/plex:rw kometateam/imagemaid

Example Docker Run command:

These docs are assuming you have a basic understanding of Docker concepts. One place to get familiar with Docker would be the official tutorial.

docker run -v "X:\Media\ImageMaid\config:/config:rw" -v "X:\Plex Media Server:/plex:rw" kometateam/imagemaid

Docker Compose:

Example Docker Compose file:

services:
  imagemaid:
    image: kometateam/imagemaid
    container_name: imagemaid
    environment:
      - TZ=TIMEZONE #optional
    volumes:
      - /path/to/config:/config
      - /path/to/plex:/plex
    restart: unless-stopped

Dockerfile

A Dockerfile is included within the GitHub repository for those who require it, although this is only suggested for those with knowledge of dockerfiles. The official ImageMaid build is available on the Dockerhub Website.

Usage

When running ImageMaid, make sure that you are not running any tools which may touch posters, backgrounds or title card images - namely Kometa or TitleCardMaker.

It is recommended to schedule ImageMaid after the above tools or Plex's Scheduled Tasks.

An example schedule would be:

Tips

Global Options

ImageMaid has multiple Global Options to change how it runs these are set in 3 different ways listed in priority order:

  1. Setting the Environment Variable.

  2. Adding the Environment Variables to config/.env

    • example.env is included as an example but is not read by the script it will only read a file specifically called .env.
  3. Use the Shell Command when launching.

Example .env File

PLEX_PATH=C:\Plex Media Server
MODE=report
SCHEDULE=
PLEX_URL=http://192.168.1.12:32400
PLEX_TOKEN=123456789
DISCORD=https://discord.com/api/webhooks/###################/####################################################################
TIMEOUT=600
SLEEP=60
IGNORE_RUNNING=False
LOCAL_DB=False
USE_EXISTING=False
PHOTO_TRANSCODER=False
EMPTY_TRASH=False
CLEAN_BUNDLES=False
OPTIMIZE_DB=False
TRACE=False
LOG_REQUESTS=False

Base Options

Plex Path

The only required Option is the Plex Path Option which is the Plex Config Directory containing the servers Metadata including Cache, Metadata, and Plug-in Support.

To set the Plex Path for the run:

Mode

How ImageMaid runs depends on the Mode Option that's currently set for that run.

To set the Global Mode for the run:

Database

The script needs to query the server's plex database to make sure it doesn't remove actively selected images.

Download From Plex API

By default, the script will expect to connect to your Plex Server to download the Database using your Plex URL and Plex Token Options (Finding a Token).

Copy From Local

Alternatively the database can be copied from your local config directory you supplied in the Plex Path Option by using the Local DB Option.

IMPORTANT! When Copying the Local Database, it is recommended to restart Plex before running this script and to make sure Plex is idle.

Restarting allows for all temp SQLite files to be written to the primary Plex DB ensuring that all currently selected posters are properly known and preserved.

The script will not run when the temp SQLite files are found. To ignore this error, use the Ignore Running Option.

Use Existing

A previously downloaded or copied database can be used if it's less than 2 hours old by using the Use Existing Option. If the database is more than 2 hours old a new one will be downloaded or copied.

Other Operations

In addition to cleaning the Plex Metadata Directory for custom images the script can clean out your PhotoTranscoder Directory, Empty Trash, Clean Bundles, and Optimize DB.

Photo Transcoder

Empty Trash

Clean Bundles

Optimize DB

Other Options

Discord URL

Discord Webhook URL to send notifications to.

Timeout

Connection Timeout in seconds that's greater than 0.

Sleep

Sleep Timer between Empty Trash, Clean Bundles, and Optimize DB in seconds that's greater than 0 .

Trace

Run with extra trace logs.

Log Requests

Run with every request and file action logged.

Continuous Schedule

ImageMaid can be run either immediately or on a schedule. The default behavior is to run immediately to run using a schedule simply pass in the Schedule Option.

Add a Schedule Block to the Schedule Option to run ImageMaid using a continuous schedule.

Schedule Blocks

Schedule Blocks define how and when the script will run.

Each Schedule Blocks has 2 required parts (time and frequency) and 1 optional part (options) all separated with a |. (Example: time|frequency or time|frequency|options)

You can have multiple Schedule Blocks separated with a , (time|frequency,time|frequency|options).

Schedule Block Parts

Schedule Block Example

SCHEDULE=08:00|weekly(sunday)|mode=clear,09:00|weekly(sunday)|mode=move,10:00|monthly(1)|mode=nothing;photo-transcoder=true

The example above is detailed out below to better explain how it works: