This page grabs a dad joke from https://icanhazdadjoke.com
no need for me to reinvent the wheel, adapted it from this example
It required a little modification, I try to avoid adding dependancies so I made it a module and added exception handling
#!/usr/bin/env python3
"""get a random dad joke"""
import requests
class Joke:
def __init__(self, id, contents):
self.id = id
self.contents = contents
def __str__(self):
return self.contents.encode('utf-8')
def dailyjoke():
try:
r = requests.get('https://icanhazdadjoke.com', headers={"Accept":"application/json"})
raw_joke = r.json();
joke = Joke(raw_joke['id'], raw_joke['joke'])
return joke.contents
except Exception as e:
print ('Failed: ', e)
return e