0
votes

When I am trying to post an Imgur link in my discord server, it is not working.

The main thing that I am trying to achieve here is to allow the user to search for a random image in a subcategory of Imgur, then have that image posted to them in an embed. Whenever I post a link, it is not showing up. This also does not work for the bot. To add, when I tried to use embedVar.set_image, the URL was not posted in the embed, and it just showed the title. Here is my code if it helps (without embedVar.set_image)

@client.command()
async def image(ctx, search):
    html_page = urlopen("https://imgur.com/r/" + search)
    soup = BeautifulSoup(html_page)
    search_images = []
    for img in soup.findAll('img'):
        search_images.append(img.get('src'))
    search_command = random.choice(search_images)
    embedVar = discord.Embed(title = search)
    search_url="https:" + search_command.translate({ord(i): None for i in "<>"})
    embedVar.add_field(name = search, value = search_url)
    await ctx.send(embed = embedVar)

There is also an image attached to explain my problem in further detail. Any form of help is appreciated. Thanks!

https://i.stack.imgur.com/Fte0p.png

1

1 Answers

0
votes

I would suggest using imgur api, you need to get your Client-ID from here. This would send it like this.

enter image description here

import requests

@bot.command()
async def image(ctx, search):
    url = f"https://api.imgur.com/3/gallery/search/?q={search}"

    payload = {}
    files = {}
    headers = {
        'Authorization': 'Client-ID CLIENT_ID_HERE'
    }

    response = requests.request("GET", url, headers=headers, data=payload, files=files)
    res = json.loads(response.text.encode('UTF-8'))

    search_url = res['data'][random.randint(0, 10)]['link']
    await ctx.send(search_url)