Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
✨ add feed as a parameter (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsthejoker authored May 18, 2023
1 parent a589c98 commit e6961f8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
20 changes: 19 additions & 1 deletion buttercup/cogs/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class SearchCacheItem(TypedDict):
# The time restriction for the search
after_time: Optional[datetime]
before_time: Optional[datetime]
feed: Optional[str]
time_str: str
# The current Discord page for the query
cur_page: int
Expand Down Expand Up @@ -263,10 +264,12 @@ async def _search_from_cache(
user_id = user["id"] if user else None
after_time = cache_item["after_time"]
before_time = cache_item["before_time"]
feed = cache_item["feed"]
time_str = cache_item["time_str"]

from_str = after_time.isoformat() if after_time else None
until_str = before_time.isoformat() if before_time else None
feed = feed if feed else None

request_page = (discord_page * self.discord_page_size) // self.request_page_size

Expand All @@ -277,6 +280,7 @@ async def _search_from_cache(
"author": user_id,
"create_time__gte": from_str,
"create_time__lte": until_str,
"feed": feed,
"url__isnull": False,
"ordering": "-create_time",
"page_size": self.request_page_size,
Expand All @@ -295,6 +299,7 @@ async def _search_from_cache(
query=query,
user=get_username(user),
time_str=time_str,
feed_str=feed if feed else "all feeds",
duration_str=get_duration_str(start),
)
)
Expand All @@ -310,6 +315,7 @@ async def _search_from_cache(
"user": cache_item["user"],
"after_time": after_time,
"before_time": before_time,
"feed": feed,
"time_str": time_str,
"cur_page": discord_page,
"discord_user_id": cache_item["discord_user_id"],
Expand Down Expand Up @@ -394,6 +400,12 @@ async def _search_from_cache(
option_type=3,
required=False,
),
create_option(
name="feed",
description="Only show transcriptions from this feed.",
option_type=3,
required=False,
),
],
)
async def search(
Expand All @@ -403,16 +415,21 @@ async def search(
username: str = "me",
after: Optional[str] = None,
before: Optional[str] = None,
feed: Optional[str] = None,
) -> None:
"""Search for transcriptions containing the given text."""
start = datetime.now(tz=pytz.UTC)
after_time, before_time, time_str = parse_time_constraints(after, before)
feed_str = feed if feed else "all feeds"

# Send a first message to show that the bot is responsive.
# We will edit this message later with the actual content.
msg = await ctx.send(
i18n["search"]["getting_search"].format(
query=query, user=get_initial_username(username, ctx), time_str=time_str
query=query,
user=get_initial_username(username, ctx),
time_str=time_str,
feed_str=feed_str,
)
)

Expand All @@ -424,6 +441,7 @@ async def search(
"user": user,
"after_time": after_time,
"before_time": before_time,
"feed": feed,
"time_str": time_str,
"cur_page": 0,
"discord_user_id": ctx.author_id,
Expand Down
4 changes: 2 additions & 2 deletions buttercup/strings/en_US.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ leaderboard:
Leaderboard for {user} ({time_frame})
search:
getting_search: |-
Searching for `{query}` in transcriptions by {user} {time_str}...
Searching for `{query}` in transcriptions by {user} {time_str} in {feed_str}...
no_results: |-
No results found for `{query}` by {user} {time_str}. ({duration_str})
No results found for `{query}` by {user} {time_str} in {feed_str}. ({duration_str})
description:
item: |-
{num}. [{tr_type} on {tr_source}]({url}) {timestamp}
Expand Down

0 comments on commit e6961f8

Please sign in to comment.