Skip to content

Commit

Permalink
fix: root dir index errors in mass add table and api, as well as refa…
Browse files Browse the repository at this point in the history
…ctoring along the way

Signed-off-by: miigotu <miigotu@gmail.com>
  • Loading branch information
miigotu committed Feb 28, 2024
1 parent 545ffc8 commit 86bb764
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 266 deletions.
10 changes: 5 additions & 5 deletions sickchill/gui/slick/views/displayShow.mako
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%inherit file="/layouts/main.mako" />
<%!
import datetime
from urllib.parse import quote
from urllib.parse import quote, urljoin
from sickchill import settings
from sickchill.oldbeard import subtitles, notifiers, scdatetime, network_timezones, helpers
Expand Down Expand Up @@ -548,10 +548,10 @@
% if settings.DOWNLOAD_URL and epResult['location']:
<%
filename = epResult['location']
for rootDir in settings.ROOT_DIRS.split('|'):
if rootDir.startswith('/'):
filename = filename.replace(rootDir, "")
filename = settings.DOWNLOAD_URL + quote(filename)
for rootDir in settings.ROOT_DIRS.split('|')[1:]:
if filename.startswith(rootDir):
filename = filename.replace(rootDir, "").lstrip("/\\")
filename = urljoin(settings.DOWNLOAD_URL, quote(filename))
%>
<a href="${filename}">${_('Download')}</a>
% endif
Expand Down
5 changes: 4 additions & 1 deletion sickchill/oldbeard/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os.path
import platform
import re
from pathlib import Path
from urllib import parse

import rarfile
Expand Down Expand Up @@ -300,7 +301,9 @@ def change_unpack_dir(unpack_dir):
return True

if os.path.normpath(settings.UNPACK_DIR) != os.path.normpath(unpack_dir):
if bool(settings.ROOT_DIRS) and any(helpers.is_subdirectory(unpack_dir, rd) for rd in settings.ROOT_DIRS.split("|")[1:]):
if settings.ROOT_DIRS and any(
Path(root_directory).resolve() in Path(unpack_dir).resolve().parents for root_directory in settings.ROOT_DIRS.split("|")[1:]
):
# don't change if it's in any of the TV root directories
logger.info("Unable to change unpack directory to a sub-directory of a TV root dir")
return False
Expand Down
25 changes: 0 additions & 25 deletions sickchill/oldbeard/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,31 +950,6 @@ def has_hidden_attribute(filepath):
return False


def real_path(path):
"""
Returns:
the canonicalized absolute pathname. The resulting path will have no symbolic link, '/./' or '/../' components.
"""
return os.path.normpath(os.path.normcase(os.path.realpath(path)))


def is_subdirectory(subdir_path, topdir_path):
"""
Returns true if a subdir_path is a subdirectory of topdir_path
else otherwise.
Parameters:
subdir_path: The full path to the subdirectory
topdir_path: The full path to the top directory to check subdir_path against
"""
topdir_path = real_path(topdir_path)
subdir_path = real_path(subdir_path)

# checks if the common prefix of both is equal to directory
# e.g. /a/b/c/d.rst and directory is /a/b, the common prefix is /a/b
return os.path.commonprefix([subdir_path, topdir_path]) == topdir_path


def set_up_anidb_connection():
"""Connect to anidb"""

