Skip to content

Instantly share code, notes, and snippets.

@managedkaos
Last active May 30, 2023 21:27
Show Gist options
  • Save managedkaos/029ca73c8dfe1c195b2c23d1fb4dfb7f to your computer and use it in GitHub Desktop.
Save managedkaos/029ca73c8dfe1c195b2c23d1fb4dfb7f to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyPLCAv0eLZw13t5rcgZRp3j",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "oH6DxYvaJoIo",
"outputId": "54641b60-e53a-4a72-c7b2-64978dc4d134"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
"Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (2.27.1)\n",
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests) (1.26.15)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests) (2022.12.7)\n",
"Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests) (2.0.12)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests) (3.4)\n"
]
}
],
"source": [
"pip install requests"
]
},
{
"cell_type": "code",
"source": [
"import threading\n",
"import requests\n",
"from queue import Queue\n"
],
"metadata": {
"id": "OO0pDcZ5Juj0"
},
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"source": [
"breeds = [\n",
" \"appenzeller\",\n",
" \"boxer\",\n",
" \"briard\",\n",
" \"cockapoo\",\n",
" \"keeshond\",\n",
" \"malinois\",\n",
" \"maltese\",\n",
" \"mix\",\n",
" \"ovcharka\",\n",
" \"terrier\",\n",
" \"FAKE_DOG_1\",\n",
" \"FAKE_DOG_2\",\n",
" \"FAKE_DOG_3\",\n",
" \"FAKE_DOG_4\",\n",
" \"FAKE_DOG_5\",\n",
"]\n"
],
"metadata": {
"id": "DKVP_3VQJ1nu"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def get_dog_info(breed, results):\n",
" message, status, http_code = None, None, None\n",
"\n",
" try:\n",
" url = f\"https://dog.ceo/api/breed/{breed}/images/random\"\n",
" response = requests.get(url)\n",
" http_code = response.status_code\n",
"\n",
" if response.ok:\n",
" data = response.json()\n",
" message = data[\"message\"]\n",
" status = data[\"status\"]\n",
"\n",
" except Exception as e:\n",
" f_message = f\"An error occurred while fetching data for breed '{breed}': {str(e)}\"\n",
" print(f_message)\n",
" message = f_message\n",
"\n",
" results.put((breed, message, status, http_code))\n"
],
"metadata": {
"id": "rB6IFhmvJ4P4"
},
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Create a queue to store the results\n",
"results_queue = Queue()\n",
"\n",
"# Create a list to hold the thread objects\n",
"threads = []\n"
],
"metadata": {
"id": "H8rVskp-J6wQ"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Create and start a thread for each breed\n",
"for breed in breeds:\n",
" thread = threading.Thread(target=get_dog_info, args=(breed, results_queue))\n",
" thread.start()\n",
" threads.append(thread)\n"
],
"metadata": {
"id": "5XDjAZUHJ8wa"
},
"execution_count": 6,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Wait for all threads to complete\n",
"for thread in threads:\n",
" thread.join()\n"
],
"metadata": {
"id": "NTCxoR7HJ_AP"
},
"execution_count": 7,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Process the results from the queue\n",
"results = []\n",
"while not results_queue.empty():\n",
" results.append(results_queue.get())\n"
],
"metadata": {
"id": "xwR8uzWAKAsT"
},
"execution_count": 8,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Print the results\n",
"for breed, message, status, http_code in results:\n",
" print(f\"Breed: {breed}\")\n",
" print(f\"Message: {message}\")\n",
" print(f\"Status: {status}\")\n",
" print(f\"HTTP Status Code: {http_code}\")\n",
" print(\"------------------------\")\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "3zyyQxfoKCPS",
"outputId": "4cd2cb6f-90ed-4368-97d1-b672f290c316"
},
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Breed: FAKE_DOG_2\n",
"Message: None\n",
"Status: None\n",
"HTTP Status Code: 404\n",
"------------------------\n",
"Breed: terrier\n",
"Message: https://images.dog.ceo/breeds/terrier-bedlington/n02093647_3356.jpg\n",
"Status: success\n",
"HTTP Status Code: 200\n",
"------------------------\n",
"Breed: FAKE_DOG_1\n",
"Message: None\n",
"Status: None\n",
"HTTP Status Code: 404\n",
"------------------------\n",
"Breed: appenzeller\n",
"Message: https://images.dog.ceo/breeds/appenzeller/n02107908_5254.jpg\n",
"Status: success\n",
"HTTP Status Code: 200\n",
"------------------------\n",
"Breed: FAKE_DOG_4\n",
"Message: None\n",
"Status: None\n",
"HTTP Status Code: 404\n",
"------------------------\n",
"Breed: FAKE_DOG_3\n",
"Message: None\n",
"Status: None\n",
"HTTP Status Code: 404\n",
"------------------------\n",
"Breed: malinois\n",
"Message: https://images.dog.ceo/breeds/malinois/n02105162_1357.jpg\n",
"Status: success\n",
"HTTP Status Code: 200\n",
"------------------------\n",
"Breed: mix\n",
"Message: https://images.dog.ceo/breeds/mix/Annabelle0.jpg\n",
"Status: success\n",
"HTTP Status Code: 200\n",
"------------------------\n",
"Breed: cockapoo\n",
"Message: https://images.dog.ceo/breeds/cockapoo/Guri7.jpg\n",
"Status: success\n",
"HTTP Status Code: 200\n",
"------------------------\n",
"Breed: keeshond\n",
"Message: https://images.dog.ceo/breeds/keeshond/n02112350_9232.jpg\n",
"Status: success\n",
"HTTP Status Code: 200\n",
"------------------------\n",
"Breed: FAKE_DOG_5\n",
"Message: None\n",
"Status: None\n",
"HTTP Status Code: 404\n",
"------------------------\n",
"Breed: maltese\n",
"Message: https://images.dog.ceo/breeds/maltese/n02085936_5582.jpg\n",
"Status: success\n",
"HTTP Status Code: 200\n",
"------------------------\n",
"Breed: boxer\n",
"Message: https://images.dog.ceo/breeds/boxer/n02108089_13340.jpg\n",
"Status: success\n",
"HTTP Status Code: 200\n",
"------------------------\n",
"Breed: briard\n",
"Message: https://images.dog.ceo/breeds/briard/n02105251_2406.jpg\n",
"Status: success\n",
"HTTP Status Code: 200\n",
"------------------------\n",
"Breed: ovcharka\n",
"Message: https://images.dog.ceo/breeds/ovcharka-caucasian/IMG_20190801_112134.jpg\n",
"Status: success\n",
"HTTP Status Code: 200\n",
"------------------------\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "ud8T-9hUKD93"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment