user can request few images instead of one

This commit is contained in:
Andrew Linim 2024-11-04 17:34:13 +03:00
parent 6ccc44a675
commit 4c07e94640
2 changed files with 31 additions and 10 deletions

13
api.py
View file

@ -3,10 +3,17 @@ import creds
gis = GoogleImagesSearch(creds.api, creds.cx) gis = GoogleImagesSearch(creds.api, creds.cx)
def get_img(query): def get_img(query, count):
gis.search(search_params= { gis.search(search_params= {
'q': query, 'q': query,
'safe': 'active', 'safe': 'active',
'num': 1, 'num': count,
}) })
return gis.results()[0].url if count == 1:
return gis.results()[0].url
else:
results = ""
response = gis.results()
for result in response:
results += f"{result.url}\n"
return results

28
main.py
View file

@ -16,13 +16,10 @@ class Client(commands.CommandsClient):
@commands.command() @commands.command()
async def gimg(self, ctx: commands.Context, *args): 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 = "" arg = ""
for word in args: # very stupid way to get args
arg += f"{word} "
banned = False banned = False
if gimgsettings['usestoplist'] == True: if gimgsettings['usestoplist'] == True:
for banword in gimgsettings['stoplist']: for banword in gimgsettings['stoplist']:
@ -31,14 +28,31 @@ class Client(commands.CommandsClient):
else: else:
pass 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: 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 await ctx.send(f"Search query: {arg}\n{url}") # sending image via embed
except IndexError: except IndexError:
await ctx.send("No images found") await ctx.send("No images found")
else: elif banned == True:
await ctx.send(f"Your search query contains banned words") 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(): async def main():