Handle shows with more than one season correctly

This commit is contained in:
PapaTutuWawa 2023-12-17 11:47:41 +01:00
parent 6887c3ef89
commit b5df984589
2 changed files with 37 additions and 13 deletions

View File

@ -24,6 +24,9 @@ class Show:
# The release year of the show. # The release year of the show.
year: int year: int
# The season number, if specified
season: int | None
# The base URL of the show. # The base URL of the show.
url: str url: str
@ -33,8 +36,21 @@ def build_show_dir_name(name: str, year: int) -> str:
return f"{name} ({year})" return f"{name} ({year})"
def build_episodes_path(
name: str, year: int, season: int | None, storage_path: Path
) -> Path:
show_path = storage_path / build_show_dir_name(name, year)
if season is not None:
return show_path / f"Season {season}"
return show_path
def get_episode_numbers( def get_episode_numbers(
show_name: str, show_year: int, storage_path: Path show_name: str,
show_year: int,
show_season: int | None,
storage_path: Path,
) -> list[int]: ) -> list[int]:
"""Computes the episode numbers that are locally available. """Computes the episode numbers that are locally available.
@ -42,7 +58,7 @@ def get_episode_numbers(
@show_year: The release year of the show. @show_year: The release year of the show.
@storage_path: Path to where the show directories are. @storage_path: Path to where the show directories are.
""" """
episodes_path = storage_path / build_show_dir_name(show_name, show_year) episodes_path = build_episodes_path(show_name, show_year, show_season, storage_path)
if not episodes_path.exists(): if not episodes_path.exists():
return [] return []
@ -79,10 +95,15 @@ def main():
storage_path = Path(config["general"]["path"]) storage_path = Path(config["general"]["path"])
shows: list[Show] = [] shows: list[Show] = []
for show in config["shows"]: for show in config["shows"]:
logging.info(f"Processing {show['name']}...") logging.info("Processing %s", show["name"])
max_episodes = show["max_episodes"] max_episodes = show["max_episodes"]
local_episodes = get_episode_numbers(show["name"], show["year"], storage_path) local_episodes = get_episode_numbers(
logging.debug(f"=> {max(local_episodes)}/{max_episodes}") show["name"],
show["year"],
show.get("season", None),
storage_path,
)
logging.debug("=> %d/%d", max(local_episodes), max_episodes)
if len(set(local_episodes)) != max_episodes: if len(set(local_episodes)) != max_episodes:
shows.append( shows.append(
Show( Show(
@ -90,6 +111,7 @@ def main():
name=show["name"], name=show["name"],
year=show["year"], year=show["year"],
url=show["url"], url=show["url"],
season=show.get("season", None),
), ),
) )
@ -102,7 +124,7 @@ def main():
cookies = {} cookies = {}
cookies_path = Path(config["general"]["cookie_path"]) cookies_path = Path(config["general"]["cookie_path"])
if cookies_path.exists(): if cookies_path.exists():
with open(cookies_path, "r") as f: with open(cookies_path, "r", encoding="utf8") as f:
cookies = json.load(f) cookies = json.load(f)
# Request a new session, if required # Request a new session, if required
@ -112,14 +134,19 @@ def main():
config["user"]["email"], config["user"]["email"],
config["user"]["password"], config["user"]["password"],
) )
with open(cookies_path, "w") as f: with open(cookies_path, "w", encoding="utf8") as f:
f.write(json.dumps(cookies)) f.write(json.dumps(cookies))
logging.info("Done") logging.info("Done")
# Iterate over the shows to process and download the new episodes # Iterate over the shows to process and download the new episodes
for show in shows: for show in shows:
logging.debug("Processing %s", show.name) logging.debug("Processing %s", show.name)
episodes_path = storage_path / build_show_dir_name(show.name, show.year) episodes_path = build_episodes_path(
show.name,
show.year,
show.season,
storage_path,
)
if not episodes_path.exists(): if not episodes_path.exists():
logging.info( logging.info(
"Episodes directory of %s does not exist. Creating...", show.name "Episodes directory of %s does not exist. Creating...", show.name
@ -135,7 +162,7 @@ def main():
for episode in episodes_remote: for episode in episodes_remote:
if episode.episode_nr in episodes_to_download: if episode.episode_nr in episodes_to_download:
logging.info(f"Downloading {episode.name} ({episode.episode_nr})...") logging.info("Downloading %s (%d)", episode.name, episode.episode_nr)
downloads = episode.get_downloads( downloads = episode.get_downloads(
cookies=cookies, cookies=cookies,
filter_quality=akibapass_downloader.episode.Quality.UHD_1440P, filter_quality=akibapass_downloader.episode.Quality.UHD_1440P,

View File

@ -17,8 +17,5 @@ async def send_notification(
to=aioxmpp.JID.fromstr(to), to=aioxmpp.JID.fromstr(to),
type_=aioxmpp.MessageType.CHAT, type_=aioxmpp.MessageType.CHAT,
) )
msg.body[None] = "Episode {} of {} is now downloaded and available!".format( msg.body[None] = f"Episode {episode} of {show_name} is now downloaded and available!"
episode,
show_name,
)
await stream.send(msg) await stream.send(msg)