from urllib import request import json def get_joke(): url = "https://icanhazdadjoke.com" try: with request.urlopen(create_request(url)) as response: joke = json.loads(response.read()) if joke["status"] == 200: return joke["joke"] except (request.URLError, request.HTTPError): return "No dad joke right now." def create_request(url): headers = { "Accept": "application/json", "User-Agent": "ameliabot (https://criminallycute.fi/ameliabot)" } return request.Request( url, data=None, headers=headers ) def run(): return get_joke()