Reverse engineering and modify the existing Python script for new purposes

I'm trying to reverse engineer and modify the python script radarrsync to work in it.

Their new purpose is to retrieve a list of two separate radars via api, compare them for matches and the & # 39; tmdbid, the & # 39; hasFile & # 39; value (true / false), the & # 39; 39; id & # 39; and the & # 39; path & # 39; values ​​of the slaves radar from any matches and then send a delete request with the & # 39; id & # 39; if only & # 39; tmdbid & # 39; match and use the modified path value to send a removal command to the actual file over the delete request & # 39; hasFile & # 39; value == true.

If I did that & # 39; hasFile & # 39; comment out, the script will run, but the error message "message": "MethodNotAllowed" and will not delete anything (although trying to delete everything in the list) and when running with the & # 39; hasFile & # 39; if statement it exits early as it is unable to evaluate it.

Can someone look and see what I have missed please

import os
Import logging
Import Json
Import sys
import requests
import the configparser
import argparse
Import Shutil
import time

parser = argparse.ArgumentParser (description = & # 39; RadarrClean Compare two radar servers and remove matches from the slave. & # 39;
parser.add_argument (& # 39; - config & 39 ;, action = "store", type = str, help = & # 39; Location of the configuration file. & # 39;
parser.add_argument (& # 39; - debug & # 39 ;, help = & # 39; Enable debug logging. & # 39 ;, action = "store_true")
parser.add_argument (& # 39; - whatif & # 39 ;, help = "Read only What would happen if I did that No posts are sent Use with --debug", action = "store_true")
args = parser.parse_args ()

def ConfigSectionMap (section):
dict1 = {}
options = Config.options (section)
for option in options:
To attempt:
dict1[option] = Config.get (section, option)
if dict1[option] == -1:
logger.debug (% option "skip:% s")
except:
print ("Exception% s!"%)
dict1[option] = None
Return dict1

Config = configparser.ConfigParser ()
settingsFilename = os.path.join (os.getcwd (), & # 39; Config.txt & # 39;
if args.config:
settingsFilename = args.config
elif not os.path.isfile (settingsFilename):
print ("Create default configuration, please edit and run again.")
shutil.copyfile (os.path.join (os.getcwd (), & # 39; config.default & # 39 ;, settingsfilename))
sys.exit (0)
Config.read (settingsfilename)

print (ConfigSectionMap (& # 39; Radarr_PQ & # 39;)['rootfolders'].Teilt (# 39 & # 39 ;;;))

######################################## ####### ## ############################################# #### #################
logger = logging.getLogger ()
if ConfigSectionMap ("General")['log_level'] == & # 39; DEBUG & # 39 ;:
logger.setLevel (logging.DEBUG)
elif ConfigSectionMap ("General")['log_level'] == & # 39; VERBOSE & # 39 ;:
logger.setLevel (logging.VERBOSE)
otherwise:
logger.setLevel (logging.INFO)
if args.debug:
logger.setLevel (logging.DEBUG)

logFormatter = logging.Formatter ("% (asctime) s) [%(threadName)-12.12s] [%(levelname)-5.5s]        %(Messages")

fileHandler = logging.FileHandler (ConfigSectionMap (& # 39; General & # 39;)['log_path'], & # 39; w & # 39 ;, utf-8 & # 39;)
fileHandler.setFormatter (logFormatter)
logger.addHandler (fileHandler)

consoleHandler = logging.StreamHandler (sys.stdout)
consoleHandler.setFormatter (logFormatter)
logger.addHandler (consoleHandler)
######################################## ####### ## ############################################# #### #################

Session = Requirements. Session ()
session.trust_env = false

radarr_url = ConfigSectionMap ("RadarrMaster")['url']
radarr_key = ConfigSectionMap ("RadarrMaster")['key']
radarrMovies = session.get (& # 39; {0} / api / movie? apikey = {1} & # 39; format (radarr_url, radarr_key))
If radarrMovies.status_code! = 200:
logger.error (& # 39; master radar server error - response {} & # 39; format (radarrMovies.status_code))
sys.exit (0)

Server = {}
for section in Config.sections ():
section = str (section)
if "Radarr_" in section:
server = (str.split (section & # 39; radar_ & # 39;))[1]
        server[server] = ConfigSectionMap (section)
movies = session.get (Format "{0} / api / movie? apikey = {1}". (Server[server]['url'], Server[server]['key']))
If movies.status_code! = 200:
logger.error (& # 39; {0} radar server error - response {1} & # 39;. format (server, movies.status_code))
sys.exit (0)
otherwise:
server[server]['movies']    = []
            server[server]['matchMovies']    = 0
server[server]['searchid']    = []
            for movie in movies.json ():
server[server]['movies'].append (movie['tmdbId'])

for a movie in radarrMovies.json ():
for name, server in servers.items ():
if movie['tmdbId'] in the server['movies']:
if movie['hasFile'] == "true":
if movie['sizeOnDisk'] ! = 0:
if & # 39; rootfolders & # 39; on the server:
allowedFolders = Server['rootfolders'].Teilt (# 39 ;; & # 39)
for folders in allowed folders:
if not folder in the movie['path']:
continue
if & # 39; local_path & # 39; on the server:
Path = str (movie['path']) .replace (server['local_path']server['cloud_path'])
logging.debug (& # 39; Updating the movie path from {0} to {1} & # 39;). Format (movie['path']Path))
otherwise:
Path = movie['path']
                    logging.debug (& # 39; server: {0} & # 39; format (name))
logging.debug (& # 39; title: {0} & # 39;. format (movie['title']))
logging.debug (& # 39; hasFile: {0} & # 39;. format (movie['hasFile']))
logging.debug (& # 39; tmdbId: {0} & # 39; format (movie['tmdbId']))
logging.debug (& # 39; id: {0} & # 39; .. format (movie['id']))
logging.debug (& # 39; path: {0} & # 39;. format (path))

Payload = {str (film['id'])}

logging.debug (& # 39; payload: {0} & # 39;. format (payload))
server['matchMovies'] + = 1
if args.whatif:
logging.debug (# 39; WhatIf: The movie is not really removed from Radarr {0}. & # 39; format (name))
otherwise:
if server['matchMovies'] > 0:
logging.debug (Sleeping for: {0} seconds. & # 39;. format (ConfigSectionMap (& # 39; General & # 39;))['wait_between_delete']))
time.sleep (int (ConfigSectionMap (& # 39; General & # 39;))['wait_between_delete']))
r = session.delete (& # 39; {0} / api / movie? apikey = {1} & # 39;. format (server['url']server['key']), data = json.dumps (payload))
Remove logger.info (& # 39; {0} from Radarr {1} server & # 39;) .format (Movie['title'], Surname))

Example of the type of values ​​in the file that are retrieved by the call to the API

        {
"title": "Anaconda",
"sizeOnDisk": 1289110670
"downloaded": true,
"hasFile": true,
"Path": "/ movies / Anaconda (1997)",
"profileId": 4,
"tmdbId": 9360
"titleSlug": "anaconda-9360",
"id": 206
},
{
"title": "Analyze that",
"sizeOnDisk": 628338045
"downloaded": true,
"hasFile": true,
"path": "/ movies / Analyze That (2002)",
"profileId": 4,
"tmdbId": 9932
"titleSlug": "analyze-that-9932",
"id": 207
},
{
"title": "Analyze that",
"sizeOnDisk": 756275693
"downloaded": true,
"hasFile": true,
"path": "/ movies / Analyze This (1999)",
"profileId": 4,
"tmdbId": 9535
"titleSlug": "analysis-this-9535",
"id": 208
},