Created
October 18, 2016 02:53
-
-
Save tyrostone/a2d52ade580c90e235da9f3ab2622b7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" 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