Skip to content

Instantly share code, notes, and snippets.

@kpatdev
Last active October 23, 2024 16:41
Show Gist options
  • Select an option

  • Save kpatdev/c23767fb8a196838d946a69aa8bc3b1b to your computer and use it in GitHub Desktop.

Select an option

Save kpatdev/c23767fb8a196838d946a69aa8bc3b1b to your computer and use it in GitHub Desktop.

Revisions

  1. kpatdev revised this gist Oct 23, 2024. 1 changed file with 18 additions and 10 deletions.
    28 changes: 18 additions & 10 deletions plex_empty_trash.py
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,6 @@
    import os
    from plexapi.server import PlexServer

    # Assumes all folders are inside /mnt/nfs
    # Tries to mount/remount broken mounts for systems that allow it

    # Replace with your actual Plex server details
    PLEX_URL = 'http://<plex_server_ip>:32400'
    PLEX_TOKEN = '<plex_x_token>'
    @@ -14,15 +11,26 @@ def check_folders():
    """Checks if the specified folders are mounted."""
    for folder in FOLDERS:
    folder_path = os.path.join(MOUNT_PATH, folder)
    if not os.listdir(folder_path):
    print(f"Folder not mounted: {folder_path}")
    try:
    mounted = os.listdir(folder_path)
    if not mounted:
    print(f"Folder not mounted: {folder_path}")
    os.system(f"sudo umount {folder_path}")
    os.system(f"sudo mount {folder_path}")
    for _ in range(2):
    if os.path.exists(folder_path):
    break
    os.system(f"sudo umount {folder_path}")
    os.system(f"sudo mount {folder_path}")
    else:
    return False
    except:
    print(f"Unmounting and remounting (stale file): {folder_path}")
    os.system(f"sudo umount {folder_path}")
    os.system("sudo mount -av")
    os.system(f"sudo mount {folder_path}")
    for _ in range(2):
    if os.path.exists(folder_path):
    break
    os.system(f"sudo umount {folder_path}")
    os.system("sudo mount -av")
    if os.path.exists(folder_path):
    break
    else:
    return False
    return True
  2. kpatdev created this gist Oct 21, 2024.
    47 changes: 47 additions & 0 deletions plex_empty_trash.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    import os
    from plexapi.server import PlexServer

    # Assumes all folders are inside /mnt/nfs
    # Tries to mount/remount broken mounts for systems that allow it

    # Replace with your actual Plex server details
    PLEX_URL = 'http://<plex_server_ip>:32400'
    PLEX_TOKEN = '<plex_x_token>'
    MOUNT_PATH = '/mnt/nfs'
    FOLDERS = ['movies', 'tvshows', 'homevideos', 'youtube']

    def check_folders():
    """Checks if the specified folders are mounted."""
    for folder in FOLDERS:
    folder_path = os.path.join(MOUNT_PATH, folder)
    if not os.listdir(folder_path):
    print(f"Folder not mounted: {folder_path}")
    os.system(f"sudo umount {folder_path}")
    os.system("sudo mount -av")
    for _ in range(2):
    if os.path.exists(folder_path):
    break
    os.system(f"sudo umount {folder_path}")
    os.system("sudo mount -av")
    else:
    return False
    return True

    def empty_trash(plex):
    """Empties the trash for all libraries."""
    for section in plex.library.sections():
    try:
    section.emptyTrash()
    print(f"Emptied trash for library: {section.title}")
    except Exception as e:
    print(f"Failed to empty trash for {section.title}: {e}")

    if __name__ == "__main__":
    if check_folders():
    try:
    plex = PlexServer(PLEX_URL, PLEX_TOKEN)
    empty_trash(plex)
    except Exception as e:
    print(f"Error connecting to Plex server: {e}")
    else:
    print("Plex media directory not mounted. Skipping trash cleanup.")