Skip to content

Commit

Permalink
refactor logic for monitoring and collection
Browse files Browse the repository at this point in the history
  • Loading branch information
chkpwd committed Oct 24, 2024
1 parent 535c370 commit e13b042
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 36 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Current File",
"type": "debugpy",
"request": "launch",
"program": "main.py",
"console": "integratedTerminal"
}
]
}
38 changes: 18 additions & 20 deletions app/plex.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging

from plexapi.collection import Collection
from plexapi.video import Episode
from plexapi.server import PlexServer
from plexapi.base import MediaContainer
Expand All @@ -15,33 +14,32 @@

def create_plex_collection(sonarr_episodes: list[int], fillers: list[int] = []):
nonfillers_items: list[Episode] = []
fillers_items: list[Episode] = []

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

shows: MediaContainer = media.search(title=var.anime_name)

for show in shows:
show_name: str = show.title
plex_episodes: list[Episode] = show.episodes()

for episode in plex_episodes:
if episode.episodeNumber not in fillers:
nonfillers_items.append(episode)

if nonfillers_items:
plex.createCollection(
title=f"{show_name} - Non-Filler Episodes", # FIXME: title gets modified for some reason
section=var.plex_anime_library,
items=nonfillers_items,
)
else:
logging.info("No non-filler episodes found.")

# # Create a smart collection
# collection = plex.createCollection(
# title="Recently Aired Comedy TV Shows",
# section="TV Shows",
# smart=True,
# sort="episode.originallyAvailableAt:desc",
# filters={"episode.originallyAvailableAt>>": "4w", "genre": "comedy"},
# )
else:
fillers_items.append(episode)

# create collections if they don't exist
for items, collection_name in [
(nonfillers_items, f"{show.title} - Non-Filler Episodes"),
(fillers_items, f"{show.title} - Filler Episodes"),
]:
if not plex.library.search(title=collection_name):
if items:
plex.createCollection(
title=collection_name,
section=var.plex_anime_library,
items=items,
)
else:
logging.info(f"No {collection_name.split(' - ')[1].lower()} found.")
24 changes: 8 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
import logging

from flask import Flask, jsonify

from app.parser import get_anime_filler_list
from app.sonarr import configure_monitoring, get_sonarr_episodes
from app.plex import create_plex_collection
Expand All @@ -21,32 +21,24 @@
handlers=[logging.StreamHandler(sys.stdout)],
)

app = Flask(__name__)

@app.route("/fillers", methods=["GET"])
def get_filler_list():
"""Returns the filler list for the specified anime."""
filler_list = get_anime_filler_list(anime_name=var.anime_name)
return jsonify({"value": filler_list})

if __name__ == "__main__":
episodes_to_monitor = []

logging.info("Initializing SOFE API...")
logging.info("Initializing SoFE...")
logging.info("Address is set to '%s'", var.listen_address)
logging.info("Port is set to '%s'", var.listen_port)

fillers_from_api = get_anime_filler_list(var.anime_name)
sonarr_episodes = get_sonarr_episodes(int(var.sonarr_series_id))
sonarr_episodes_id = get_sonarr_episodes(int(var.sonarr_series_id))

if var.create_plex_collection is True:
create_plex_collection(sonarr_episodes=sonarr_episodes, fillers=fillers_from_api)
create_plex_collection(sonarr_episodes=sonarr_episodes_id, fillers=fillers_from_api)

for episode in sonarr_episodes:
if episode['number'] not in fillers_from_api:
for episode in sonarr_episodes_id:
if episode['episode_number'] not in fillers_from_api:
episodes_to_monitor.append(episode.get('id'))

logging.debug("Non-Filler Episodes: %s", episodes_to_monitor)
configure_monitoring(monitored_list=episodes_to_monitor)

app.run(host=var.listen_address, port=var.listen_port)
if not episodes_to_monitor or var.monitor_non_filler_sonarr_episodes is True:
configure_monitoring(monitored_list=episodes_to_monitor)

0 comments on commit e13b042

Please sign in to comment.