-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
42 lines (35 loc) · 1.28 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import requests
import config as c
NINJAS_KEY = c.NINJAS_KEY
# get the quotes from ninjas api
def morning():
category = "morning"
api_url = "https://api.api-ninjas.com/v1/quotes?category={}&limit=1".format(
category
)
response = requests.get(api_url, headers={"X-Api-Key": NINJAS_KEY})
if response.status_code == requests.codes.ok:
quotes = response.json()
quote = quotes[0]["quote"]
author = quotes[0]["author"]
return quote, author
else:
return ("Error:", response.status_code, response.text)
def dad_joke():
api_url = "https://api.api-ninjas.com/v1/dadjokes?limit={}".format(1)
response = requests.get(api_url, headers={"X-Api-Key": NINJAS_KEY})
if response.status_code == requests.codes.ok:
dad_jokes = response.json()
joke = dad_jokes[0]["joke"]
return joke
else:
return ("Error:", response.status_code, response.text)
def fact():
api_url = "https://api.api-ninjas.com/v1/facts?limit={}".format(1)
response = requests.get(api_url, headers={"X-Api-Key": NINJAS_KEY})
if response.status_code == requests.codes.ok:
facts = response.json()
fact = facts[0]["fact"]
return fact
else:
return ("Error:", response.status_code, response.text)