aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-07-07 02:21:08 -0700
committerFuwn <[email protected]>2025-07-07 02:21:08 -0700
commit0ba6c81033faa9fa172e732fa203fa82105428f2 (patch)
tree5f515852c110ecf2b0697d36f7cbd8ce01b8e03b /src
parentfeat(constants): Update default countries (diff)
downloadoguri-0ba6c81033faa9fa172e732fa203fa82105428f2.tar.xz
oguri-0ba6c81033faa9fa172e732fa203fa82105428f2.zip
style: Replace Black with YAPF
Diffstat (limited to 'src')
-rw-r--r--src/oguri/__init__.py2
-rw-r--r--src/oguri/__main__.py2
-rw-r--r--src/oguri/cli.py70
-rw-r--r--src/oguri/constants.py4
-rw-r--r--src/oguri/schedule.py316
5 files changed, 190 insertions, 204 deletions
diff --git a/src/oguri/__init__.py b/src/oguri/__init__.py
index 3e66273..d2d379b 100644
--- a/src/oguri/__init__.py
+++ b/src/oguri/__init__.py
@@ -1,4 +1,4 @@
from .cli import main_script
if __name__ == "__main__":
- main_script()
+ main_script()
diff --git a/src/oguri/__main__.py b/src/oguri/__main__.py
index 6cda171..a49dab1 100644
--- a/src/oguri/__main__.py
+++ b/src/oguri/__main__.py
@@ -1,4 +1,4 @@
from oguri.cli import main_script
if __name__ == "__main__":
- main_script()
+ main_script()
diff --git a/src/oguri/cli.py b/src/oguri/cli.py
index f3781a4..5a5fbd3 100644
--- a/src/oguri/cli.py
+++ b/src/oguri/cli.py
@@ -7,17 +7,19 @@ from . import constants
@click.group(cls=ClickAliasedGroup)
def cli():
- """A command-line tool for AniList"""
+ """A command-line tool for AniList"""
- pass
+ pass
@cli.command(aliases=["s"])
@click.argument("day", required=False, default="today")
[email protected]("--reverse", is_flag=True, help="Reverse the order of the schedule")
@click.option(
- "--exact", is_flag=True, help="Show exact airing times instead of relative times"
-)
+ "--reverse", is_flag=True, help="Reverse the order of the schedule")
+ "--exact",
+ is_flag=True,
+ help="Show exact airing times instead of relative times")
@click.option("--first", is_flag=True, help="Show only first airing episodes")
@click.option(
"--sort",
@@ -75,42 +77,40 @@ def schedule(
month,
genres,
):
- """Shows the airing schedule for a given day
+ """Shows the airing schedule for a given day
DAY can be "today", "tomorrow", "yesterday", or an integer representing the number of days from now.
"""
- days_offset = 0
+ days_offset = 0
- if day == "tomorrow":
- days_offset = 1
- elif day == "yesterday":
- days_offset = -1
- elif day != "today":
- try:
- days_offset = int(day)
- except ValueError:
- raise click.BadParameter(
- 'DAY must be "today", "tomorrow", "yesterday", or an integer.'
- )
+ if day == "tomorrow":
+ days_offset = 1
+ elif day == "yesterday":
+ days_offset = -1
+ elif day != "today":
+ try:
+ days_offset = int(day)
+ except ValueError:
+ raise click.BadParameter(
+ 'DAY must be "today", "tomorrow", "yesterday", or an integer.')
- asyncio.run(
- schedule_logic.show_schedule(
- days_offset,
- reverse,
- exact,
- first,
- sort_by,
- countries,
- episode,
- formats,
- sources,
- year,
- month,
- genres,
- )
- )
+ asyncio.run(
+ schedule_logic.show_schedule(
+ days_offset,
+ reverse,
+ exact,
+ first,
+ sort_by,
+ countries,
+ episode,
+ formats,
+ sources,
+ year,
+ month,
+ genres,
+ ))
def main_script():
- cli()
+ cli()
diff --git a/src/oguri/constants.py b/src/oguri/constants.py
index 95c8cf9..d015dce 100644
--- a/src/oguri/constants.py
+++ b/src/oguri/constants.py
@@ -1,6 +1,8 @@
COUNTRIES = ["JP", "KR", "CN", "TW"]
FORMATS = ["TV", "TV_SHORT", "MOVIE", "SPECIAL", "OVA"]
-SOURCES = ["ORIGINAL", "MANGA", "LIGHT_NOVEL", "VISUAL_NOVEL", "VIDEO_GAME", "OTHER"]
+SOURCES = [
+ "ORIGINAL", "MANGA", "LIGHT_NOVEL", "VISUAL_NOVEL", "VIDEO_GAME", "OTHER"
+]
GENRES = [
"Action",
"Adventure",
diff --git a/src/oguri/schedule.py b/src/oguri/schedule.py
index b036f33..15b7fe1 100644
--- a/src/oguri/schedule.py
+++ b/src/oguri/schedule.py
@@ -26,193 +26,177 @@ async def show_schedule(
month: int | None = None,
genres: list[str] = constants.GENRES,
):
- client = Client(url="https://graphql.anilist.co")
- now = datetime.now()
- target_year = year if year is not None else now.year
- target_month = month if month is not None else now.month
-
- if days_offset != 0:
- start_of_day = now.replace(
- hour=0, minute=0, second=0, microsecond=0
- ) + timedelta(days=days_offset)
- end_of_day = start_of_day.replace(
- hour=23, minute=59, second=59, microsecond=999999
- )
+ client = Client(url="https://graphql.anilist.co")
+ now = datetime.now()
+ target_year = year if year is not None else now.year
+ target_month = month if month is not None else now.month
+
+ if days_offset != 0:
+ start_of_day = now.replace(
+ hour=0, minute=0, second=0, microsecond=0) + timedelta(days=days_offset)
+ end_of_day = start_of_day.replace(
+ hour=23, minute=59, second=59, microsecond=999999)
+ else:
+ start_of_day = datetime(target_year, target_month, 1).replace(
+ hour=0, minute=0, second=0, microsecond=0)
+
+ if target_month == 12:
+ end_of_day = datetime(target_year + 1, 1, 1) - timedelta(microseconds=1)
else:
- start_of_day = datetime(target_year, target_month, 1).replace(
- hour=0, minute=0, second=0, microsecond=0
- )
-
- if target_month == 12:
- end_of_day = datetime(target_year + 1, 1, 1) - timedelta(microseconds=1)
- else:
- end_of_day = datetime(target_year, target_month + 1, 1) - timedelta(
- microseconds=1
- )
-
- airing_schedules_query = Query.page().fields(
- PageFields.airing_schedules(
- airing_at_greater=int(start_of_day.timestamp()),
- episode=episode if episode else 1 if first_episode_only else None,
- airing_at_lesser=int(end_of_day.timestamp()),
- ).fields(
- AiringScheduleFields.airing_at,
- AiringScheduleFields.episode,
- AiringScheduleFields.media().fields(
- MediaFields.site_url,
- MediaFields.title().fields(
- MediaTitleFields.english(),
- MediaTitleFields.romaji(),
- MediaTitleFields.native(),
- ),
- MediaFields.country_of_origin,
- MediaFields.format,
- MediaFields.source(),
- MediaFields.genres,
- ),
- )
- )
-
- try:
- response = await client.query(
- airing_schedules_query, operation_name="get_airing_schedules"
- )
-
- if response:
- page = response.get("Page")
-
- if page:
- airing_schedules = page.get("airingSchedules")
-
- if airing_schedules:
- airing_schedules = [
- s
- for s in 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")
-
- 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")
- )
-
- 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,
- )
-
- console.print(table)
- except Exception as exception:
- print(exception)
+ end_of_day = datetime(target_year, target_month + 1,
+ 1) - timedelta(microseconds=1)
+
+ airing_schedules_query = Query.page().fields(
+ PageFields.airing_schedules(
+ airing_at_greater=int(start_of_day.timestamp()),
+ episode=episode if episode else 1 if first_episode_only else None,
+ airing_at_lesser=int(end_of_day.timestamp()),
+ ).fields(
+ AiringScheduleFields.airing_at,
+ AiringScheduleFields.episode,
+ AiringScheduleFields.media().fields(
+ MediaFields.site_url,
+ MediaFields.title().fields(
+ MediaTitleFields.english(),
+ MediaTitleFields.romaji(),
+ MediaTitleFields.native(),
+ ),
+ MediaFields.country_of_origin,
+ MediaFields.format,
+ MediaFields.source(),
+ MediaFields.genres,
+ ),
+ ))
+
+ try:
+ response = await client.query(
+ airing_schedules_query, operation_name="get_airing_schedules")
+
+ if response:
+ page = response.get("Page")
+
+ if page:
+ airing_schedules = page.get("airingSchedules")
+
+ if airing_schedules:
+ airing_schedules = [
+ s for s in 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")
+
+ 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"))
+
+ 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,
+ )
+
+ console.print(table)
+ except Exception as exception:
+ print(exception)
def format_exact_time(date):
- day = date.day
- hour = date.strftime("%I").lstrip("0")
- minute = date.strftime("%M")
+ day = date.day
+ hour = date.strftime("%I").lstrip("0")
+ minute = date.strftime("%M")
- if minute == "00":
- minute_string = ""
- else:
- minute_string = f":{minute}"
+ if minute == "00":
+ minute_string = ""
+ else:
+ minute_string = f":{minute}"
- am_pm = date.strftime("%p").replace("AM", "a.m.").replace("PM", "p.m.")
+ am_pm = date.strftime("%p").replace("AM", "a.m.").replace("PM", "p.m.")
- return date.strftime(f"%A, %B {day}, %Y, at {hour}{minute_string} {am_pm}")
+ return date.strftime(f"%A, %B {day}, %Y, at {hour}{minute_string} {am_pm}")
def format_future_airing_time(date):
- now = datetime.now()
- delta = date - now
- hours, remainder = divmod(int(delta.total_seconds()), 3600)
- minutes, _ = divmod(remainder, 60)
- time_string = date.strftime("%I:%M %p").replace("AM", "a.m.").replace("PM", "p.m.")
- parts = []
+ now = datetime.now()
+ delta = date - now
+ hours, remainder = divmod(int(delta.total_seconds()), 3600)
+ minutes, _ = divmod(remainder, 60)
+ time_string = date.strftime("%I:%M %p").replace("AM",
+ "a.m.").replace("PM", "p.m.")
+ parts = []
- if hours > 0:
- parts.append(f"{hours}h")
- if minutes > 0:
- parts.append(f"{minutes}m")
+ if hours > 0:
+ parts.append(f"{hours}h")
+ if minutes > 0:
+ parts.append(f"{minutes}m")
- if not parts:
- return f"Airing soon at {time_string}"
+ if not parts:
+ return f"Airing soon at {time_string}"
- return f"In {' '.join(parts)} at {time_string}"
+ return f"In {' '.join(parts)} at {time_string}"
def relative_time(date):
- now = datetime.now()
- delta = now - date
+ now = datetime.now()
+ delta = now - date
- if delta.days >= 365:
- years = delta.days // 365
+ if delta.days >= 365:
+ years = delta.days // 365
- return f"{years} year{'s' if years > 1 else ''} ago"
- elif delta.days >= 30:
- months = delta.days // 30
+ return f"{years} year{'s' if years > 1 else ''} ago"
+ elif delta.days >= 30:
+ months = delta.days // 30
- return f"{months} month{'s' if months > 1 else ''} ago"
- elif delta.days >= 7:
- weeks = delta.days // 7
+ return f"{months} month{'s' if months > 1 else ''} ago"
+ elif delta.days >= 7:
+ weeks = delta.days // 7
- return f"{weeks} week{'s' if weeks > 1 else ''} ago"
- elif delta.days >= 1:
- days = delta.days
+ return f"{weeks} week{'s' if weeks > 1 else ''} ago"
+ elif delta.days >= 1:
+ days = delta.days
- return f"{days} day{'s' if days > 1 else ''} ago"
- elif delta.total_seconds() >= 3600:
- hours = int(delta.total_seconds() // 3600)
+ return f"{days} day{'s' if days > 1 else ''} ago"
+ elif delta.total_seconds() >= 3600:
+ hours = int(delta.total_seconds() // 3600)
- return f"{hours} hour{'s' if hours > 1 else ''} ago"
- elif delta.total_seconds() >= 60:
- minutes = int(delta.total_seconds() // 60)
+ return f"{hours} hour{'s' if hours > 1 else ''} ago"
+ elif delta.total_seconds() >= 60:
+ minutes = int(delta.total_seconds() // 60)
- return f"{minutes} minute{'s' if minutes > 1 else ''} ago"
- else:
- seconds = int(delta.total_seconds())
+ return f"{minutes} minute{'s' if minutes > 1 else ''} ago"
+ else:
+ seconds = int(delta.total_seconds())
- return f"{seconds} second{'s' if seconds > 1 else ''} ago"
+ return f"{seconds} second{'s' if seconds > 1 else ''} ago"