Expand Down
52 changes: 32 additions & 20 deletions sickchill/oldbeard/processTV.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,31 @@ def delete_folder(folder, check_empty=True):
return: True on success, False on failure
"""

folder = Path(folder).resolve()
# check if it's a folder
if not os.path.isdir(folder):
if not folder.is_dir():
return False

# check if it isn't TV_DOWNLOAD_DIR
if settings.TV_DOWNLOAD_DIR and helpers.real_path(folder) == helpers.real_path(settings.TV_DOWNLOAD_DIR):
if settings.TV_DOWNLOAD_DIR and str(Path(folder).resolve()) == str(Path(settings.TV_DOWNLOAD_DIR).resolve()):
return False

# check if it's empty folder when wanted to be checked
if check_empty:
check_files = os.listdir(folder)
if check_files:
logger.info(f"Not deleting folder {folder} found the following files: {check_files}")
found_files = [file for file in folder.iterdir()]
if found_files:
logger.info(f"Not deleting folder {folder} found the following files: {found_files}")
return False

try:
logger.info(f"Deleting folder (if it's empty): {folder}")
os.rmdir(folder)
folder.rmdir()
except (OSError, IOError) as error:
logger.warning(f"Warning: unable to delete folder: {folder}: {error}")
return False
else:
try:
logger.info("Deleting folder: " + folder)
logger.info(f"Deleting folder: {folder}")
shutil.rmtree(folder)
except (OSError, IOError) as error:
logger.warning(f"Warning: unable to delete folder: {folder}: {error}")
Expand All @@ -82,27 +83,28 @@ def delete_files(process_path, unwanted_files, result, force=False):
elif not result.result:
return

process_path = Path(process_path)
# Delete all file not needed
for cur_file in unwanted_files:
cur_file_path = os.path.join(process_path, cur_file)
if not os.path.isfile(cur_file_path):
for current_file in unwanted_files:
file_path = process_path / current_file
if not file_path.is_file():
continue # Prevent error when a notwantedfiles is an associated files

result.output += log_helper(f"Deleting file: {cur_file}", logger.DEBUG)
result.output += log_helper(f"Deleting file: {current_file}", logger.DEBUG)

# check first the read-only attribute
file_attribute = os.stat(cur_file_path)[0]
file_attribute = file_path.stat()[0]
if not file_attribute & stat.S_IWRITE:
# File is read-only, so make it writeable
result.output += log_helper(f"Changing ReadOnly Flag for file: {cur_file}", logger.DEBUG)
result.output += log_helper(f"Changing ReadOnly Flag for file: {current_file}", logger.DEBUG)
try:
os.chmod(cur_file_path, stat.S_IWRITE)
file_path.chmod(stat.S_IWRITE)
except OSError as error:
result.output += log_helper(f"Cannot change permissions of {cur_file_path}: {error}", logger.DEBUG)
result.output += log_helper(f"Cannot change permissions of {current_file}: {error}", logger.DEBUG)
try:
os.remove(cur_file_path)
file_path.unlink(True)
except OSError as error:
result.output += log_helper(f"Unable to delete file {cur_file}: {error}", logger.DEBUG)
result.output += log_helper(f"Unable to delete file {current_file}: {error}", logger.DEBUG)


def log_helper(message, level=logging.INFO):
Expand Down Expand Up @@ -132,13 +134,17 @@ def process_dir(process_path, release_name=None, process_method=None, force=Fals

# if the client and SickChill are not on the same machine translate the directory into a network directory
elif all(
[settings.TV_DOWNLOAD_DIR, os.path.isdir(settings.TV_DOWNLOAD_DIR), os.path.normpath(process_path) == os.path.normpath(settings.TV_DOWNLOAD_DIR)]
[
settings.TV_DOWNLOAD_DIR,
Path(settings.TV_DOWNLOAD_DIR).is_dir(),
str(Path(process_path).resolve()) == str(Path(settings.TV_DOWNLOAD_DIR).resolve()),
]
):
process_path = os.path.join(settings.TV_DOWNLOAD_DIR, os.path.abspath(process_path).split(os.path.sep)[-1])
result.output += log_helper(f"Trying to use folder: {process_path} ", logger.DEBUG)

# if we didn't find a real dir then quit
if not os.path.isdir(process_path):
if not Path(process_path).is_dir():
result.output += log_helper(
"Unable to figure out what folder to process. "
"If your downloader and SickChill aren't on the same PC "
Expand All @@ -162,6 +168,8 @@ def process_dir(process_path, release_name=None, process_method=None, force=Fals
result.output += log_helper(_("Processing {process_path}").format(process_path=process_path))
generator_to_use = os.walk(process_path, followlinks=settings.PROCESSOR_FOLLOW_SYMLINKS)

rar_files = []

for current_directory, directory_names, filenames in generator_to_use:
result.result = True

Expand Down Expand Up @@ -273,7 +281,11 @@ def validate_dir(process_path, release_name, failed, result):
result.missed_files.append(f"{process_path} : Failed download")
return False

if settings.TV_DOWNLOAD_DIR and helpers.real_path(process_path) != helpers.real_path(settings.TV_DOWNLOAD_DIR) and helpers.is_hidden_folder(process_path):
if (
settings.TV_DOWNLOAD_DIR
and str(Path(process_path).resolve()) != str(Path(settings.TV_DOWNLOAD_DIR).resolve())
and helpers.is_hidden_folder(process_path)
):
result.output += log_helper(f"Ignoring hidden folder: {process_path}", logger.DEBUG)
if not process_path.endswith("@eaDir"):
result.missed_files.append(f"{process_path} : Hidden folder")
Expand Down
2 changes: 1 addition & 1 deletion sickchill/oldbeard/traktChecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ def _add_show_with_defaults(indexer, indexer_id, name, status):
root_dirs = settings.ROOT_DIRS.split("|")

try:
location = root_dirs[int(root_dirs[0]) + 1]
location = root_dirs[int(root_dirs[0])]
except Exception:
location = None

Expand Down
4 changes: 3 additions & 1 deletion sickchill/show/indexers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Union

from .handler import ShowIndexer

indexer = None
indexer: Union[ShowIndexer, None] = None
2 changes: 1 addition & 1 deletion sickchill/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,7 @@ def save_config():
"metadata_mede8er": settings.METADATA_MEDE8ER,
"backlog_days": int(settings.BACKLOG_DAYS),
"backlog_missing_only": int(settings.BACKLOG_MISSING_ONLY),
"root_dirs": settings.ROOT_DIRS if settings.ROOT_DIRS else "",
"root_dirs": settings.ROOT_DIRS or "",
"tv_download_dir": settings.TV_DOWNLOAD_DIR,
"keep_processed_dir": int(settings.KEEP_PROCESSED_DIR),
"process_method": settings.PROCESS_METHOD,
Expand Down
10 changes: 9 additions & 1 deletion sickchill/tv.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,11 @@ def load_imdb_info(self):

if not self.imdb_id:
# TODO: Load tvmaze/tvdb info into other imdb_info fields
self.imdb_id = helpers.imdb_from_tvdbid_on_tvmaze(self.indexerid)
# noinspection PyBroadException
try:
self.imdb_id = helpers.imdb_from_tvdbid_on_tvmaze(self.indexerid)
except Exception:
self.imdb_id = None

try:
client = Cinemagoer()
Expand Down Expand Up @@ -941,6 +945,8 @@ def load_imdb_info(self):

logger.debug(f"{self.indexerid}: Loading show info from IMDb")
imdb_title: dict = client.get_movie(self.imdb_id.strip("t"))
if not imdb_title:
return

self.imdb_info = {
"indexer_id": self.indexerid,
Expand All @@ -959,6 +965,8 @@ def load_imdb_info(self):
}

logger.debug(f"{self.indexerid}: Obtained info from IMDb ->{self.imdb_info}")
except KeyError:
logger.info(f"Could not get IMDB info for {self.name}")
except (
TypeError,
ValueError,
Expand Down

0 comments on commit 86bb764

Please sign in to comment.