From 4c07e94640f1619a6520ad545334e662360e5e7f Mon Sep 17 00:00:00 2001 From: Andrew Linim Date: Mon, 4 Nov 2024 17:34:13 +0300 Subject: [PATCH] user can request few images instead of one --- api.py | 13 ++++++++++--- main.py | 28 +++++++++++++++++++++------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/api.py b/api.py index 2394ea7..eeea09b 100644 --- a/api.py +++ b/api.py @@ -3,10 +3,17 @@ import creds gis = GoogleImagesSearch(creds.api, creds.cx) -def get_img(query): +def get_img(query, count): gis.search(search_params= { 'q': query, 'safe': 'active', - 'num': 1, + 'num': count, }) - return gis.results()[0].url \ No newline at end of file + if count == 1: + return gis.results()[0].url + else: + results = "" + response = gis.results() + for result in response: + results += f"{result.url}\n" + return results \ No newline at end of file diff --git a/main.py b/main.py index bb9e856..0e57523 100644 --- a/main.py +++ b/main.py @@ -16,13 +16,10 @@ class Client(commands.CommandsClient): @commands.command() async def gimg(self, ctx: commands.Context, *args): - """Get image from Google Images""" + """[count of images, 1 by default] - 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']: @@ -31,14 +28,31 @@ class Client(commands.CommandsClient): else: pass - if banned == False: + try: + count = int(args[0]) + for word in range(1, len(args)): + arg += f"{args[word]} " + except: + count = 1 + for word in args: + arg += f"{word} " + + + if count>10: + toomanyimages = True + else: + toomanyimages = False + + if toomanyimages == False and banned == False: try: - url = get_img(arg) # requesting image + url = get_img(arg, count) # requesting image await ctx.send(f"Search query: {arg}\n{url}") # sending image via embed except IndexError: await ctx.send("No images found") - else: + elif banned == True: await ctx.send(f"Your search query contains banned words") + elif toomanyimages == True: + await ctx.send(f"You requested too many images (>10)") async def main():