mirror of
https://github.com/ManiMatter/decluttarr.git
synced 2026-04-21 00:05:35 +02:00
Initial commit (fork from ManiMatter/mirrarr)
Carved out the queue cleaning bits from mirrarr, without taking over the instance synchronization items. Not tested yet.
This commit is contained in:
47
src/utils/nest_functions.py
Normal file
47
src/utils/nest_functions.py
Normal file
@@ -0,0 +1,47 @@
|
||||
|
||||
async def nested_set(dic, keys, value, matchConditions=None):
|
||||
# Sets the value of a key in a dictionary to a certain value.
|
||||
# If multiple items are present, it can filter for a matching item
|
||||
for key in keys[:-1]:
|
||||
dic = dic.setdefault(key, {})
|
||||
if matchConditions:
|
||||
i = 0
|
||||
match = False
|
||||
for item in dic:
|
||||
for matchCondition in matchConditions:
|
||||
if item[matchCondition] != matchConditions[matchCondition]:
|
||||
match = False
|
||||
break
|
||||
else:
|
||||
match = True
|
||||
if match:
|
||||
dic = dic[i]
|
||||
break
|
||||
i += 1
|
||||
dic[keys[-1]] = value
|
||||
|
||||
|
||||
async def add_keys_nested_dict(d, keys, defaultValue = None):
|
||||
# Creates a nested value if key does not exist
|
||||
for key in keys[:-1]:
|
||||
if key not in d:
|
||||
d[key] = {}
|
||||
d = d[key]
|
||||
d.setdefault(keys[-1], defaultValue)
|
||||
|
||||
async def nested_get(dic, return_attribute, matchConditions):
|
||||
# Retrieves a list contained in return_attribute, found within dic based on matchConditions
|
||||
i = 0
|
||||
match = False
|
||||
hits = []
|
||||
for item in dic:
|
||||
for matchCondition in matchConditions:
|
||||
if item[matchCondition] != matchConditions[matchCondition]:
|
||||
match = False
|
||||
break
|
||||
else:
|
||||
match = True
|
||||
if match:
|
||||
hits.append(dic[i][return_attribute])
|
||||
i += 1
|
||||
return hits
|
||||
72
src/utils/rest.py
Normal file
72
src/utils/rest.py
Normal file
@@ -0,0 +1,72 @@
|
||||
########### Functions to call radarr/sonarr APIs
|
||||
import logging
|
||||
import asyncio
|
||||
import requests
|
||||
from requests.exceptions import RequestException
|
||||
import json
|
||||
from config.config import (TEST_RUN)
|
||||
|
||||
# GET
|
||||
async def rest_get(url, api_key, params=None):
|
||||
try:
|
||||
headers = {'X-Api-Key': api_key} # | {'accept': 'application/json'}
|
||||
response = await asyncio.get_event_loop().run_in_executor(None, lambda: requests.get(url, params=params, headers=headers))
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
except RequestException as e:
|
||||
logging.error(f'Error making API request to {url}: {e}')
|
||||
return None
|
||||
except ValueError as e:
|
||||
logging.error(f'Error parsing JSON response from {url}: {e}')
|
||||
return None
|
||||
|
||||
# DELETE
|
||||
async def rest_delete(url, api_key, params=None):
|
||||
if TEST_RUN: return
|
||||
try:
|
||||
headers = {'X-Api-Key': api_key}
|
||||
response = await asyncio.get_event_loop().run_in_executor(None, lambda: requests.delete(url, params=params, headers=headers))
|
||||
response.raise_for_status()
|
||||
if response.status_code in [200, 204]:
|
||||
return None
|
||||
return response.json()
|
||||
except RequestException as e:
|
||||
logging.error(f'Error making API request to {url}: {e}')
|
||||
return None
|
||||
except ValueError as e:
|
||||
logging.error(f'Error parsing JSON response from {url}: {e}')
|
||||
return None
|
||||
|
||||
# POST
|
||||
async def rest_post(url, api_key, data):
|
||||
if TEST_RUN: return
|
||||
try:
|
||||
headers = {'X-Api-Key': api_key} | {"content-type": "application/json"}
|
||||
response = await asyncio.get_event_loop().run_in_executor(None, lambda: requests.post(url, data=data, headers=headers))
|
||||
response.raise_for_status()
|
||||
if response.status_code == 201:
|
||||
return None
|
||||
return response.json()
|
||||
except RequestException as e:
|
||||
logging.error(f'Error making API request to {url}: {e}')
|
||||
return None
|
||||
except ValueError as e:
|
||||
logging.error(f'Error parsing JSON response from {url}: {e}')
|
||||
return None
|
||||
|
||||
|
||||
# PUT
|
||||
async def rest_put(url, api_key, data):
|
||||
if TEST_RUN: return
|
||||
try:
|
||||
headers = {'X-Api-Key': api_key} | {"content-type": "application/json"}
|
||||
response = await asyncio.get_event_loop().run_in_executor(None, lambda: requests.put(url, data=data, headers=headers))
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
except RequestException as e:
|
||||
logging.error(f'Error making API request to {url}: {e}')
|
||||
return None
|
||||
except ValueError as e:
|
||||
logging.error(f'Error parsing JSON response from {url}: {e}')
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user