a random gif web-service
#!/usr/bin/python3 | |
import platform | |
import giphy_client | |
from giphy_client.rest import ApiException | |
from http.server import HTTPServer, BaseHTTPRequestHandler | |
api_key = "REPLACE_WITH_YOUR_GIPHY_API_KEY" # str | Giphy API Key. | |
# create an instance of the API class | |
api_instance = giphy_client.DefaultApi() | |
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): | |
def do_GET(self): | |
content = """<head> | |
<style> | |
div {text-align: center;} | |
</style> | |
</head> | |
""" | |
content += "\nServer: " + platform.node() + "\n" | |
rating = "g" | |
fmt = "json" | |
tag = "bunny" | |
try: | |
api_response = api_instance.gifs_random_get( | |
api_key, tag=tag, rating=rating, fmt=fmt | |
) | |
gif_id = api_response.data | |
content += "<h1>Welcome to the Mysocket Gif generator</h1>" | |
content += f"<img src='{gif_id.image_original_url}'>" | |
content += "<br><p>For more details see: <a href='https://www.mysocket.io/post/easy-multi-region-load-balancing-with-mysocket-as-a-load-balancer'>Easy Multi-region load balancing with Mysocket.io as a global Load Balancer</a></p>" | |
except ApiException as e: | |
content += "Exception when calling Giphy API :( " | |
content_center = f"<div> {content} </div>" | |
self.send_response(200) | |
self.send_header("Content-type", "text/html") | |
self.end_headers() | |
self.wfile.write(content_center.encode()) | |
httpd = HTTPServer(("0.0.0.0", 8000), SimpleHTTPRequestHandler) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment