Skip to content

Commit

Permalink
Words refactor (#8787)
Browse files Browse the repository at this point in the history
* name refactor, logging

* name refactor, logging

* Mako error/new issue?

* name refactor

* cleanup and layout

* cleanup and layout
  • Loading branch information
BKSteve authored Oct 7, 2024
1 parent 74ce06f commit 5bd179c
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 65 deletions.
2 changes: 1 addition & 1 deletion SickChill.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def start(self):

sys.exit(int(not result)) # Ok -> 0 , Error -> 1
else:
self.log(f"Cannot process this upgrade directory, you have files that don't belong in it. Continuing with startup", 1)
self.log(f"Cannot process this upgrade directory, you have files that don't belong in {upgrade_dir}. Continuing with startup", 1)
sys.exit(1) # Ok -> 0 , Error -> 1

# Load the config and publish it to the oldbeard package
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ exclude = 'contrib/scmaintools|\.venv|venv|\.git|\.hg|\.mypy_cache|\.tox|_build|
[tool.ruff]
line-length = 160
builtins = ["_"]
target-version = 'py310'

[tool.poe.tasks]
pytest = "pytest"
Expand Down
12 changes: 6 additions & 6 deletions sickchill/gui/slick/views/editShow.mako
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@
</div>
<div class="row">
<div class="col-md-12">
<label>${_('search results with one or more words from this list will be ignored.')}</label>
<label><b>${_('note')}:</b> ${_('this option adds to the globally ignored words!')}</label>
<label>${_('search results with one or more words from this list will be ignored.<br/>'
'<b>note:</b> this option adds to the globally ignored words!')}</label>
</div>
</div>
</div>
Expand All @@ -341,8 +341,8 @@
</div>
<div class="row">
<div class="col-md-12">
<label>${_('search results with these words will be preferred in this order.')}</label>
<label><b>${_('note')}:</b> ${_('this option overrides the globally preferred words!')}</label>
<label>${_('search results with these words will be preferred in this order.<br/>'
'<b>note:</b> this option overrides the globally preferred words!')}</label>
</div>
</div>
</div>
Expand All @@ -369,8 +369,8 @@
</div>
<div class="row">
<div class="col-md-12">
<label>${_('search results with no words from this list will be ignored.')}</label>
<label><b>${_('note')}:</b> ${_('this option overrides the globally required words, and globally ignored words!')}</label>
<label>${_('search results with no words from this list will be ignored.<br/>'
'<b>note:</b> this option overrides the globally required words, and globally ignored words!')}</label>
</div>
</div>
</div>
Expand Down
14 changes: 7 additions & 7 deletions sickchill/oldbeard/providers/morethantv.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from sickchill.helper.exceptions import AuthException
from sickchill.oldbeard import tvcache
from sickchill.oldbeard.bs4_parser import BS4Parser
from sickchill.oldbeard.show_name_helpers import allPossibleShowNames
from sickchill.oldbeard.show_name_helpers import all_possible_show_names
from sickchill.providers.torrent.TorrentProvider import TorrentProvider

if TYPE_CHECKING:
Expand Down Expand Up @@ -97,9 +97,9 @@ def process_column_header(td):
if mode != "RSS":
logger.debug(_("Search String: {search_string}").format(search_string=search_string))

searchedSeason = "0"
searched_season = "0"
if mode == "Season":
searchedSeason = re.match(r".*\s(Season\s\d+|S\d+)", search_string).group(1)
searched_season = re.match(r".*\s(Season\s\d+|S\d+)", search_string).group(1)

search_params["searchstr"] = search_string
data = self.get_url(self.urls["search"], params=search_params, returns="text")
Expand Down Expand Up @@ -131,7 +131,7 @@ def process_column_header(td):
if mode == "Season":
# Skip if torrent isn't the right season, we can't search
# for an exact season on MTV, it returns all of them
if searchedSeason not in title:
if searched_season not in title:
continue

# If torrent is grouped, we need a folder name for title
Expand All @@ -142,10 +142,10 @@ def process_column_header(td):
group_params = {"torrentid": torrentid}

# Obtain folder name to use as title
torrentInfo = self.get_url(self.urls["search"], params=group_params, returns="text").replace("\n", "")
torrent_info = self.get_url(self.urls["search"], params=group_params, returns="text").replace("\n", "")

releaseregex = '.*files_{0}.*?;">/(.+?(?=/))'.format(re.escape(torrentid))
releasename = re.search(releaseregex, torrentInfo).group(1)
releasename = re.search(releaseregex, torrent_info).group(1)
title = releasename

download_url = urljoin(self.url, result.find("span", title="Download").parent["href"])
Expand Down Expand Up @@ -185,7 +185,7 @@ def process_column_header(td):
def get_season_search_strings(self, episode: "TVEpisode") -> List[Dict]:
search_string = {"Season": set()}

for show_name in allPossibleShowNames(episode.show, season=episode.scene_season):
for show_name in all_possible_show_names(episode.show, season=episode.scene_season):
season_string = show_name + " "

if episode.show.air_by_date or episode.show.sports:
Expand Down
2 changes: 1 addition & 1 deletion sickchill/oldbeard/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def pick_best_result(results, show):
picked_result = None

# order the list so that preferred releases are at the top
results.sort(key=lambda ep: show_name_helpers.hasPreferredWords(ep.name, ep.show), reverse=True)
results.sort(key=lambda ep: show_name_helpers.has_preferred_words(ep.name, ep.show), reverse=True)

# find the best result for the current episode
for result in results:
Expand Down
50 changes: 25 additions & 25 deletions sickchill/oldbeard/show_name_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
resultFilters.add("(" + settings.IGNORED_SUBS_LIST.replace(",", "|") + ")sub(bed|ed|s)?")


def containsAtLeastOneWord(name, words):
def contains_at_least_one_word(name, words):
"""
Filters out results based on filter_words
Expand Down Expand Up @@ -65,12 +65,12 @@ def clean_set(words):
ignore_words = ignore_words.union(clean_set(show and show.rls_ignore_words or "")) # Show specific ignored words
ignore_words = ignore_words.union(clean_set(settings.IGNORE_WORDS)) # Plus Global ignored words
ignore_words = ignore_words.difference(clean_set(show and show.rls_require_words or "")) # Minus show specific required words
if settings.REQUIRE_WORDS and not (show and show.rls_ignore_words): # Only remove global require words from the list if we arent using show ignore words
if settings.REQUIRE_WORDS and not (show and show.rls_ignore_words): # Only remove global require words from the list if we aren't using show ignore words
ignore_words = ignore_words.difference(clean_set(settings.REQUIRE_WORDS))

word = containsAtLeastOneWord(name, ignore_words)
word = contains_at_least_one_word(name, ignore_words)
if word:
logger.info("Release: {} contains {}, ignoring it".format(name, word))
logger.info(f"Release: {name} contains {word}, ignoring it")
return False

# if any of the good strings aren't in the name then say no
Expand All @@ -81,14 +81,14 @@ def clean_set(words):
if settings.IGNORE_WORDS and not (show and show.rls_require_words): # Only remove global ignore words from the list if we arent using show require words
require_words = require_words.difference(clean_set(settings.IGNORE_WORDS))

if require_words and not containsAtLeastOneWord(name, require_words):
logger.info("Release: " + name + " doesn't contain any of " + ", ".join(set(require_words)) + ", ignoring it")
if require_words and not contains_at_least_one_word(name, require_words):
logger.info(f"Release: {name} doesn't contain a required word {require_words}, ignoring it")
return False

return True


def allPossibleShowNames(show, season=-1):
def all_possible_show_names(show, season=-1):
"""
Figures out every possible variation of the name for a particular show. Includes TVDB name, TVRage name,
country codes on the end, eg. "Show Name (AU)", and any scene exception names.
Expand All @@ -98,36 +98,36 @@ def allPossibleShowNames(show, season=-1):
Returns: a list of all the possible show names
"""

showNames = get_scene_exceptions(show.indexerid, season=season)
if not showNames: # if we don't have any season specific exceptions fallback to generic exceptions
show_names = get_scene_exceptions(show.indexerid, season=season)
if not show_names: # if we don't have any season specific exceptions fallback to generic exceptions
season = -1
showNames = get_scene_exceptions(show.indexerid, season=season)
show_names = get_scene_exceptions(show.indexerid, season=season)

showNames.append(show.name)
show_names.append(show.name)

if not show.is_anime:
newShowNames = []
new_show_names = []
country_list = common.countryList
country_list.update({common.countryList[k]: k for k in common.countryList})
for curName in set(showNames):
if not curName:
for current_name in set(show_names):
if not current_name:
continue

# if we have "Show Name Australia" or "Show Name (Australia)" this will add "Show Name (AU)" for
# any countries defined in common.countryList
# (and vice versa)
for curCountry in country_list:
if curName.endswith(" " + curCountry):
newShowNames.append(curName.replace(" " + curCountry, " (" + country_list[curCountry] + ")"))
elif curName.endswith(" (" + curCountry + ")"):
newShowNames.append(curName.replace(" (" + curCountry + ")", " (" + country_list[curCountry] + ")"))
for current_country in country_list:
if current_name.endswith(" " + current_country):
new_show_names.append(current_name.replace(" " + current_country, " (" + country_list[current_country] + ")"))
elif current_name.endswith(" (" + current_country + ")"):
new_show_names.append(current_name.replace(" (" + current_country + ")", " (" + country_list[current_country] + ")"))

# # if we have "Show Name (2013)" this will strip the (2013) show year from the show name
# newShowNames.append(re.sub('\(\d{4}\)', '', curName))
# new_show_names.append(re.sub('\(\d{4}\)', '', current_name))

showNames += newShowNames
show_names += new_show_names

return set(showNames)
return set(show_names)


def determine_release_name(directory=None, release_name=None):
Expand Down Expand Up @@ -168,8 +168,8 @@ def determine_release_name(directory=None, release_name=None):
return None


def hasPreferredWords(name, show=None):
"""Determine based on the full episode (file)name combined with the preferred words what the weight its preference should be"""
def has_preferred_words(name, show=None):
"""Determine based on the full episode (file) name combined with the preferred words what the weight its preference should be"""

name = name.lower()

Expand All @@ -187,7 +187,7 @@ def clean_set(words):

prefer_words = []

# Because we weigh values, we can not union global and show based values, so we don't do that
# Because we weigh values, we cannot union global and show based values, so we don't do that
if settings.PREFER_WORDS:
prefer_words = clean_set(settings.PREFER_WORDS)
if show and show.rls_prefer_words:
Expand Down
6 changes: 3 additions & 3 deletions sickchill/providers/GenericProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from sickchill.oldbeard.db import DBConnection
from sickchill.oldbeard.helpers import download_file, getURL, make_session, remove_file_failed
from sickchill.oldbeard.name_parser.parser import InvalidNameException, InvalidShowException, NameParser
from sickchill.oldbeard.show_name_helpers import allPossibleShowNames
from sickchill.oldbeard.show_name_helpers import all_possible_show_names
from sickchill.oldbeard.tvcache import TVCache
from sickchill.providers.result_classes import Proper, SearchResult

Expand Down Expand Up @@ -417,7 +417,7 @@ def get_episode_search_strings(self, episode: "TVEpisode", add_string: str = "")

search_string = {"Episode": set()}

for show_name in allPossibleShowNames(episode.show, season=episode.scene_season):
for show_name in all_possible_show_names(episode.show, season=episode.scene_season):
episode_string = show_name + " "
episode_string_fallback = None

Expand Down Expand Up @@ -450,7 +450,7 @@ def get_episode_search_strings(self, episode: "TVEpisode", add_string: str = "")
def get_season_search_strings(self, episode: "TVEpisode") -> List[Dict]:
search_string = {"Season": set()}

for show_name in allPossibleShowNames(episode.show, season=episode.scene_season):
for show_name in all_possible_show_names(episode.show, season=episode.scene_season):
season_string = show_name + " "

if episode.show.air_by_date or episode.show.sports:
Expand Down
4 changes: 2 additions & 2 deletions sickchill/providers/torrent/FrenchProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sickchill.helper.common import valid_url
from sickchill.oldbeard import tvcache
from sickchill.oldbeard.bs4_parser import BS4Parser
from sickchill.oldbeard.show_name_helpers import allPossibleShowNames
from sickchill.oldbeard.show_name_helpers import all_possible_show_names
from sickchill.providers.torrent.TorrentProvider import TorrentProvider

if TYPE_CHECKING:
Expand Down Expand Up @@ -91,7 +91,7 @@ def url(self, url):

def get_season_search_strings(self, episode: "TVEpisode") -> List[Dict]:
search_string = {"Season": set()}
for show_name in allPossibleShowNames(episode.show, season=episode.scene_season):
for show_name in all_possible_show_names(episode.show, season=episode.scene_season):
season = int(episode.scene_season)
if episode.show.air_by_date or episode.show.sports:
year = str(episode.airdate).split("-")[0]
Expand Down
22 changes: 11 additions & 11 deletions sickchill/views/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ def write_error(self, status_code, **kwargs):
self.set_header("Content-Type", "text/html")
return self.finish(
"""<html>
<title>{0}</title>
<body>
<h2>Error</h2>
<p>{1}</p>
<h2>Traceback</h2>
<p>{2}</p>
<h2>Request Info</h2>
<p>{3}</p>
<button onclick="window.location='{4}/errorlogs/';">View Log(Errors)</button>
</body>
</html>""".format(
<title>{0}</title>
<body>
<h2>Error</h2>
<p>{1}</p>
<h2>Traceback</h2>
<p>{2}</p>
<h2>Request Info</h2>
<p>{3}</p>
<button onclick="window.location='{4}/errorlogs/';">View Log(Errors)</button>
</body>
</html>""".format(
error, error, trace_info, request_info, settings.WEB_ROOT
)
)
Expand Down
5 changes: 5 additions & 0 deletions sickchill/views/manage/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

