Skip to content

Instantly share code, notes, and snippets.

@tyrostone
Created October 18, 2016 02:53
Show Gist options
  • Save tyrostone/a2d52ade580c90e235da9f3ab2622b7c to your computer and use it in GitHub Desktop.
Save tyrostone/a2d52ade580c90e235da9f3ab2622b7c to your computer and use it in GitHub Desktop.
""" catfact.py
Returns a random cat fact.
Facts obtained from https://catfacts-api.appspot.com/
"""
import json
import sys
try:
import requests
except ImportError:
print "The requests library is not installed - \
please install it using\n 'sudo pip install requests'"
sys.exit(1)
FACTS_API = "http://catfacts-api.appspot.com/api/facts"
def get_cat_fact():
try:
return json.loads(requests.get(FACTS_API).text)['facts'][0]
except requests.exceptions.ConnectionError:
return "System is offline - try again later for cat facts!"
if __name__ == "__main__":
print(get_cat_fact())
print("************************************************")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment