Random cat api python

Send Random Cat Images Discord Python Bot

$ python3 -m pip install -U discord.py requests 
py -3 -m pip install -U discord.py 
import discord, requests from discord.ext import commands

Now, let's make the function that will get a random cat's image/gif URL and send that in a nice discord embed.

@client.command() async def catto(ctx): r = requests.get("https://api.thecatapi.com/v1/images/search").json() cat_embed = discord.Embed() cat_embed.set_image(url=f"") await ctx.send(embed=cat_embed)
requests.get() will fetch the response from the API. 
discord.Embed() is a discord's class which is used to created discord embed. 
After initializing the embed we grabbed the URL part from the response and simply set that URL 
as the image of the embed.
Finally we sent the embed as the bot's response.
To run our bot, we need to call the run() function and pass in the bot's token.
import discord, requests from discord.ext import commands TOKEN = "your-token-number" @client.command() async def catto(ctx): r = requests.get("https://api.thecatapi.com/v1/images/search").json() cat_em = discord.Embed() cat_em.set_image(url=f'') await ctx.send(embed=cat_em) client.run(TOKEN)

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Modul to get random cat images

License

gravmatt/random-cat

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

random-cat is a amazing modul to get cat images. This Project won't be posible without the great Cat API (http://thecatapi.com).

Big Thanks to the great Cat API

Python 2 and 3 compatible

Install through pip.

$ git clone https://github.com/gravmatt/random-cat $ cd random-cat $ python setup.py install 

The cat module has just one function getCat() with three optional arguments.

directory - default is the current directory

filename - default is a unique id

format - default is png. optional png, jpg and gif is available

import cat # cat.getCat(directory=None, filename=None, format='png') cat.getCat(directory='/users/tor', filename='cat', format='gif') # /users/tor/cat.gif 

The function return the image name (absolute path if directory is specified) of the image.

You can also request an image on the terminal.

$ randomcat [format] [file] # Example: $ randomcat gif /users/tor/cat.gif 

The two arguments format and file are optional.

You can select the formats png, jpg or gif.

The command return the filename/absolute path of the image to the standard output (stdout).

Источник

Geospatial Solutions Expert

The cat API is a public service API all about Cats. This means it is a service where people who like cats share picture and other details about cats and developers uses it in there applications.

The python code below uses the free cat api to search for random cats, fetch there details and organize it into a dataframe table and the download the pictures onto the local disc.

import json import shutil import requests url = 'https://api.thecatapi.com/v1/images/search' data_list = [] for i in range(101): print('Getting random cat image. ', i) # Send requests. response = requests.get(url) # Get values from response. data = list(response.json()[0].values()) # Append data to list. data_list.append(data) # Get columns names from response. cols = list(response.json()[0].keys()) # Create df. data_list_df = pd.DataFrame(data_list, columns=cols) # =========================================== # Download images. i = 1 for url in data_list_df['url']: # Get image extension. img_ext = url.split('.')[-1] imgfile_name = f'cat_.' print('Processsing. ', imgfile_name) # Send requests. res = requests.get(url, stream=True) # Write image to disc. with open(f'cat_image/','wb') as f: shutil.copyfileobj(res.raw, f) # Alternative: write image to disc. # with open(f'cat_image/','wb') as f: # f.write(res.content) i = i+1 # break

Similarly, the code above can be adapted for the Dog API or Dog CEO API.

Источник

Fun with Cats – Python API Calls

python api calls cat

python api calls cat

One of the great things about Python as a programming language is that you can do fairly powerful things with just a few lines of code. In this article we are going to look at how to do a simple API call in Python to retrieve a random cat and display it on the screen.

API calls are everywhere on the internet. Although you will not need to know a huge amount about them for school-level Computer Science, it is important to have a basic knowledge of them. Besides they can be a lot of fun.

Retrieve a Cat

Here’s an example, which retrieves a random cat from an API called CATAAS (Cat as a service)

import urllib.request import turtle screen = turtle.Screen() url = 'https://cataas.com/cat/gif' filename = "random-cat.gif" urllib.request.urlretrieve(url, filename) screen.bgpic('random-cat.gif') turtle.done() 

Adding Sound

If you spend much time using Python, you will eventually come across the need to extend its power using packages. Leaning how to do this can take some effort, and can be more or less easy depending on your setup. If you have a recent version of Python (3.4 upwards), then you can use pip install playsound from a command prompt window. Otherwise, if you ae using something like WinPython you can install packages using a GUI. If this sounds like voodoo, maybe leave the next part of this activity for now.

The package we will use to add sound is playsound. Once you have installed it, download meow.mp3 into the same folder as your program, and modify you code to look like this:

import urllib.request import turtle import playsound screen = turtle.Screen() url = 'https://cataas.com/cat/gif' filename = "random-cat.gif" urllib.request.urlretrieve(url, filename) screen.bgpic('random-cat.gif') playsound.playsound('meow.mp3', True) turtle.done() 

Your cat should now meow for you.

In future articles, we will look at more API calls. Hopefully for now this has provided you with a fun introduction.

Источник

Читайте также:  Javascript array число элементов
Оцените статью