Skip to content

Commit

Permalink
fix logging
Browse files Browse the repository at this point in the history
  • Loading branch information
chkpwd committed Oct 29, 2024
1 parent 894faf9 commit 03191aa
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 33 deletions.
1 change: 1 addition & 0 deletions app/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

logger = logging.getLogger(__name__)


def get_anime_filler_list(afl_anime_name: str):
"""Get the anime filler list."""

Expand Down
13 changes: 7 additions & 6 deletions app/plex.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
from plexapi.server import PlexServer
from plexapi.base import MediaContainer
from plexapi.library import ShowSection
from constants.variables import UserConfig

var = UserConfig()
from app.variables import USER_CONFIG


logger = logging.getLogger(__name__)

plex = PlexServer(baseurl=var.plex_url, token=var.plex_token)
plex = PlexServer(baseurl=USER_CONFIG.plex_url, token=USER_CONFIG.plex_token)


def create_plex_collection(collection_items: list[str] = []):
nonfillers_items: list[Episode] = []
fillers_items: list[Episode] = []

media: ShowSection = plex.library.section(title=var.plex_anime_library)
media: ShowSection = plex.library.section(title=USER_CONFIG.plex_anime_library)

shows: MediaContainer = media.search(title=var.plex_anime_name)
shows: MediaContainer = media.search(title=USER_CONFIG.plex_anime_name)

for show in shows:
plex_episodes: list[Episode] = show.episodes()
Expand All @@ -38,7 +39,7 @@ def create_plex_collection(collection_items: list[str] = []):
if items:
plex.createCollection(
title=collection_name,
section=var.plex_anime_library,
section=USER_CONFIG.plex_anime_library,
items=items,
)
else:
Expand Down
28 changes: 14 additions & 14 deletions app/sonarr.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import sonarr
import logging

from constants.variables import UserConfig
from app.variables import USER_CONFIG

logger = logging.getLogger(__name__)

var = UserConfig()

CONFIGURATION = sonarr.Configuration(host=var.sonarr_url)
CONFIGURATION.api_key["X-Api-Key"] = var.sonarr_api_key
CONFIGURATION = sonarr.Configuration(host=USER_CONFIG.sonarr_url)
CONFIGURATION.api_key["X-Api-Key"] = USER_CONFIG.sonarr_api_key


def get_sonarr_episodes(series_id: int):
Expand All @@ -23,14 +22,16 @@ def get_sonarr_episodes(series_id: int):

for item in api_response:
if item.season_number != 0:
episodes.append({
"id": item.id,
"title": item.title,
"season": item.season_number,
"monitored": item.monitored,
"episode_number": item.episode_number,
"absolute_episode_number": item.absolute_episode_number
})
episodes.append(
{
"id": item.id,
"title": item.title,
"season": item.season_number,
"monitored": item.monitored,
"episode_number": item.episode_number,
"absolute_episode_number": item.absolute_episode_number,
}
)
except Exception as e:
logger.error("Exception when calling EpisodeApi->list_episode: %s\n" % e)

Expand All @@ -43,8 +44,7 @@ def configure_monitoring(monitored_list: list[int]):
api_instance = sonarr.EpisodeApi(api_client)

episodes_monitored_resource = sonarr.EpisodesMonitoredResource(
episodeIds=[*monitored_list],
monitored=True
episodeIds=[*monitored_list], monitored=True
)

try:
Expand Down
11 changes: 9 additions & 2 deletions constants/variables.py → app/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import sys
import logging

from attrs.converters import to_bool
from dotenv import load_dotenv
from attrs.converters import to_bool


load_dotenv(dotenv_path=".env.priv")
Expand All @@ -16,6 +16,7 @@ class UserConfig:
Configuration constants for the application.
Attributes:
debug (bool): Whether or not to enable debug logging. Defaults to False.
afl_anime_name (str): The name of the anime to fetch filler episodes for.
sonarr_url (str): The URL of the Sonarr server.
sonarr_series_id (int): The ID of the Sonarr series to update.
Expand All @@ -29,14 +30,17 @@ class UserConfig:
"""

def __init__(self):
self.debug: bool = to_bool(os.environ.get("DEBUG", default="False"))
self.afl_anime_name: str = self._get_env_var("AFL_ANIME_NAME", required=True)
self.sonarr_url: str = self._get_env_var("SONARR_URL", required=True)
self.sonarr_series_id: int = int(
self._get_env_var("SONARR_SERIES_ID", required=True)
)
self.sonarr_api_key: str = self._get_env_var("SONARR_API_KEY", required=True)
self.monitor_non_filler_sonarr_episodes: bool = to_bool(
self._get_env_var("MONITOR_NON_FILLER_SONARR_EPISODES", required=True, default="True")
self._get_env_var(
"MONITOR_NON_FILLER_SONARR_EPISODES", required=True, default="True"
)
)
self.create_plex_collection: bool = to_bool(
self._get_env_var("CREATE_PLEX_COLLECTION", required=True, default="False")
Expand All @@ -62,3 +66,6 @@ def _get_env_var(
except ValueError:
logger.error(f"Invalid value for {key}: {value}")
sys.exit(1)


USER_CONFIG = UserConfig()
Empty file removed constants/__init__.py
Empty file.
27 changes: 16 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import logging

from app.plex import create_plex_collection

from app.plex import create_plex_collection
from app.parser import get_anime_filler_list
from app.sonarr import get_sonarr_episodes, configure_monitoring
from app.variables import USER_CONFIG

from constants.variables import UserConfig

logger = logging.getLogger(__name__)
logging.basicConfig(level=logger.level, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")

var = UserConfig()
if USER_CONFIG.debug:
logger.setLevel("DEBUG")

logger = logging.getLogger(__name__)

def main():
nonfillers_episodes = []

fillers_from_api = get_anime_filler_list(var.afl_anime_name)
sonarr_episodes = get_sonarr_episodes(int(var.sonarr_series_id))
fillers_from_api = get_anime_filler_list(USER_CONFIG.afl_anime_name)
sonarr_episodes = get_sonarr_episodes(int(USER_CONFIG.sonarr_series_id))

for episode in sonarr_episodes:
if episode["absolute_episode_number"] not in fillers_from_api:
nonfillers_episodes.append({
nonfillers_episodes.append(
{
"id": episode.get("id"),
"season": episode.get("season"),
"episode_number": episode.get("episode_number"),
"absolute_episode_number": episode.get("absolute_episode_number"),
})
}
)

episodes_in_season_episode_format = [
f"s{episode['season']:02d}e{episode['episode_number']:02d}"
Expand All @@ -34,14 +38,15 @@ def main():

episodes_to_monitor = [episode.get("id") for episode in nonfillers_episodes]

if var.create_plex_collection is True:
if USER_CONFIG.create_plex_collection is True:
create_plex_collection(collection_items=episodes_in_season_episode_format)

logger.debug("Non-Filler Episodes: %s", nonfillers_episodes)
logger.debug(f"Non-Filler Episodes: {nonfillers_episodes}")

if nonfillers_episodes and var.monitor_non_filler_sonarr_episodes is True:
if nonfillers_episodes and USER_CONFIG.monitor_non_filler_sonarr_episodes is True:
configure_monitoring(monitored_list=episodes_to_monitor)


if __name__ == "__main__":
logger.info("Initializing SoFE...")
main()

0 comments on commit 03191aa

Please sign in to comment.