-
-
Notifications
You must be signed in to change notification settings - Fork 40
Improve the wiki searching code #684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,3 +1,4 @@ | ||||||||||||||||||
| import discord | ||||||||||||||||||
| import httpx | ||||||||||||||||||
| from discord.ext import commands | ||||||||||||||||||
| from loguru import logger | ||||||||||||||||||
|
|
@@ -10,57 +11,58 @@ | |||||||||||||||||
| class Wiki(commands.Cog): | ||||||||||||||||||
| def __init__(self, bot: Tux) -> None: | ||||||||||||||||||
| self.bot = bot | ||||||||||||||||||
| self.arch_wiki_base_url = "https://wiki.archlinux.org/api.php" | ||||||||||||||||||
| self.atl_wiki_base_url = "https://atl.wiki/api.php" | ||||||||||||||||||
| self.arch_wiki_api_url = "https://wiki.archlinux.org/api.php" | ||||||||||||||||||
| self.atl_wiki_api_url = "https://atl.wiki/api.php" | ||||||||||||||||||
| self.wiki.usage = generate_usage(self.wiki) | ||||||||||||||||||
| self.arch_wiki.usage = generate_usage(self.arch_wiki) | ||||||||||||||||||
| self.atl_wiki.usage = generate_usage(self.atl_wiki) | ||||||||||||||||||
|
|
||||||||||||||||||
| def query_arch_wiki(self, search_term: str) -> tuple[str, str]: | ||||||||||||||||||
| def create_embed(self, title: tuple[str, str], ctx: commands.Context[Tux]) -> discord.Embed: | ||||||||||||||||||
| """ | ||||||||||||||||||
| Query the ArchWiki API for a search term and return the title and URL of the first search result. | ||||||||||||||||||
| Create a Discord embed message based on the search result. | ||||||||||||||||||
|
|
||||||||||||||||||
| Parameters | ||||||||||||||||||
| ---------- | ||||||||||||||||||
| search_term : str | ||||||||||||||||||
| The search term to query the ArchWiki API with. | ||||||||||||||||||
| title : tuple[str, str] | ||||||||||||||||||
| A tuple containing the title and description of the search result. | ||||||||||||||||||
| If the first element is "error", it indicates no search results were found. | ||||||||||||||||||
| ctx : commands.Context[Tux] | ||||||||||||||||||
| The context object for the command. | ||||||||||||||||||
|
|
||||||||||||||||||
| Returns | ||||||||||||||||||
| ------- | ||||||||||||||||||
| tuple[str, str] | ||||||||||||||||||
| The title and URL of the first search result. | ||||||||||||||||||
| discord.Embed | ||||||||||||||||||
| The created embed message. | ||||||||||||||||||
| """ | ||||||||||||||||||
| if title[0] == "error": | ||||||||||||||||||
| embed = EmbedCreator.create_embed( | ||||||||||||||||||
| bot=self.bot, | ||||||||||||||||||
| embed_type=EmbedCreator.ERROR, | ||||||||||||||||||
| user_name=ctx.author.name, | ||||||||||||||||||
| user_display_avatar=ctx.author.display_avatar.url, | ||||||||||||||||||
| description="No search results found.", | ||||||||||||||||||
| ) | ||||||||||||||||||
| else: | ||||||||||||||||||
| embed = EmbedCreator.create_embed( | ||||||||||||||||||
| bot=self.bot, | ||||||||||||||||||
| embed_type=EmbedCreator.INFO, | ||||||||||||||||||
| user_name=ctx.author.name, | ||||||||||||||||||
| user_display_avatar=ctx.author.display_avatar.url, | ||||||||||||||||||
| title=title[0], | ||||||||||||||||||
| description=title[1], | ||||||||||||||||||
| ) | ||||||||||||||||||
| return embed | ||||||||||||||||||
|
|
||||||||||||||||||
| search_term = search_term.capitalize() | ||||||||||||||||||
|
|
||||||||||||||||||
| params: dict[str, str] = { | ||||||||||||||||||
| "action": "opensearch", | ||||||||||||||||||
| "format": "json", | ||||||||||||||||||
| "limit": "1", | ||||||||||||||||||
| "search": search_term, | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| # Send a GET request to the ArchWiki API | ||||||||||||||||||
| with httpx.Client() as client: | ||||||||||||||||||
| response = client.get(self.arch_wiki_base_url, params=params) | ||||||||||||||||||
| logger.info(f"GET request to {self.arch_wiki_base_url} with params {params}") | ||||||||||||||||||
|
|
||||||||||||||||||
| # example response: ["pacman",["Pacman"],[""],["https://wiki.archlinux.org/title/Pacman"]] | ||||||||||||||||||
|
|
||||||||||||||||||
| # Check if the request was successful | ||||||||||||||||||
| if response.status_code == 200: | ||||||||||||||||||
| data = response.json() | ||||||||||||||||||
| return (data[1][0], data[3][0]) if data[1] else ("error", "error") | ||||||||||||||||||
| return "error", "error" | ||||||||||||||||||
|
|
||||||||||||||||||
| def query_atl_wiki(self, search_term: str) -> tuple[str, str]: | ||||||||||||||||||
| def query_wiki(self, base_url: str, search_term: str) -> tuple[str, str]: | ||||||||||||||||||
| """ | ||||||||||||||||||
| Query the atl.wiki API for a search term and return the title and URL of the first search result. | ||||||||||||||||||
| Query a wiki API for a search term and return the title and URL of the first search result. | ||||||||||||||||||
|
|
||||||||||||||||||
| Parameters | ||||||||||||||||||
| ---------- | ||||||||||||||||||
| base_url : str | ||||||||||||||||||
| The base URL of the wiki API. | ||||||||||||||||||
| search_term : str | ||||||||||||||||||
| The search term to query the atl.wiki API with. | ||||||||||||||||||
| The search term to query the wiki API with. | ||||||||||||||||||
|
|
||||||||||||||||||
| Returns | ||||||||||||||||||
| ------- | ||||||||||||||||||
|
|
@@ -70,24 +72,28 @@ def query_atl_wiki(self, search_term: str) -> tuple[str, str]: | |||||||||||||||||
|
|
||||||||||||||||||
| search_term = search_term.capitalize() | ||||||||||||||||||
|
|
||||||||||||||||||
| params: dict[str, str] = { | ||||||||||||||||||
| "action": "opensearch", | ||||||||||||||||||
| "format": "json", | ||||||||||||||||||
| "limit": "1", | ||||||||||||||||||
| "search": search_term, | ||||||||||||||||||
| } | ||||||||||||||||||
| params: dict[str, str] = {"action": "query", "format": "json", "list": "search", "srsearch": search_term} | ||||||||||||||||||
|
|
||||||||||||||||||
| # Send a GET request to the ATL Wiki API | ||||||||||||||||||
| # Send a GET request to the wiki API | ||||||||||||||||||
| with httpx.Client() as client: | ||||||||||||||||||
| response = client.get(self.atl_wiki_base_url, params=params) | ||||||||||||||||||
| logger.info(f"GET request to {self.atl_wiki_base_url} with params {params}") | ||||||||||||||||||
|
|
||||||||||||||||||
| # example response: ["pacman",["Pacman"],[""],["https://atl.wiki/title/Pacman"]] | ||||||||||||||||||
| response = client.get(base_url, params=params) | ||||||||||||||||||
| logger.info(f"GET request to {base_url} with params {params}") | ||||||||||||||||||
|
|
||||||||||||||||||
| # Check if the request was successful | ||||||||||||||||||
| if response.status_code == 200: | ||||||||||||||||||
| data = response.json() | ||||||||||||||||||
| return (data[1][0], data[3][0]) if data[1] else ("error", "error") | ||||||||||||||||||
| logger.info(data) | ||||||||||||||||||
| if data.get("query") and data["query"].get("search"): | ||||||||||||||||||
|
Comment on lines
84
to
+86
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚨 suggestion (security): Consider logging only relevant fields to avoid potential sensitive data exposure
Suggested change
|
||||||||||||||||||
| search_results = data["query"]["search"] | ||||||||||||||||||
| if search_results: | ||||||||||||||||||
|
Comment on lines
+87
to
+88
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code-quality): Use named expression to simplify assignment and conditional (
Suggested change
|
||||||||||||||||||
| title = search_results[0]["title"] | ||||||||||||||||||
| url_title = title.replace(" ", "_") | ||||||||||||||||||
| if "atl.wiki" in base_url: | ||||||||||||||||||
| url = f"https://atl.wiki/{url_title}" | ||||||||||||||||||
| else: | ||||||||||||||||||
| url = f"https://wiki.archlinux.org/title/{url_title}" | ||||||||||||||||||
| return title, url | ||||||||||||||||||
| return "error", "error" | ||||||||||||||||||
| return "error", "error" | ||||||||||||||||||
|
|
||||||||||||||||||
| @commands.hybrid_group( | ||||||||||||||||||
|
|
@@ -122,26 +128,9 @@ async def arch_wiki(self, ctx: commands.Context[Tux], query: str) -> None: | |||||||||||||||||
| The search query. | ||||||||||||||||||
| """ | ||||||||||||||||||
|
|
||||||||||||||||||
| title: tuple[str, str] = self.query_arch_wiki(query) | ||||||||||||||||||
|
|
||||||||||||||||||
| if title[0] == "error": | ||||||||||||||||||
| embed = EmbedCreator.create_embed( | ||||||||||||||||||
| bot=self.bot, | ||||||||||||||||||
| embed_type=EmbedCreator.ERROR, | ||||||||||||||||||
| user_name=ctx.author.name, | ||||||||||||||||||
| user_display_avatar=ctx.author.display_avatar.url, | ||||||||||||||||||
| description="No search results found.", | ||||||||||||||||||
| ) | ||||||||||||||||||
| title: tuple[str, str] = self.query_wiki(self.arch_wiki_api_url, query) | ||||||||||||||||||
|
|
||||||||||||||||||
| else: | ||||||||||||||||||
| embed = EmbedCreator.create_embed( | ||||||||||||||||||
| bot=self.bot, | ||||||||||||||||||
| embed_type=EmbedCreator.INFO, | ||||||||||||||||||
| user_name=ctx.author.name, | ||||||||||||||||||
| user_display_avatar=ctx.author.display_avatar.url, | ||||||||||||||||||
| title=title[0], | ||||||||||||||||||
| description=title[1], | ||||||||||||||||||
| ) | ||||||||||||||||||
| embed = self.create_embed(title, ctx) | ||||||||||||||||||
|
|
||||||||||||||||||
| await ctx.send(embed=embed) | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -160,26 +149,9 @@ async def atl_wiki(self, ctx: commands.Context[Tux], query: str) -> None: | |||||||||||||||||
| The search query. | ||||||||||||||||||
| """ | ||||||||||||||||||
|
|
||||||||||||||||||
| title: tuple[str, str] = self.query_atl_wiki(query) | ||||||||||||||||||
| title: tuple[str, str] = self.query_wiki(self.atl_wiki_api_url, query) | ||||||||||||||||||
|
|
||||||||||||||||||
| if title[0] == "error": | ||||||||||||||||||
| embed = EmbedCreator.create_embed( | ||||||||||||||||||
| bot=self.bot, | ||||||||||||||||||
| embed_type=EmbedCreator.ERROR, | ||||||||||||||||||
| user_name=ctx.author.name, | ||||||||||||||||||
| user_display_avatar=ctx.author.display_avatar.url, | ||||||||||||||||||
| description="No search results found.", | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| else: | ||||||||||||||||||
| embed = EmbedCreator.create_embed( | ||||||||||||||||||
| bot=self.bot, | ||||||||||||||||||
| embed_type=EmbedCreator.INFO, | ||||||||||||||||||
| user_name=ctx.author.name, | ||||||||||||||||||
| user_display_avatar=ctx.author.display_avatar.url, | ||||||||||||||||||
| title=title[0], | ||||||||||||||||||
| description=title[1], | ||||||||||||||||||
| ) | ||||||||||||||||||
| embed = self.create_embed(title, ctx) | ||||||||||||||||||
|
|
||||||||||||||||||
| await ctx.send(embed=embed) | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (code-quality): We've found these issues:
assign-if-exp)inline-immediately-returned-variable)