user can request few images instead of one
This commit is contained in:
parent
6ccc44a675
commit
4c07e94640
2 changed files with 31 additions and 10 deletions
13
api.py
13
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
|
||||
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
28
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():
|
||||
|
|
Loading…
Reference in a new issue