From b1d64507a6dda2bd4feb5c5f051022a9d791dce2 Mon Sep 17 00:00:00 2001 From: Anurag Patil Date: Sat, 21 Mar 2020 22:07:15 +0530 Subject: [PATCH] added anime and manga search command --- config-example.json | 43 --------------- extensions/search.py | 106 +++++++++++++++++++++++++++++++++--- extensions/utils/logging.py | 2 +- 3 files changed, 98 insertions(+), 53 deletions(-) delete mode 100644 config-example.json diff --git a/config-example.json b/config-example.json deleted file mode 100644 index d01e716..0000000 --- a/config-example.json +++ /dev/null @@ -1,43 +0,0 @@ -{ - "VERSION": "1.9 testing", - "DESCRIPTION": "a minimalist search utility bot for discord, designed by taciturasa.", - "REPO": "https://github.com/taciturasa/searchbot-discord", - "SERVER": "https://discord.gg/4BpReNV", - "TOKEN": "", - "CACHE": false, - "PREFIX": ["search!"], - - "MAINTENANCE": false, - "CASE_INSENSITIVE": true, - "PREFIXLESS_DMS": true, - "MENTION_ASSIST": true, - "CUSTOM_HELP": true, - - "PERMS": 378944, - "BLOCKED": [], - - "BOTLISTS": { - "DBL": "", - "DBOTS": "", - "BOD": "", - "DBLCOM": "", - "BLSPACE": "", - "TABFT_LINK": "", - "DAD": "" - }, - - "HOOKS": { - "INFO_HOOK": "", - "WARN_HOOK": "", - "ERROR_HOOK": "", - "DEBUG_HOOK": "" - }, - - "RETHINK": { - "DB": "", - "USERNAME": "", - "PASSWORD": "", - "HOST": "", - "PORT": null - } -} \ No newline at end of file diff --git a/extensions/search.py b/extensions/search.py index eea9062..da1d504 100644 --- a/extensions/search.py +++ b/extensions/search.py @@ -29,8 +29,7 @@ class Search(commands.Cog): with open('searxes.txt') as f: self.instances = f.read().split('\n') - - async def _search_logic(self, query: str, is_nsfw: bool = False, + async def _search_logic(self, query: str, is_nsfw: bool = False, category: str = None) -> str: """Provides search logic for all search commands.""" @@ -207,7 +206,7 @@ class Search(commands.Cog): content=f"**{ctx.author}** searched for `{query}` videos in \"{ctx.guild}\" and got this:" f"\n\n{msg}", name="Search Results" - ) + ) await ctx.send(msg) @commands.command() @@ -220,7 +219,7 @@ class Search(commands.Cog): content=f"**{ctx.author}** searched for `{query}` music in \"{ctx.guild}\" and got this:" f"\n\n{msg}", name="Search Results" - ) + ) await ctx.send(msg) @commands.command(aliases=['file']) @@ -233,7 +232,7 @@ class Search(commands.Cog): content=f"**{ctx.author}** searched for `{query}` files in \"{ctx.guild}\" and got this:" f"\n\n{msg}", name="Search Results" - ) + ) await ctx.send(msg) @commands.command(aliases=['image']) @@ -247,7 +246,7 @@ class Search(commands.Cog): content=f"**{ctx.author}** searched for `{query}` images in \"{ctx.guild}\" and got this:" f"\n\n{msg}", name="Search Results" - ) + ) await ctx.send(msg) @commands.command() @@ -275,9 +274,99 @@ class Search(commands.Cog): content=f"**{ctx.author}** searched for `{query}` maps in \"{ctx.guild}\" and got this:" f"\n\n{msg}", name="Search Results" - ) + ) await ctx.send(msg) + @commands.command() + async def anime(self, ctx, *, query: str): + """Lookup anime information online, uses the https://kitsu.io/ public API.""" + base = "https://kitsu.io/api/edge/" + # Handling + async with ctx.typing(): + async with aiohttp.ClientSession() as session: + async with session.get(base + "anime", params={"filter[text]": query}) as resp: + resp = await resp.json() + resp = resp['data'] + if not resp: + return await ctx.send("The requested anime coudn't be found") + + anime = resp[0] + title = f'{anime["attributes"]["canonicalTitle"]}' + anime_id = anime["id"] + url = f"https://kitsu.io/anime/{anime_id}" + + embed = discord.Embed( + title=f"{title}", color=ctx.author.color, url=url) + embed.description = anime["attributes"]["synopsis"][0:425] + "..." + embed.add_field(name="Average Rating", + value=anime["attributes"]["averageRating"]) + embed.add_field(name="Popularity Rank", + value=anime["attributes"]["popularityRank"]) + embed.add_field(name="Age Rating", + value=anime["attributes"]["ageRating"]) + embed.add_field( + name="Status", value=anime["attributes"]["status"]) + thing = '' if not anime['attributes'][ + 'endDate'] else f' to {anime["attributes"]["endDate"]}' + embed.add_field( + name="Aired", value=f"{anime['attributes']['startDate']}{thing}") + embed.add_field(name="Episodes", + value=anime['attributes']["episodeCount"]) + embed.add_field( + name="Type", value=anime['attributes']["showType"]) + embed.set_thumbnail( + url=anime['attributes']["posterImage"]["original"]) + embed.set_footer( + text=f"Requested by {ctx.author.name} | Powered by kitsu.io", icon_url=ctx.author.avatar_url_as(format="png")) + await ctx.send(embed=embed) + session.close() + + @commands.command() + async def manga(self, ctx, *, query: str): + """Lookup manga information online, uses the https://kitsu.io/ public API.""" + base = "https://kitsu.io/api/edge/" + # Handling + async with ctx.typing(): + async with aiohttp.ClientSession() as session: + async with session.get(base + "manga", params={"filter[text]": query}) as resp: + resp = await resp.json() + resp = resp['data'] + if not resp: + return await ctx.send("The requested manga coudn't be found") + + manga = resp[0] + title = f'{manga["attributes"]["canonicalTitle"]}' + manga_id = manga["id"] + url = f"https://kitsu.io/manga/{manga_id}" + + embed = discord.Embed( + title=f"{title}", color=ctx.author.color, url=url) + embed.description = manga["attributes"]["synopsis"][0:425] + "..." + if manga["attributes"]["averageRating"]: + embed.add_field(name="Average Rating", + value=manga["attributes"]["averageRating"]) + embed.add_field(name="Popularity Rank", + value=manga["attributes"]["popularityRank"]) + if manga["attributes"]["ageRating"]: + embed.add_field(name="Age Rating", + value=manga["attributes"]["ageRating"]) + embed.add_field( + name="Status", value=manga["attributes"]["status"]) + thing = '' if not manga['attributes'][ + 'endDate'] else f' to {manga["attributes"]["endDate"]}' + embed.add_field( + name="Published", value=f"{manga['attributes']['startDate']}{thing}") + if manga['attributes']['chapterCount']: + embed.add_field(name="Chapters", + value=manga['attributes']["chapterCount"]) + embed.add_field( + name="Type", value=manga['attributes']["mangaType"]) + embed.set_thumbnail( + url=manga['attributes']["posterImage"]["original"]) + + await ctx.send(embed=embed) + session.close() + @commands.command() @commands.is_owner() async def rejson(self, ctx): @@ -313,7 +402,7 @@ class Search(commands.Cog): if isinstance(error, commands.CommandNotFound) or \ isinstance(error, commands.CheckFailure): - + # Handling async with ctx.typing(): # Prepares term @@ -330,7 +419,6 @@ class Search(commands.Cog): name="Search Results" ) - # Sends result await ctx.send(msg) diff --git a/extensions/utils/logging.py b/extensions/utils/logging.py index 88a2560..43f2334 100644 --- a/extensions/utils/logging.py +++ b/extensions/utils/logging.py @@ -132,7 +132,7 @@ class Logging(): async def error(self, error: Exception, ctx: Context, name: Optional[str]): """Logs errors and sends them to the appropriate places.""" - + # Prerequisites error_embed = await self._create_error_embed(error, ctx)