Skip to content
Go to file
Cannot retrieve contributors at this time
executable file 53 lines (39 sloc) 1.34 KB
#!/usr/bin/env python3
import requests # pip install requests
import json
import time
class Chuck(object):
"""Use the to get a random joke"""
def __init__(self):
self._exclude = 'explicit'
self._use_tls = False
self._debug = False
def _get_url(self, path):
"""Return the URL for the icndb"""
if self._use_tls:
scheme = 'https'
scheme = 'http'
return '{0}://{1}/{2}'.format(scheme, Chuck.API_DOMAIN, path)
def joke(self):
query= {'exclude': self._exclude}
response = requests.get(self._get_url('jokes/random'), params= query)
# Be respectful to the server - 1 per second is good
if self._debug:
print("DEBUG URL: ", response.url)
print("DEBUG DATA: ", response.text)
data = json.loads(response.text)
if data['type'] != 'success':
raise ValueError('Error when getting joke: ' + str(data))
if self._debug:
print("JOKE ID:", data['value']['id'])
return data['value']['joke']
if __name__ == "__main__":
chuck = Chuck()
#chuck._debug = True
for i in range(3):
#print("Loop i = " + str(i))
You can’t perform that action at this time.