@Route("/manage(/?.*)", name="manage:main")
class Manage(Home, WebRoot):
def __init__(self, backend, back2=None):
super().__init__(backend, back2)
self.to_change_show = None
self.to_change_eps = None

def index(self):
t = PageTemplate(rh=self, filename="manage.mako")
return t.render(
Expand Down
16 changes: 8 additions & 8 deletions tests/test_prefer_words.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest

from sickchill import settings
from sickchill.oldbeard.show_name_helpers import hasPreferredWords
from sickchill.oldbeard.show_name_helpers import has_preferred_words


class PreferWordFilterTest(unittest.TestCase):
Expand All @@ -20,13 +20,13 @@ def setUp(self):
]

def test_prefer_words_determine_weight(self):
assert hasPreferredWords(self.results_names_only_source[0]) == 0
assert hasPreferredWords(self.results_names_only_source[1]) == 1
assert hasPreferredWords(self.results_names_only_source[2]) == 3
assert hasPreferredWords(self.results_names_only_source[3]) == 5
assert hasPreferredWords(self.results_names_only_source[4]) == 5
assert hasPreferredWords(self.results_names_only_source[5]) == 4
assert hasPreferredWords(self.results_names_only_source[6]) == 2
assert has_preferred_words(self.results_names_only_source[0]) == 0
assert has_preferred_words(self.results_names_only_source[1]) == 1
assert has_preferred_words(self.results_names_only_source[2]) == 3
assert has_preferred_words(self.results_names_only_source[3]) == 5
assert has_preferred_words(self.results_names_only_source[4]) == 5
assert has_preferred_words(self.results_names_only_source[5]) == 4
assert has_preferred_words(self.results_names_only_source[6]) == 2


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/test_scene_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def _test_all_possible_show_names(self, name, indexerid=0, expected=None):
show = Show(1, indexerid)
show.name = name

result = show_name_helpers.allPossibleShowNames(show)
result = show_name_helpers.all_possible_show_names(show)
assert len(set(expected).intersection(set(result))) == len(expected)

def test_all_possible_show_names(self):
Expand Down

0 comments on commit 5bd179c

Please sign in to comment.