Skip to content

Commit

Permalink
feat: active release
Browse files Browse the repository at this point in the history
d
  • Loading branch information
wolfemir committed Dec 22, 2024
1 parent 7d3a004 commit d3bd27a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
30 changes: 22 additions & 8 deletions src/program/apis/tvmaze_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,13 @@ def get_show_by_imdb(self, imdb_id: str, show_name: Optional[str] = None, season
if not episodes:
return None

# Find the next episode that hasn't aired yet
current_time = datetime.now()
# Find all unreleased episodes and the next episode
current_time = datetime.now().astimezone() # Make sure current_time has timezone info
unreleased_episodes = []
next_episode = None
target_episode_time = None

for episode in episodes:
for episode in sorted(episodes, key=lambda x: (getattr(x, 'season', 0), getattr(x, 'number', 0))):
try:
if not episode.airstamp:
continue
Expand Down Expand Up @@ -159,22 +160,35 @@ def get_show_by_imdb(self, imdb_id: str, show_name: Optional[str] = None, season
else:
logger.debug(f"Found S{season_number}E{episode_number} airing at {air_time}")
target_episode_time = air_time
break # No need to continue looking

# If we're looking for next episode and this one is in the future
elif air_time > current_time:
# If we haven't found any future episode yet, or this one airs sooner
# Add all unreleased episodes to our list
if air_time > current_time:
ep_info = {
'air_time': air_time,
'season': getattr(episode, 'season', 0),
'episode': getattr(episode, 'number', 0),
'name': getattr(episode, 'name', '')
}
unreleased_episodes.append(ep_info)
# Track next episode separately
if not next_episode or air_time < next_episode:
next_episode = air_time

except Exception as e:
logger.error(f"Failed to process episode {getattr(episode, 'number', '?')}: {e}")
continue

# Return target episode time if we found one, otherwise return next episode
# Return target episode time if we found one
if target_episode_time is not None:
return target_episode_time

# Log all unreleased episodes in sequence
if unreleased_episodes:
unreleased_episodes.sort(key=lambda x: (x['season'], x['episode']))
for ep in unreleased_episodes:
logger.debug(f"Unreleased: S{ep['season']}E{ep['episode']} '{ep['name']}' airs at {ep['air_time']}")

# Return next episode air time
if next_episode:
logger.debug(f"Next episode airs at {next_episode}")
return next_episode
Expand Down
12 changes: 3 additions & 9 deletions src/program/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,6 @@ def _update_ongoing(self) -> None:
# Skip if no IMDB ID
if not item.imdb_id:
continue

# For episodes, check if we already found a future episode for this show
if item.type == "episode":
show_id = item.parent.parent.id if item.parent and item.parent.parent else None
if show_id in checked_shows:
continue

trakt_time = None
tvmaze_time = None
Expand Down Expand Up @@ -316,7 +310,7 @@ def _update_ongoing(self) -> None:
previous_state, new_state = item.store_state()
if previous_state != new_state:
self.em.add_event(Event("StateTransition", item_id=item.id))
logger.log("🎬 RELEASE", f" Released (late): {item.log_string}")
logger.log("RELEASE", f"🎬 Released (late): {item.log_string}")
continue

# Check if releasing in next 24 hours
Expand Down Expand Up @@ -346,8 +340,8 @@ def _update_ongoing(self) -> None:
logger.error(f"Failed to schedule release for {item.log_string}: {e}")

# If this episode isn't releasing soon, skip rest of the season
if item.type == "episode":
checked_shows.add(show_id)
# if item.type == "episode":
# checked_shows.add(show_id)

except Exception as e:
logger.error(f"Failed to process {item.log_string}: {e}")
Expand Down

0 comments on commit d3bd27a

Please sign in to comment.