diff --git a/api.py b/api.py new file mode 100644 index 0000000..2394ea7 --- /dev/null +++ b/api.py @@ -0,0 +1,12 @@ +from google_images_search import GoogleImagesSearch +import creds + +gis = GoogleImagesSearch(creds.api, creds.cx) + +def get_img(query): + gis.search(search_params= { + 'q': query, + 'safe': 'active', + 'num': 1, + }) + return gis.results()[0].url \ No newline at end of file diff --git a/creds.py b/creds.py new file mode 100644 index 0000000..8e2518e --- /dev/null +++ b/creds.py @@ -0,0 +1,5 @@ +bot = "" # bot api-key from next +api = "" # google api key +cx = "" # google cx + +# tutorial for api&cx - https://pypi.org/project/Google-Images-Search/ \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..bb9e856 --- /dev/null +++ b/main.py @@ -0,0 +1,50 @@ +import asyncio +import aiohttp +import next +import creds +from next.ext import commands +from api import get_img + +gimgsettings = { + "usestoplist": True, + "stoplist": ["пенис", "хуй", "шлюха", "penis", "pride", 'lgbt', "лгбт", "прайд", "dick", "1488", "swastika", "свастика", "свастон"] +} + +class Client(commands.CommandsClient): + async def get_prefix(self, message: next.Message): + return "!" + + @commands.command() + async def gimg(self, ctx: commands.Context, *args): + """Get image from Google Images""" + + arg = "" + + for word in args: # very stupid way to get args + arg += f"{word} " + + banned = False + if gimgsettings['usestoplist'] == True: + for banword in gimgsettings['stoplist']: + if banword in arg: + banned = True + else: + pass + + if banned == False: + try: + url = get_img(arg) # requesting image + await ctx.send(f"Search query: {arg}\n{url}") # sending image via embed + except IndexError: + await ctx.send("No images found") + else: + await ctx.send(f"Your search query contains banned words") + + +async def main(): + async with aiohttp.ClientSession() as session: + client = Client(session, creds.bot) + print("Running GIMG") + await client.start() + +asyncio.run(main())