2021-05-08 14:25:29 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
|
|
|
import os
|
|
|
|
import requests
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from config import settings, url_sonarr, url_radarr
|
|
|
|
from helper import path_mappings
|
2021-05-29 02:45:54 +00:00
|
|
|
from database import TableShowsRootfolder, TableMoviesRootfolder, TableShows, TableMovies
|
2021-05-08 14:25:29 +00:00
|
|
|
|
|
|
|
headers = {"User-Agent": os.environ["SZ_USER_AGENT"]}
|
|
|
|
|
|
|
|
|
|
|
|
def get_sonarr_rootfolder():
|
|
|
|
apikey_sonarr = settings.sonarr.apikey
|
|
|
|
sonarr_rootfolder = []
|
|
|
|
|
|
|
|
# Get root folder data from Sonarr
|
|
|
|
url_sonarr_api_rootfolder = url_sonarr() + "/api/rootfolder?apikey=" + apikey_sonarr
|
|
|
|
|
|
|
|
try:
|
|
|
|
rootfolder = requests.get(url_sonarr_api_rootfolder, timeout=60, verify=False, headers=headers)
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
logging.exception("BAZARR Error trying to get rootfolder from Sonarr. Connection Error.")
|
|
|
|
return []
|
|
|
|
except requests.exceptions.Timeout:
|
|
|
|
logging.exception("BAZARR Error trying to get rootfolder from Sonarr. Timeout Error.")
|
|
|
|
return []
|
|
|
|
except requests.exceptions.RequestException:
|
|
|
|
logging.exception("BAZARR Error trying to get rootfolder from Sonarr.")
|
|
|
|
return []
|
|
|
|
else:
|
2021-05-29 02:45:54 +00:00
|
|
|
sonarr_movies_paths = list(TableShows.select(TableShows.path).dicts())
|
2021-05-08 14:25:29 +00:00
|
|
|
for folder in rootfolder.json():
|
2021-05-29 02:45:54 +00:00
|
|
|
if any(item['path'].startswith(folder['path']) for item in sonarr_movies_paths):
|
|
|
|
sonarr_rootfolder.append({'id': folder['id'], 'path': folder['path']})
|
2021-05-26 20:47:14 +00:00
|
|
|
db_rootfolder = TableShowsRootfolder.select(TableShowsRootfolder.id, TableShowsRootfolder.path).dicts()
|
2021-05-08 14:25:29 +00:00
|
|
|
rootfolder_to_remove = [x for x in db_rootfolder if not
|
|
|
|
next((item for item in sonarr_rootfolder if item['id'] == x['id']), False)]
|
|
|
|
rootfolder_to_update = [x for x in sonarr_rootfolder if
|
|
|
|
next((item for item in db_rootfolder if item['id'] == x['id']), False)]
|
|
|
|
rootfolder_to_insert = [x for x in sonarr_rootfolder if not
|
|
|
|
next((item for item in db_rootfolder if item['id'] == x['id']), False)]
|
|
|
|
|
|
|
|
for item in rootfolder_to_remove:
|
2021-05-26 20:47:14 +00:00
|
|
|
TableShowsRootfolder.delete().where(TableShowsRootfolder.id == item['id']).execute()
|
2021-05-08 14:25:29 +00:00
|
|
|
for item in rootfolder_to_update:
|
2021-05-26 20:47:14 +00:00
|
|
|
TableShowsRootfolder.update({TableShowsRootfolder.path: item['path']})\
|
|
|
|
.where(TableShowsRootfolder.id == item['id'])\
|
|
|
|
.execute()
|
2021-05-08 14:25:29 +00:00
|
|
|
for item in rootfolder_to_insert:
|
2021-05-26 20:47:14 +00:00
|
|
|
TableShowsRootfolder.insert({TableShowsRootfolder.id: item['id'], TableShowsRootfolder.path: item['path']})\
|
|
|
|
.execute()
|
2021-05-08 14:25:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_sonarr_rootfolder():
|
|
|
|
get_sonarr_rootfolder()
|
2021-05-26 20:47:14 +00:00
|
|
|
rootfolder = TableShowsRootfolder.select(TableShowsRootfolder.id, TableShowsRootfolder.path).dicts()
|
2021-05-08 14:25:29 +00:00
|
|
|
for item in rootfolder:
|
|
|
|
if not os.path.isdir(path_mappings.path_replace(item['path'])):
|
2021-05-26 20:47:14 +00:00
|
|
|
TableShowsRootfolder.update({TableShowsRootfolder.accessible: 0,
|
|
|
|
TableShowsRootfolder.error: 'This Sonarr root directory does not seems to '
|
|
|
|
'be accessible by Bazarr. Please check path '
|
|
|
|
'mapping.'})\
|
|
|
|
.where(TableShowsRootfolder.id == item['id'])\
|
|
|
|
.execute()
|
2021-05-08 14:25:29 +00:00
|
|
|
elif not os.access(path_mappings.path_replace(item['path']), os.W_OK):
|
2021-05-26 20:47:14 +00:00
|
|
|
TableShowsRootfolder.update({TableShowsRootfolder.accessible: 0,
|
|
|
|
TableShowsRootfolder.error: 'Bazarr cannot write to this directory.'}) \
|
|
|
|
.where(TableShowsRootfolder.id == item['id']) \
|
|
|
|
.execute()
|
2021-05-08 14:25:29 +00:00
|
|
|
else:
|
2021-05-26 20:47:14 +00:00
|
|
|
TableShowsRootfolder.update({TableShowsRootfolder.accessible: 1,
|
|
|
|
TableShowsRootfolder.error: ''}) \
|
|
|
|
.where(TableShowsRootfolder.id == item['id']) \
|
|
|
|
.execute()
|
2021-05-08 14:25:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_radarr_rootfolder():
|
|
|
|
apikey_radarr = settings.radarr.apikey
|
|
|
|
radarr_rootfolder = []
|
|
|
|
|
|
|
|
# Get root folder data from Radarr
|
|
|
|
url_radarr_api_rootfolder = url_radarr() + "/api/rootfolder?apikey=" + apikey_radarr
|
|
|
|
|
|
|
|
try:
|
|
|
|
rootfolder = requests.get(url_radarr_api_rootfolder, timeout=60, verify=False, headers=headers)
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
logging.exception("BAZARR Error trying to get rootfolder from Radarr. Connection Error.")
|
|
|
|
return []
|
|
|
|
except requests.exceptions.Timeout:
|
|
|
|
logging.exception("BAZARR Error trying to get rootfolder from Radarr. Timeout Error.")
|
|
|
|
return []
|
|
|
|
except requests.exceptions.RequestException:
|
|
|
|
logging.exception("BAZARR Error trying to get rootfolder from Radarr.")
|
|
|
|
return []
|
|
|
|
else:
|
2021-05-29 02:45:54 +00:00
|
|
|
radarr_movies_paths = list(TableMovies.select(TableMovies.path).dicts())
|
2021-05-08 14:25:29 +00:00
|
|
|
for folder in rootfolder.json():
|
2021-05-29 02:45:54 +00:00
|
|
|
if any(item['path'].startswith(folder['path']) for item in radarr_movies_paths):
|
|
|
|
radarr_rootfolder.append({'id': folder['id'], 'path': folder['path']})
|
2021-05-26 20:47:14 +00:00
|
|
|
db_rootfolder = TableMoviesRootfolder.select(TableMoviesRootfolder.id, TableMoviesRootfolder.path).dicts()
|
2021-05-08 14:25:29 +00:00
|
|
|
rootfolder_to_remove = [x for x in db_rootfolder if not
|
|
|
|
next((item for item in radarr_rootfolder if item['id'] == x['id']), False)]
|
|
|
|
rootfolder_to_update = [x for x in radarr_rootfolder if
|
|
|
|
next((item for item in db_rootfolder if item['id'] == x['id']), False)]
|
|
|
|
rootfolder_to_insert = [x for x in radarr_rootfolder if not
|
|
|
|
next((item for item in db_rootfolder if item['id'] == x['id']), False)]
|
|
|
|
|
|
|
|
for item in rootfolder_to_remove:
|
2021-05-26 20:47:14 +00:00
|
|
|
TableMoviesRootfolder.delete().where(TableMoviesRootfolder.id == item['id']).execute()
|
2021-05-08 14:25:29 +00:00
|
|
|
for item in rootfolder_to_update:
|
2021-05-26 20:47:14 +00:00
|
|
|
TableMoviesRootfolder.update({TableMoviesRootfolder.path: item['path']})\
|
|
|
|
.where(TableMoviesRootfolder.id == item['id']).execute()
|
2021-05-08 14:25:29 +00:00
|
|
|
for item in rootfolder_to_insert:
|
2021-05-26 20:47:14 +00:00
|
|
|
TableMoviesRootfolder.insert({TableMoviesRootfolder.id: item['id'],
|
|
|
|
TableMoviesRootfolder.path: item['path']}).execute()
|
2021-05-08 14:25:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_radarr_rootfolder():
|
|
|
|
get_radarr_rootfolder()
|
2021-05-26 20:47:14 +00:00
|
|
|
rootfolder = TableMoviesRootfolder.select(TableMoviesRootfolder.id, TableMoviesRootfolder.path).dicts()
|
2021-05-08 14:25:29 +00:00
|
|
|
for item in rootfolder:
|
|
|
|
if not os.path.isdir(path_mappings.path_replace_movie(item['path'])):
|
2021-05-26 20:47:14 +00:00
|
|
|
TableMoviesRootfolder.update({TableMoviesRootfolder.accessible: 0,
|
|
|
|
TableMoviesRootfolder.error: 'This Radarr root directory does not seems to '
|
|
|
|
'be accessible by Bazarr. Please check path '
|
|
|
|
'mapping.'}) \
|
|
|
|
.where(TableMoviesRootfolder.id == item['id']) \
|
|
|
|
.execute()
|
2021-05-08 14:25:29 +00:00
|
|
|
elif not os.access(path_mappings.path_replace_movie(item['path']), os.W_OK):
|
2021-05-26 20:47:14 +00:00
|
|
|
TableMoviesRootfolder.update({TableMoviesRootfolder.accessible: 0,
|
|
|
|
TableMoviesRootfolder.error: 'Bazarr cannot write to this directory'}) \
|
|
|
|
.where(TableMoviesRootfolder.id == item['id']) \
|
|
|
|
.execute()
|
2021-05-08 14:25:29 +00:00
|
|
|
else:
|
2021-05-26 20:47:14 +00:00
|
|
|
TableMoviesRootfolder.update({TableMoviesRootfolder.accessible: 1,
|
|
|
|
TableMoviesRootfolder.error: ''}) \
|
|
|
|
.where(TableMoviesRootfolder.id == item['id']) \
|
|
|
|
.execute()
|