Fetching random cat pictures using openAPI
Used API
- https://gql.thatcopy.pw/
- REST API for retrieving .jpg cat images.
Programming language and skills used
- Python
- Multiprocessing - concurrent.futures
Code
We will use the url which will send us randomly chosen cat pictures. As
retrieving images is a IO bound task, I'm going to use multiprocessing (you
can use multi-threading because this is not a CPU bound task).
Steps to take
- Retrieve json using the API
- Json will be in a byte format, so we will decode it and load it to json object
- From the json object, extract the image source uri.
- Open a new file (I'll name the file randomly) and write the image to the file.
I'll initiate download_img function with multiprocessing.
This is the complete source code
Comments
Post a Comment