Skip to content

Instantly share code, notes, and snippets.

@mcdwayne
Created February 23, 2023 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcdwayne/f3ae62ab01bbdf8f933246e23a62d530 to your computer and use it in GitHub Desktop.
Save mcdwayne/f3ae62ab01bbdf8f933246e23a62d530 to your computer and use it in GitHub Desktop.
# Code written by ChatGPT using input:
"""
write a python script that takes in json from the API at https://random.dog/woof.json, finds the picture url from the json, retrieves image from url and displays it as a http server locally
"""
# Everything below this line was the response
"""
Sure, here's a Python script that retrieves a random dog image from the JSON API provided by https://random.dog/woof.json, downloads the image to your local machine, and serves it locally on an HTTP server using the Flask web framework.
Before running the script, you will need to install the requests and Flask modules using pip.
"""
##
import requests
from flask import Flask, send_file
app = Flask(__name__)
@app.route('/')
def get_random_dog_image():
# Make a request to the API and get the JSON response
response = requests.get('https://random.dog/woof.json')
json_data = response.json()
# Get the URL of the random dog image from the JSON response
image_url = json_data['url']
# Download the image to a local file
image_file = requests.get(image_url)
with open('random_dog.jpg', 'wb') as f:
f.write(image_file.content)
# Serve the image on a local HTTP server
return send_file('random_dog.jpg', mimetype='image/jpeg')
if __name__ == '__main__':
app.run()
"""
To run the script, save it in a file named random_dog.py, and then run python random_dog.py. The script will start a local HTTP server at http://localhost:5000/. When you visit this URL in your web browser, you should see a random dog image displayed.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment