Skip to content

Commit

Permalink
fix summary report
Browse files Browse the repository at this point in the history
  • Loading branch information
meisnate12 committed Feb 19, 2023
1 parent abc7f76 commit e8e0428
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 44 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ When running Plex Image Cleanup, make sure that you are not running any tools wh
It is recommended to schedule Plex Image Cleanup after the above tools or Plex's Scheduled Tasks.

An example schedule would be:
00:00-02:00 - TitleCardMaker
02:00-05:00 - Plex Scheduled Tasks
05:00-07:00 - Plex Meta Manager
07:00-09:00 - Plex Image Cleanup
* 00:00-02:00 - TitleCardMaker
* 02:00-05:00 - Plex Scheduled Tasks
* 05:00-07:00 - Plex Meta Manager
* 07:00-09:00 - Plex Image Cleanup

### Tips

Expand Down Expand Up @@ -206,7 +206,7 @@ Alternatively the database can be copied from your local config folder you suppl
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 this use the `Ignore Running` Option.
The script will not run when the temp SQLite files are found. To ignore this error, use the `Ignore Running` Option.
* **Environment Variable:** `IGNORE_RUNNING=True`
* **Shell Command:** `-i` or `--ignore`
Expand All @@ -218,9 +218,6 @@ A previously downloaded or copied database can be used if it's less than 2 hours
* **Environment Variable:** `USE_EXISTING=True`
* **Shell Command:** `-e` or `--existing`
Plex Image Cleanup 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](#continuous-schedule).
### 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.
Expand Down Expand Up @@ -279,6 +276,8 @@ Run with every request and file action logged.
### Continuous Schedule
Plex Image Cleanup 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 Plex Image Cleanup using a continuous schedule.
* **Shell Command:** `-sc` or `--schedule "05:00|weekly(sunday)"`
Expand Down Expand Up @@ -311,6 +310,8 @@ You can have multiple Schedule Blocks separated with a `,` (`time|frequency,time
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:
* Run at 8:00 AM on Sundays with the Options: `mode: clear`
* `08:00|weekly(sunday)|mode=remove`
* `time |frequency |options`
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.9
0.9.10
75 changes: 41 additions & 34 deletions plex_image_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,38 @@ def pic_thread(attrs):

def run_plex_image_cleanup(attrs):
logger.header(pmmargs, sub=True, discord_update=True)
logger.separator()
logger.separator("Validating Options", space=False, border=False)
do_transcode = attrs["photo-transcoder"] if "photo-transcoder" in attrs else pmmargs["photo-transcoder"]
do_trash = attrs["empty-trash"] if "empty-trash" in attrs else pmmargs["empty-trash"]
do_bundles = attrs["clean-bundles"] if "clean-bundles" in attrs else pmmargs["clean-bundles"]
do_optimize = attrs["optimize-db"] if "optimize-db" in attrs else pmmargs["optimize-db"]
mode = attrs["mode"].lower() if "mode" in attrs else pmmargs["mode"].lower()
description = f"Running in {mode.capitalize()} Mode"
extras = []
if do_trash:
extras.append("Empty Trash")
if do_bundles:
extras.append("Clean Bundles")
if do_optimize:
extras.append("Optimize DB")
if do_transcode:
extras.append("PhotoTrancoder")
if extras:
description += f" with {', '.join(extras[:-1])}{', and ' if len(extras) > 1 else ''}{extras[-1]} set to True"
logger.info(description)
report = []
messages = []
try:
logger.info("Validating Options")
try:
logger.info("Script Started", log=False, discord=True)
except Failed as e:
logger.error(e)

# Check Mode
if mode not in modes:
raise Failed(f"Mode Error: {mode} Invalid. Options: \n\t{mode_descriptions}")
logger.info(f"{mode.capitalize()}: {modes[mode]['desc']}")

# Check Plex Path
if not pmmargs["plex"]:
if not os.path.exists(os.path.join(base_dir, "plex")):
Expand All @@ -122,10 +140,6 @@ def run_plex_image_cleanup(attrs):
f' Should contain "Cache", "Metadata", and "Plug-in Support"\n'
f' Contents:\n {contents}')

# Check Mode
mode = attrs["mode"].lower() if "mode" in attrs else pmmargs["mode"].lower()
if mode not in modes:
raise Failed(f"Mode Error: {mode} Invalid. Options: \n\t{mode_descriptions}")

# Delete PhotoTranscoder
if do_transcode:
Expand Down Expand Up @@ -181,12 +195,11 @@ def plex_connect():
except Exception as e1:
logger.error(e1)
raise

return plex_connect()
plex_server = plex_connect()
logger.info("Successfully Connected to Plex")
return plex_server

if mode != "nothing":
logger.info(f"Running in {mode} mode. {modes[mode]['desc']}")

if os.path.exists(restore_dir):
match mode:
case "restore":
Expand Down Expand Up @@ -233,12 +246,8 @@ def plex_connect():
logger.info(f"Space Recovered: {space}")
logger.info(f"Runtime: {logger.runtime()}")
report.append([("Removing PIC Restore Bloat Images", "")])
report.append([
("Space Recovered", space),
("Files Removed", len(del_paths)),
("Scan Time", f"{logger.runtime('scanning')}"),
("Restore Time", f"{logger.runtime('work')}")
])
report.append([("Space Recovered", space),("Files Removed", len(del_paths))])
report.append([("Scan Time", f"{logger.runtime('scanning')}"), ("Restore Time", f"{logger.runtime('work')}")])
case _:
logger.error(f"{mode} mode invalid while the PIC Restore Folder exists.", discord=True, rows=[
[("PIC Path", restore_dir)],
Expand Down Expand Up @@ -289,11 +298,12 @@ def plex_connect():
else:
logger.warning(f"Existing Database not found {'making' if pmmargs['local'] else 'downloading'} a new copy")

report.append([("Database", "")])
fields = []
if is_usable:
report.append([("Plex Database", "Using Existing Database")])
report.append([("", "Using Existing Database")])
else:
report.append([("Plex Database", f"{'Copied' if pmmargs['local'] else 'Downloaded'} New Database")])
report.append([("", f"{'Copied' if pmmargs['local'] else 'Downloaded'} New Database")])
if os.path.exists(dbpath):
os.remove(dbpath)
if os.path.exists(temp_dir):
Expand All @@ -303,9 +313,10 @@ def plex_connect():
logger.info(f"Copying database from {os.path.join(databases_dir, plex_db_name)}", start="database")
util.copy_with_progress(os.path.join(databases_dir, plex_db_name), dbpath, description=f"Copying database file to: {dbpath}")
else:
logger.info("Downloading Database via the API. Plex will This will take some time... To see progress, log into Plex and\n"
"go to Settings | Manage | Console and filter on Database. You can also look at the Plex Dashboard\n"
"to see the progress of the Database backup.", start="database")
logger.info("Downloading Database via the API. Plex will This will take some time... To see progress, log into\n"
"Plex and go to Settings | Manage | Console and filter on Database.\n"
"You can also look at the Plex Dashboard to see the progress of the Database backup.", start="database")
logger.info()

# fetch the data to be saved
headers = {'X-Plex-Token': server._token}
Expand Down Expand Up @@ -398,17 +409,13 @@ def plex_connect():
logger.debug(message)
else:
logger.trace(message)
logger.info(f"Removing Complete: Removed {len(bloat_paths)} Bloat Images")
logger.info(f"{modes[mode]['ing']} Complete: {modes[mode]['ed']} {len(bloat_paths)} Bloat Images")
space = util.format_bytes(logger["size"])
logger.info(f"Space Recovered: {space}")
logger.info(f"{modes[mode]['space']}: {space}")
logger.info(f"Runtime: {logger.runtime()}")
report.append([("Removing Bloat Images", "")])
report.append([
(modes[mode]["space"], space),
(f"Files {modes[mode]['ed']}", len(bloat_paths)),
("Scan Time", f"{logger.runtime('scanning')}"),
(f"{mode.capitalize()} Time", f"{logger.runtime('work')}")
])
report.append([(f"{modes[mode]['ing']} Bloat Images", "")])
report.append([(modes[mode]["space"], space), (f"Files {modes[mode]['ed']}", len(bloat_paths))])
report.append([("Scan Time", f"{logger.runtime('scanning')}"), (f"{mode.capitalize()} Time", f"{logger.runtime('work')}")])
elif do_trash or do_bundles or do_optimize:
server = get_server()

Expand Down Expand Up @@ -439,11 +446,11 @@ def plex_connect():
logger.debug(message)
logger.stacktrace()
logger.critical(e, discord=True)

logger.error_report()
logger.separator()
logger.info(f"{script_name} Finished\nRun Time: {logger.runtime()}", discord=True)
logger.separator()
logger.switch()
report.append([(f"{script_name} Finished", "")])
report.append([("Total Runtime", f"{logger.runtime()}")])
logger.report(f"{script_name} Summary", description=description, rows=report, discord=True)

if __name__ == "__main__":
try:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ numpy==1.24.2
opencv-python==4.7.0.68
pillow==9.4.0
PlexAPI==4.13.2
pmmutils==0.3.11
pmmutils==0.3.12
requests==2.28.2
retrying==1.3.4
tmdbapis==1.1.0
Expand Down

0 comments on commit e8e0428

Please sign in to comment.