Python random 模块,cat() 实例源码
我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用random.cat()。
def randomcat(self, ctx):
"""Meow."""
channel = ctx.message.channel
author = ctx.message.author
server = ctx.message.server
if not self.canDisplay(server):
return
url = 'http://random.cat/meow'
# Grab our image url
r = requests.get(url, headers = {'User-agent': self.ua})
catURL = r.json()['file']
await GetImage.get(catURL, self.bot, channel, 'A cat for you!', self.ua)
def dog(self, ctx):
"""Get a random cat image!
**Usage:** `g_dog`
**Permission:** User"""
isVideo = True
while isVideo:
r = requests.get('https://random.dog/woof.json')
js = r.json()
if js['url'].endswith('.mp4'):
pass
else:
isVideo = False
colours = [0x1abc9c, 0x11806a, 0x2ecc71, 0x1f8b4c, 0x3498db, 0x206694, 0x9b59b6, 0x71368a, 0xe91e63, 0xad1457, 0xf1c40f, 0xc27c0e, 0xa84300, 0xe74c3c, 0x992d22, 0x95a5a6, 0x607d8b, 0x979c9f, 0x546e7a]
col = int(random.random() * len(colours))
content = [":dog: Don't be sad! This doggy wants to play with you!", "You seem lonely, {0.mention}. Here, have a dog. They're not as nice as cats, but enjoy!".format(ctx.message.author), "Weuf, woof, woooooooooof. Woof you.", "Pupper!", "Meow... wait wrong animal."]
con = int(random.random() * len(content))
em = discord.Embed(color=colours[col])
em.set_image(url=js['url'])
await ctx.send(content=content[con], embed=em)
def cat(ctx):
"""Grabs a random cat picture"""
r = requests.get("https://random.cat/meow")
r = str(r.content)
r = r.replace("b'","")
r = r.replace("'","")
r = r.replace("\\","")
url = json.loads(r)["file"]
await client.say(url)
i = random.randint(0, 29)
if i == 0:
await asyncio.sleep(3)
await client.say("MEOW.")
elif i == 1:
await asyncio.sleep(3)
await client.say("I love cats.")
elif i == 2:
await asyncio.sleep(3)
await client.say("*purr...*")
# RANDOM DOG
def cat(self, event):
# Sometimes random.cat gives us gifs (smh)
for _ in range(3):
try:
r = requests.get('http://random.cat/meow')
r.raise_for_status()
except:
continue
url = r.json()['file']
if not url.endswith('.gif'):
break
else:
return event.msg.reply('404 cat not found :(')
r = requests.get(url)
r.raise_for_status()
event.msg.reply('', attachments=[('cat.jpg', r.content)])
def cats(self, ctx):
async with aiohttp.get('http://random.cat/meow') as r:
if r.status == 200:
js = await r.json()
await self.bot.say(js['file'])
def cat(self, ctx):
"""Get a random cat image!
**Usage:** `g_cat`
**Permission:** User"""
colours = [0x1abc9c, 0x11806a, 0x2ecc71, 0x1f8b4c, 0x3498db, 0x206694, 0x9b59b6, 0x71368a, 0xe91e63, 0xad1457, 0xf1c40f, 0xc27c0e, 0xa84300, 0xe74c3c, 0x992d22, 0x95a5a6, 0x607d8b, 0x979c9f, 0x546e7a]
col = int(random.random() * len(colours))
content = [";w; Don't be sad, here's a cat!", "You seem lonely, {0.mention}. Here, have a cat".format(ctx.message.author), "Meeeooowwww!", "Awww, so cute! Look at the kitty!!1!", "Woof... wait wrong animal."]
con = int(random.random() * len(content))
r = requests.get('http://random.cat/meow')
js = r.json()
em = discord.Embed(color=colours[col])
em.set_image(url=js['file'])
await ctx.send(content=content[con], embed=em)
def cat(self, ctx):
"""Fetch a random cat."""
async with ctx.bot.session.get(URL_RANDOM_CAT_API) as response:
if response.status == 200:
data = await response.text()
catto = json.loads(data)
url = catto["file"]
await ctx.send(url)
else:
await ctx.send("Could not reach random.cat. :<")
def cat(self, ctx):
"""Get a random cat! Because why not.
Usage: cat"""
with async_timeout.timeout(8):
async with self.bot.cog_http.get('http://random.cat/meow') as response:
ret = await response.json()
e = discord.Embed(color=random.randint(1, 255**3-1))
e.set_image(url=ret['file'])
await ctx.send(embed=e)
def randomcat(self, ctx):
"""Display a random cat"""
r = requests.get('http://random.cat/meow.php')
cat = str(r.json()['file'])
embed = discord.Embed(title="Meow", description="[Voir le chat plus grand]({})".format(cat), colour=0x03C9A9)
embed.set_thumbnail(url=cat)
embed.set_author(name="Random.cat", url='https://random.cat/', icon_url='http://outout.tech/tuxbot/nyancat2.gif')
await ctx.send(embed=embed)
def neko(self, ctx):
'''Zufällige Katzen Bilder nyan~'''
#http://discordpy.readthedocs.io/en/latest/faq.html#what-does-blocking-mean
async with aiohttp.ClientSession() as cs:
async with cs.get('http://random.cat/meow') as r:
res = await r.json()
emojis = [':cat2: ', ':cat: ', ':heart_eyes_cat: ']
await ctx.send(random.choice(emojis) + res['file'])
def cat(self, ctx):
with ctx.channel.typing():
with aiohttp.ClientSession() as session:
async with session.get("http://random.cat/meow") as r:
r = await r.read()
url = json.JSONDecoder().decode(r.decode("utf-8"))["file"]
await ctx.send(embed=discord.Embed(title="Random Cat").set_image(url=url).set_footer(text="Powered by random.cat"))