Here I use the code from RapidAPI to import the data from the Inspirational quote database. The RapidAPI key is specific for me and allows for me to access the API with spam protection. The RapidAPI host directs me the proper API that I want data from!
import requests
url = "https://pquotes.p.rapidapi.com/api/quote"
headers = {
"content-type": "application/json",
"X-RapidAPI-Key": "f8c1edc71amsh2ceb94e75170cf3p1172ddjsn28d9990da6a6",
"X-RapidAPI-Host": "pquotes.p.rapidapi.com"
}
After Setting the parameters for the API, I now used the .request function
response = requests.request("POST", url, json={"topic": "motivation"}, headers=headers).json() #This uses the .request function that sends a post request to the API to retrieve data
quote = response["quote"] #Sets the variable quote to the quote inside of the API
author = response["by"] #sets author to the author inside of the API
print("Your random inspirational quote is, \"" +quote +"\"" "\n", "--", author) # Outputs the quote using print()
Now with this API, we can retrieve random inspiration quotes. This is very beneficial for our website because now we have access to 1000s of quotes and we can use this to provide inspiration to users. Our app is based on inspiration and these quotes would help push people to reach for their goals!
import random #importing packages
import math
from flask import Flask, request
app = Flask(__name__) # Creating the flask application
# Creating the array of quotes for the API to parse through
quotes = ["The sky is the limit - Kalani", "The celling is the roof - Micheal Jordan", "You miss 100%\ of the shots you don't take -Wayne Gretsky", "hehe - Navan", "JSON! - Mort"]
# Creates a function that uses random to generate a random number whithin the range of the quotes array
def randomgen():
number = int(random.random()*len(quotes))
return number
# Creating the GET endpoint, so when we call it using /get, it will call to the randomgen function and return the quote from the array
@app.route('/get', methods=['GET'])
def get():
return {"connection": "succesful", "Your quote is" : quotes[randomgen()]}