diff options
| author | Fuwn <[email protected]> | 2025-07-07 04:07:51 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-07-07 04:07:51 -0700 |
| commit | 15f3a35da45e75eb7e461f166cd313d09106c8c2 (patch) | |
| tree | a3ef3b4cd784ecd6116a468f1d5aa4ee506b24ff /src | |
| parent | chore: Add check task (diff) | |
| download | oguri-15f3a35da45e75eb7e461f166cd313d09106c8c2.tar.xz oguri-15f3a35da45e75eb7e461f166cd313d09106c8c2.zip | |
refactor(schedule): Split into sub-functions
Diffstat (limited to 'src')
| -rw-r--r-- | src/oguri/schedule.py | 129 |
1 files changed, 77 insertions, 52 deletions
diff --git a/src/oguri/schedule.py b/src/oguri/schedule.py index 5b39bf8..7aa29b6 100644 --- a/src/oguri/schedule.py +++ b/src/oguri/schedule.py @@ -29,7 +29,19 @@ async def show_schedule( ): client = Client(url="https://graphql.anilist.co") now = datetime.now() + start_of_day, end_of_day = _calculate_date_range(now, days_offset, year, + month) + all_airing_schedules = await _fetch_airing_schedules(client, start_of_day, + end_of_day, episode, + first_episode_only) + airing_schedules = _filter_and_sort_schedules(all_airing_schedules, countries, + formats, sources, genres, + sort_by, reverse_order) + _display_schedule_table(airing_schedules, exact_time) + + +def _calculate_date_range(now, days_offset, year, month): if year is not None or month is not None: target_year = year if year is not None else now.year target_month = month if month is not None else now.month @@ -47,6 +59,11 @@ async def show_schedule( end_of_day = start_of_day.replace( hour=23, minute=59, second=59, microsecond=999999) + return start_of_day, end_of_day + + +async def _fetch_airing_schedules(client, start_of_day, end_of_day, episode, + first_episode_only): all_airing_schedules = [] has_next_page = True page_number = 1 @@ -100,60 +117,68 @@ async def show_schedule( has_next_page = False - if all_airing_schedules: - airing_schedules = [ - s for s in all_airing_schedules - if s.get("media", {}).get("countryOfOrigin") in countries and - s.get("media", {}).get("format") in formats and - s.get("media", {}).get("source") in sources and any( - g in genres for g in s.get("media", {}).get("genres", [])) - ] - - if sort_by == "time": - airing_schedules.sort( - key=lambda x: x.get("airingAt"), reverse=not reverse_order) - elif sort_by == "episode": - airing_schedules.sort( - key=lambda x: x.get("episode"), reverse=not reverse_order) - - console = Console() - table = Table(show_header=True, header_style="bold magenta") - - table.add_column("Title") - table.add_column("Episode") - table.add_column("Airing Time") - table.add_column("Genres") - - for schedule in airing_schedules: - airing_at = schedule.get("airingAt") - episode = schedule.get("episode") - media = schedule.get("media") - site_url = media.get("siteUrl") - titles = media.get("title") - title = ( - titles.get("english") or titles.get("romaji") or titles.get("native")) - media_genres = media.get("genres", []) - genres_string = ", ".join(media_genres) - - if airing_at: - airing_at_date = datetime.fromtimestamp(airing_at) - airing_time_string = "" - - if exact_time: - airing_time_string = format_exact_time(airing_at_date) - elif datetime.now() > airing_at_date: - airing_time_string = (f"Aired {relative_time(airing_at_date)}") - else: - airing_time_string = format_future_airing_time(airing_at_date) + return all_airing_schedules + + +def _filter_and_sort_schedules(all_airing_schedules, countries, formats, + sources, genres, sort_by, reverse_order): + airing_schedules = [ + s for s in all_airing_schedules + if s.get("media", {}).get("countryOfOrigin") in countries and + s.get("media", {}).get("format") in formats and + s.get("media", {}).get("source") in sources and any( + g in genres for g in s.get("media", {}).get("genres", [])) + ] + + if sort_by == "time": + airing_schedules.sort( + key=lambda x: x.get("airingAt"), reverse=not reverse_order) + elif sort_by == "episode": + airing_schedules.sort( + key=lambda x: x.get("episode"), reverse=not reverse_order) + + return airing_schedules + + +def _display_schedule_table(airing_schedules, exact_time): + console = Console() + table = Table(show_header=True, header_style="bold magenta") + + table.add_column("Title") + table.add_column("Episode") + table.add_column("Airing Time") + table.add_column("Genres") + + for schedule in airing_schedules: + airing_at = schedule.get("airingAt") + episode = schedule.get("episode") + media = schedule.get("media") + site_url = media.get("siteUrl") + titles = media.get("title") + title = ( + titles.get("english") or titles.get("romaji") or titles.get("native")) + media_genres = media.get("genres", []) + genres_string = ", ".join(media_genres) + + if airing_at: + airing_at_date = datetime.fromtimestamp(airing_at) + airing_time_string = "" + + if exact_time: + airing_time_string = format_exact_time(airing_at_date) + elif datetime.now() > airing_at_date: + airing_time_string = (f"Aired {relative_time(airing_at_date)}") + else: + airing_time_string = format_future_airing_time(airing_at_date) - table.add_row( - f"[link={site_url}]{title}[/link]", - str(episode), - airing_time_string, - genres_string, - ) + table.add_row( + f"[link={site_url}]{title}[/link]", + str(episode), + airing_time_string, + genres_string, + ) - console.print(table) + console.print(table) def format_exact_time(date): |