-
-
Save saitoh183/6b778b2ebb408b9e5a63afcb0dfbceb3 to your computer and use it in GitHub Desktop.
plexupdate
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/env python | |
import os | |
import subprocess | |
from subprocess import STDOUT,PIPE,Popen | |
import logging | |
from logging.handlers import RotatingFileHandler | |
import psutil | |
import shutil | |
from time import sleep | |
import string | |
import random | |
#logging: | |
log_filename = "/logs/PlexUpdate.log" | |
logger = logging.getLogger('PlexUpdate') | |
logger.setLevel(logging.INFO) | |
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
fh = RotatingFileHandler(log_filename, maxBytes=100000, backupCount=20) | |
fh.setFormatter(formatter) | |
logger.addHandler(fh) | |
console = logging.StreamHandler() | |
console.setLevel(logging.DEBUG) | |
console.setFormatter(formatter) | |
logger.addHandler(console) | |
#Check if folder has content | |
def get_child(path): | |
return [item for item in os.listdir(path) if item not in ('.stignore', '.stfolder')] | |
#Declare variables | |
source = "/plexupdate" | |
backup = "/plextemp" | |
childcount = len(get_child(source)) | |
process = True | |
#Main script | |
def main(): | |
os.environ['LD_LIBRARY_PATH'] = "/usr/lib/plexmediaserver" | |
os.environ['PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR'] = "/plexdata/Library/Application Support" | |
#Process each .txt file found in source folder | |
for p, d, f in os.walk(source): | |
if not childcount: | |
logger.info('There is nothing to process in {}.'.format(source)) | |
logger.info('-----------------------------------------------------------------') | |
continue | |
gen = [file_ for file_ in f if file_ not in ('.stignore', '.stfolder')] #ignore these files | |
for file_ in gen: | |
logger.info('Files found in {}.'.format(source)) | |
current_file = os.path.join(p, file_) | |
logger.info('Processing file {}.'.format(file_)) | |
with open(current_file,'r') as f2: | |
for line in f2: | |
line_no_return = line.rstrip('\n') | |
#Set Library section number based on library name found in file | |
logger.info('LINE: {}.'.format(line_no_return)) | |
if 'FrenchTV' in line_no_return: | |
libsection = 4 | |
logger.info('Section: {}.'.format(libsection)) | |
elif 'TV' in line_no_return: | |
libsection = 8 | |
logger.info('Section: {}.'.format(libsection)) | |
elif 'UFC' in line_no_return: | |
libsection = 3 | |
logger.info('Section: {}.'.format(libsection)) | |
elif 'French Movies' in line_no_return: | |
libsection = 2 | |
logger.info('Section: {}.'.format(libsection)) | |
elif 'Dual Audio Movies' in line_no_return: | |
libsection = 5 | |
logger.info('Section: {}.'.format(libsection)) | |
else: | |
libsection = 7 | |
logger.info('Section: {}.'.format(libsection)) | |
plexscan = '"/usr/lib/plexmediaserver/Plex Media Scanner" --scan --refresh --section %s --directory "/media/gdrive_encrypted/%s"' %(libsection,line_no_return) | |
Plex = subprocess.Popen(plexscan, shell=True, stdout=PIPE).communicate()[0] | |
logger.info('Processing Ended...') | |
logger.info('{}'.format(Plex)) | |
logger.info('{}'.format(plexscan)) | |
sleep(60) | |
# Move txt files to backup folder incase i need them to update because something didnt work. | |
try: | |
logger.info('Backing up file to {}'.format(backup)) | |
shutil.move(current_file,backup) | |
logger.info('-----------------------------------------------------------------') | |
except: | |
logger.info('file {} exists in backup location. Going to rename current file'.format(file_)) | |
newname = os.path.join(backup,file_+ random.choice(string.letters)) | |
os.rename(current_file,newname) | |
#Check if plex scanner is already running | |
while process: | |
for proc in psutil.process_iter(): | |
process = False | |
try: | |
if proc.name() == "Plex Media Scanner": | |
logger.info('{} is running'.format(proc.name())) | |
sleep(30) | |
process = True | |
except psutil.NoSuchProcess: | |
logger.info('Plex Media Scanner is not running') | |
continue | |
except psutil.AccessDenied: | |
print "Permission error or access denied on process" | |
continue | |
else: | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment