Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

python - How can I generate a random Youtube link without using Youtube API in Discord.py

I am creating a discord bot and using python and I want it to be able to generate a random or pseudo-random youtube link. Most people use youtube API for this, but I am limited on what I could insert into my bot. What I did was generate a random 11 character alphanumeric string and insert it into a partial youtube URL. The only problem is that nearly every link generated doesn't exist. Would there be a way to loop the link generation until it finds an existing link in discord.py async?

Here is what I have so far:

    import discord
    import random
    import string

linkCode = h81

def getRandom():
        letters_and_digits = string.ascii_letters + string.digits
        global linkCode
        linkCode = ''.join((random.choice(letters_and_digits) for i in range(11)))

    @client.command()
    async def randtube(ctx):
        getRandom()
        await ctx.send(f"https://www.youtube.com/watch?v={linkCode}")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...