Sonarrr with custom variables to update files on Gdrive using pythons script

Hi

Im wanting to see if I can get help in regards to custom variables.

My Setup:
My plex server runs on rclone via NSSM(Windows 10).
- I Have 2 drives, W: and X:
- W: is a full write drive, which all the *arr’s are pointed to so that all the transfers go through that.
- X: is a read-only drive, which Plex is connected to.
For my *arr’s, i have 2 copies of Radarr running, one for normal movies, one exclusive for 4k

Im using a script below which was made in a previous Plex thread in r/plex.
My Dilemma is that I want to use the script to update the certain folder only, not do a total drive scan everytime one of the *arr’s upload. BUT, because i use X: rather than W: for plex, I’m abit lost fo the commands for the filepath. Also, how do i differentiate between the 2 radarr’s?

Below is the code
import datetime
import os
import requests # pip install requests if needed
import urllib.parse

PLEX_HOST = ‘http://localhost:32400
PLEX_TOKEN = ‘HAHAHAHAno’
LOG_ROOT = ‘F:\Logs’
TV_SECTION = 1
MOVIE_SECTION = 2

SONARR_ENV = {
‘type’ : ‘SONARR_EVENTTYPE’,
‘episode_path’ : ‘SONARR_EPISODEFILE_PATH’,
‘series_path’ : ‘SONARR_SERIES_PATH’,
‘expected_events’ : { ‘test’, ‘download’, ‘rename’, ‘episodedeleted’, ‘seriesdeleted’ },
‘section’ : TV_SECTION
}

RADARR_ENV = {
‘type’ : ‘RADARR_EVENTTYPE’,
‘path’ : ‘RADARR_MOVIE_PATH’,
‘expected_events’ : { ‘download’, ‘rename’ },
‘section’ : MOVIE_SECTION
}

def process(env):
if env[‘type’] not in os.environ:
write_error(f’{env[“type”]} not set!’)
return

evt = os.environ[env['type']].lower()
if evt not in env['expected_events']:
	write_error(f'Unexpected event type: {evt}')
	return

path = get_scan_folder(env)
if len(path) == 0:
	return

web = f'{PLEX_HOST}/library/sections/{env["section"]}/refresh?path={urllib.parse.quote(path)}&X-Plex-Token={PLEX_TOKEN}'
result = requests.get(web).status_code
if result != 200:
	write_error(f'Error refreshing path "{path}"')

write_success(path)

def get_scan_folder(env):
if ‘episode_path’ not in env:
if env[‘path’] not in os.environ:
write_error(f’Movie path not set!’)
return ‘’
path = os.environ[env[‘path’]]
if os.path.isdir(path):
return path
return os.path.dirname(path)
if env[‘episode_path’] not in os.environ:
if env[‘series_path’] not in os.environ:
write_error(f’Series path not set!’)
return ‘’
return os.environ[env[‘series_path’]]
return os.path.dirname(os.environ[env[‘episode_path’]])

def write_error(msg):
with open(log_file(True), ‘a+’) as f:
f.write(f’[{datetime.datetime.now()}] {msg}\n{env_vars()}\n’)

def write_success(path):
write_log(f’[{datetime.datetime.now()}] Successfully scanned {path}\n{env_vars()}\n’)

def write_log(msg):
with open(log_file(False), ‘a+’) as f:
f.write(msg)

def log_file(err):
mid = ‘’
ext = ‘.err’ if err else ‘.log’
if SONARR_ENV[‘type’] in os.environ:
mid = ‘sonarr’
elif RADARR_ENV[‘type’] in os.environ:
mid = ‘radarr’
else:
mid = ‘arr’
return os.path.join(LOG_ROOT, mid + ext)

def env_vars():
EV = ‘’
global EV
if len(EV) > 0:
return EV
look = ‘SONARR_’
if RADARR_ENV[‘type’] in os.environ:
look = ‘RADARR_’
for k, v in os.environ.items():
if k.startswith(look):
EV += f’,\n\t\t"{k}" => “{v}”’
if len(EV) > 0:
EV += ‘\n\t’
EV = ‘\tENV: {’ + EV[1:] + ‘}’
return EV

def get_env():
# Default to sonarr, we’ll fail anyway if neither are present
if RADARR_ENV[‘type’] in os.environ:
return RADARR_ENV
return SONARR_ENV

process(get_env())

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.