Created
April 22, 2014 16:32
-
-
Save wolph/11185774 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 = '1' | |
Shows = [''] | |
OnDeck = '' | |
# ############################################################################ | |
# NO NEED TO EDIT BELOW THIS LINE | |
# ############################################################################ | |
import os | |
import xml.dom.minidom | |
import platform | |
import re | |
import urllib | |
# ############################################################################ | |
# Checking URL | |
# ############################################################################ | |
if HOST == '': | |
HOST = '127.0.0.1' | |
if PORT == '': | |
PORT = '32400' | |
if SECTION == '': | |
SECTION = '1' | |
BASE_URL = 'http://%s:%s/library/sections/' % (HOST, PORT) | |
URL = BASE_URL + SECTION + '/recentlyViewed' | |
ON_DECK_URL = BASE_URL + SECTION + '/onDeck' | |
DEEP_SCAN_URL = BASE_URL + 'all/refresh?deep=1' | |
print('---------------------------------------------------------------------') | |
print(' Detected Settings') | |
print('---------------------------------------------------------------------') | |
print('Host: ' + HOST) | |
print('Port: ' + PORT) | |
print('Section: ' + SECTION) | |
print('URL: ' + URL) | |
print('OnDeck URL: ' + ON_DECK_URL) | |
# ############################################################################ | |
# 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(ON_DECK_URL)) | |
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(ON_DECK_URL)) | |
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): | |
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' and confirm_delete(CheckFile): | |
print('**[DELETED] ' + CheckFile) | |
os.remove(file) | |
DeleteCount += 1 | |
else: | |
print('**[FLAGGED] ' + CheckFile) | |
FlaggedCount += 1 | |
else: | |
print('[KEEPING]' + ShowFound + ' ' + CheckFile) | |
def confirm_delete(CheckFile): | |
response = raw_input('Are you sure you want to delete %r?' % CheckFile) | |
return response.lower() in ('y', 'yes', 'true', '1') | |
# ############################################################################ | |
# 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) | |
# ############################################################################ | |
# After deletion, do a deep rescan of all files | |
# ############################################################################ | |
urllib.urlopen(DEEP_SCAN_URL) | |
# ############################################################################ | |
# 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('---------------------------------------------------------------------') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment