This repository has been archived by the owner on Apr 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoTrash.py
executable file
·97 lines (84 loc) · 4.36 KB
/
AutoTrash.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
import sys
import subprocess
from os import path
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from plexapi.server import PlexServer
from discord_webhook import DiscordWebhook, DiscordEmbed
# Please ensure that the "anchor.lock" file is available in your mount's root (can change the path to anything)
# It is recommended to use the path using MergerFS as then the anchor file will be unavailable if either services go down
# -----------------------------------------------------------------------------
# Input Plex URL and token
baseurl = 'http:https://[PMS_IP_Address]:32400'
token = 'TOKEN'
# Path starts from home, this one is ~/MergerFS/anchor.lock
anchorPath = "/MergerFS/anchor.lock"
# Leave empty to not use Discord for logging. Paste in the webhook URL if it is in use.
DiscordWebhookURL = ''
# Use your Discord User ID to allow pinging by the webhook during failures
DiscordUserID = ''
# Parse command line arguments
# -----------------------------------------------------------------------------
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument("-s", "--shell", action='store_true', help="Invoke a shell, used for cron")
parser.add_argument("-q", "--quiet", action='store_true', help="Suppress Discord notification on success")
args = parser.parse_args()
# This is used to check the the rclone and MergerFS services are running on the machine
# -----------------------------------------------------------------------------
# Pass the "-s" or "--shell" argument to be use this script with crontab
rcloneCheck = subprocess.call(["systemctl", "--user", "--quiet", "is-active", "rclone-vfs.service"], shell=args.shell)
mergerFSCheck = subprocess.call(["systemctl", "--user", "--quiet", "is-active", "mergerfs.service"], shell=args.shell)
# Plex URL (SSL recommended), and Anchor file location
# -----------------------------------------------------------------------------
try:
plex = PlexServer(baseurl, token)
plexStatus = ':white_check_mark: Online'
except:
plexStatus = ':x: Offline'
# Edit this line to change the path of the mount to start from root instead
anchor = path.exists(path.expanduser('~') + anchorPath)
# Script
# -----------------------------------------------------------------------------
scriptStatus = 'Failed'
if anchor:
anchorStatus = ':white_check_mark: Available'
# The subprocess.call returns a 0 if the service is active
if rcloneCheck == 0 and mergerFSCheck == 0:
serviceStatus = ':white_check_mark: Active'
# The API call to empty the trash for all the libraries
if plexStatus == ':white_check_mark: Online':
plex.library.emptyTrash()
scriptStatus = 'Success'
else:
serviceStatus = ':x: Inactive'
else:
anchorStatus = ':x: Unavailable'
if rcloneCheck == 0 and mergerFSCheck == 0:
serviceStatus = ':white_check_mark: Active'
else:
serviceStatus = ':x: Inactive'
# This bit is for the discord embed, useful for logging during crontab use
if DiscordWebhookURL:
webhookAvatar = 'https://github.com/specarino/AutoTrash/blob/main/assets/AutoTrash-128px.png?raw=True'
defaultWebhook = DiscordWebhook(url=DiscordWebhookURL, username='AutoTrash', avatar_url=webhookAvatar, content=f"Empty Trash Task: **{scriptStatus}**!")
mentionWebhook = DiscordWebhook(url=DiscordWebhookURL, username='AutoTrash', avatar_url=webhookAvatar, content=f"<@{DiscordUserID}> Empty Trash Task: **{scriptStatus}**!")
def printe(scriptStatus):
webhook = defaultWebhook
if scriptStatus == 'Success':
if args.quiet:
sys.exit()
embedColor = '03b2f8'
else:
embedColor = 'f84903'
if DiscordUserID:
webhook = mentionWebhook
titleFull = f"Empty Trash Task: {scriptStatus}!"
embed = DiscordEmbed(title=titleFull, description="Automatic emptying of trash for Plex based on remote mount's availability", color=embedColor)
embed.set_author(name='specarino/AutoTrash', url='https://github.com/specarino/AutoTrash/', icon_url='https://github.com/specarino.png?size=48')
embed.set_timestamp()
embed.add_embed_field(name="Plex Media Server", value=plexStatus, inline=False)
embed.add_embed_field(name="Anchor File (through MergerFS)", value=anchorStatus, inline=False)
embed.add_embed_field(name="rclone & MergerFS Services", value=serviceStatus, inline=False)
webhook.add_embed(embed)
webhook.execute()
printe(scriptStatus)