**** INFORMATION IS OLD AND OUT DATED PLEASE REFER TO POST 1
AutoDelete.py - (Auto Delete Watched Shows) Script - 2013 (DOES NOT WORK)
Disclaimer:
USE AT YOUR OWN RISK I WILL NOT BE HELD RESPONSIBLE FOR ANYTHING BAD THAT HAPPENS ***
Also Note: I’m New To Python. So Please Ignore The Messy Code.
I’ll Get Around to Cleaning It Up Later…
What it is it and What Does It Do?This is a Script I Wrote In Python.
This Script is used to look for TV Shows That have been marked as “WATCHED”.
It will then find the “Watched” file and DELETE it to save you some hard drive space and clutter.
- Tested on Ubuntu 12.04 Linux Server using Python 2.7.3
- Tested on Windows 8 Ultimate (64-Bit) using Python 3.3.2Configuring:
Also Briefly Described in The Top Portion Of The AutoDelete.py
If Plex Was Installed with all the default settings, you do not need to configure ANYTHING.
You may need to adjust the “Section” Variable if you have multiple librarys like TV Shows, Movies, Audio, Etc
You may want to adjust the “Shows” Variable if you have Shows, Movies, Audio, That You Don’t Want Delete (Verifies Against Full File Path).
If you want to save the OnDeck Shows from being Deleted, Set “OnDeck” = “1”;
Understanding the Configuration:
http : // :/library/sections//recenlyViewed
= IP Address or Host name of the Server (You can also use 127.0.0.1 for localhost)
= Port For Plex Web Server (Default is 32400)
= KEY for the Section You Want This Script To Look At
**You can See what Sections are Available by going to: http : // 192.168.X.X:32400/library/sections/
Looking at the TITLE= and the associated KEY=
It Is Using the XML Data Provided By Your Plex Web Server.
The Number “1” might vary for you.
For my new plex server I have only setup tv shows (no movies section yet) so since I created tv shows first it was given the index of 1.
On my old plex server (windows) I setup tv shows second so it was given the index of 2.
Easy way to tell whats what… Go to the URL and see if the data you are looking at is TV shows or whatever content you are wishing to remove.Notes:
Delete = “”
Noting will be deleted - Safe for Review and Testing
Delete = “1”
Your files will be deleted… Please Don’t Cry
Linux Users (Ubuntu):sudo apt-get -y install python nano (typically skip this step it should already be installed)
python -V (CAPITAL V - Check the Version - I use 2.7.3)
touch AutoDelete.py
chmod +x AutoDelete.py
nano AutoDelete.py (OR YOUR FAVORITE TEXT EDITOR)
Copy & Paste Contents
PRESS: (CTRL + X) answer YES to Save and WriteOut the file)
To run it:
./AutoDelete.py
Or Dump It To A Log File:
./AutoDelete.py >> AutoDelete.log
note the period then then slash followed by the file name.
Then you’ll need to setup a schedule task using cronjob (google it, if you really need help then ask).
Windows User:Download and Install Python:
Windows x86 MSI Installer (3.3.2) (sig) and Visual Studio debug information files (sig)
or
Windows X86-64 MSI Installer (3.3.2) [1] (sig) and Visual Studio debug information files (sig)
Note its installation location:
Mine Was: C:\Python33
CREATE A BAT FIle using Notepad or whatever…
AutoDelete.BAT
SET PythonEXE=“C:\Python33\Python.exe”
SET AutoDelete=“C:\Python33\AutoDelete.py”
SET LOGFILE=C:\Python33\AutoDelete.log"
%PythonEXE% %AutoDelete%>>%LOGFILE%
Then get your AutoDelete.py script and put in the location and modify the 3 above variables accordingly.
I recommended a bat file as it easiest to use when setting up a scheduled task.
Code for AutoDelete.py:SECOND POST
**** INFORMATION IS OLD AND OUT DATED PLEASE REFER TO POST 1 ***
AutoDelete.py
NOTE: If Plex Was Installed With The Default Setting
- No Configuration Are Needed
You may want to configure the following though:
- Section (What Section 1,2,3 Contains Your TV Shows, Mine is 2) – ( If You A Few Different Libraries EG. Shows, Movies, Etc…)
- Shows (What Shows NOT to do Delete)
- OnDeck (Do Not Delete Show If “On Deck”)
Download or Copy/Paste Below
#!/usr/bin/python
####################################################################################
## INFORMATION
####################################################################################
## Developed by: Steven Johnson
## Last Updated: 11/13/2013 4:00AM PST
##
## Description: Auto-Delete Watched Items In Plex
## Required Configurations:
## - PC (Blank = AutoDetect | W = Windows | L = Linux/Unix/MAC )
## - Host (Hostname or IP | Blank = 127.0.0.1)
## - Port (Port | Blank = 32400)
## - Section (Section aka Library 1 2 3 varies on your system | Blank = 1)
## - Delete (1 = Delete | Blank = 0 (For Testing or Review))
## - Shows ( [“Show1”,“Show2”]; = Kept Shows OR [""]; = DEL ALL SHOWS )
## - OnDeck ( 1 = Keep If On Deck | Blank = Delete Regardless if onDeck )
####################################################################################
####################################################################################
PC = “”;
Host = “”;
Port = “”;
Section = “”;
Delete = “”;
Shows = [""];
OnDeck = “”;
####################################################################################
## NO NEED TO EDIT BELOW THIS LINE
####################################################################################
import os
import xml.dom.minidom
import platform
import re
####################################################################################
## Checking URL
####################################################################################
if Host=="":
Host=“127.0.0.1”
if Port=="":
Port=“32400”
if Section=="":
Section = “1”
URL = (“http://” + Host + “:” + Port + “/library/sections/” + Section + “/recentlyViewed”)
OnDeckURL = (“http://” + Host + “:” + Port + “/library/sections/” + Section + “/onDeck”)
print("----------------------------------------------------------------------------")
print(" Detected Settings")
print("----------------------------------------------------------------------------")
print("Host: " + Host)
print("Port: " + Port)
print("Section: " + Section)
print("URL: " + URL)
print("OnDeck URL: " + OnDeckURL)
####################################################################################
## Checking Shows
####################################################################################
NoDelete = " | "
ShowCount = len(Shows)
print("Show Count: " + str(ShowCount))
for Show in Shows:
Show = re.sub(’[^A-Za-z0-9 ]+’, ‘’, Show).strip()
if Show=="":
NoDelete += "(None Listed) | "
ShowCount -= 1
else:
NoDelete += Show + " | "
print("Number of Shows Detected For Keeping: " + str(ShowCount))
print (“Shows to Keep:” + NoDelete)
###################################################################################
## Checking Delete
####################################################################################
if Delete==“1”:
print(“Delete: Enabled”)
else:
print(“Delete: Disabled - Flagging Only”)
if OnDeck==“1”:
print(“Delete OnDeck: No”)
else:
print(“Delete OnDeck: Yes”)
####################################################################################
## Checking OS
####################################################################################
AD = “”
if PC=="":
AD = “(Auto Detected)”
if platform.system()==“Windows”:
PC = “W”
elif platform.system()==“Linux”:
PC = “L”
elif platform.system()==“Darwin”:
PC = “L”
####################################################################################
## Setting OS Based Variables
####################################################################################
if PC==“L”:
print(“Operating System: Linux " + AD)
import urllib2
doc = xml.dom.minidom.parse(urllib2.urlopen(URL))
deck = xml.dom.minidom.parse(urllib2.urlopen(OnDeckURL))
elif PC==“W”:
print(“Operating System: Windows " + AD)
import urllib.request
doc = xml.dom.minidom.parse(urllib.request.urlopen(URL))
deck = xml.dom.minidom.parse(urllib.request.urlopen(OnDeckURL))
else:
print(“Operating System: ** Not Configured ** (” + platform.system() + “) is not recognized.”)
exit()
print(”----------------------------------------------------------------------------”)
print("----------------------------------------------------------------------------")
print("")
FileCount = 0
DeleteCount = 0
FlaggedCount = 0
OnDeckCount = 0
ShowsCount = 0
####################################################################################
## Check On Deck
####################################################################################
def CheckOnDeck( CheckDeckFile ):
InTheDeck = 0
for DeckVideoNode in deck.getElementsByTagName(“Video”):
DeckMediaNode = DeckVideoNode.getElementsByTagName(“Media”)
for DeckMedia in DeckMediaNode:
DeckPartNode = DeckMedia.getElementsByTagName(“Part”)
for DeckPart in DeckPartNode:
Deckfile = DeckPart.getAttribute(“file”)
if CheckDeckFile==Deckfile:
InTheDeck += 1
else:
InTheDeck += 0
return InTheDeck
####################################################################################
## Check Shows And Delete If Configured
####################################################################################
def CheckShows( CheckFile ):
global FileCount
global DeleteCount
global FlaggedCount
global OnDeckCount
global ShowsCount
FileCount += 1
CantDelete = 0
ShowFound = “”
## – CHECK SHOWS –
for Show in Shows:
Show = re.sub(’[^A-Za-z0-9 ]+’, ‘’, Show).strip()
if Show=="":
CantDelete = 0
else:
if (’ ’ in Show) == True:
if all(str(Word) in CheckFile for Word in Show.split()):
CantDelete += 1
ShowFound = “[” + Show + “]”
ShowsCount += 1
else:
CantDelete += 0
else:
if Show in CheckFile:
CantDelete += 1
ShowFound = “[” + Show + “]”
ShowsCount += 1
else:
CantDelete += 0
– Check OnDeck –
if OnDeck==“1”:
IsOnDeck = CheckOnDeck(CheckFile);
if IsOnDeck==0:
CantDelete += 0
else:
CantDelete += 1
ShowFound = “[OnDeck]” + ShowFound
OnDeckCount += 1
– DELETE SHOWS –
if CantDelete == 0:
if Delete==“1”:
print("[DELETED] " + CheckFile)
os.remove(file)
DeleteCount += 1
else:
print("[FLAGGED] " + CheckFile)
FlaggedCount += 1
else:
print("[KEEPING]" + ShowFound + " " + CheckFile)
####################################################################################
## Get Files for Watched Shows
####################################################################################
for VideoNode in doc.getElementsByTagName(“Video”):
view = VideoNode.getAttribute(“viewCount”)
if view == ‘’:
view = 0
view = int(view)
MediaNode = VideoNode.getElementsByTagName(“Media”)
for Media in MediaNode:
PartNode = Media.getElementsByTagName(“Part”)
for Part in PartNode:
file = Part.getAttribute(“file”)
if view > 0:
if os.path.isfile(file):
CheckShows(file);
else:
print("##[NOT FOUND] " + file)
####################################################################################
## Check Shows And Delete If Configured
####################################################################################
print("")
print("----------------------------------------------------------------------------")
print("----------------------------------------------------------------------------")
print(" Summary – Script Completed Successfully")
print("----------------------------------------------------------------------------")
print("")
print(" Total File Count " + str(FileCount))
print(" Kept Show Files " + str(ShowsCount))
print(" On Deck Files " + str(OnDeckCount))
print(" Deleted Files " + str(DeleteCount))
print(" Flagged Files " + str(FlaggedCount))
print("")
print("----------------------------------------------------------------------------")
print("----------------------------------------------------------------------------")