Python RapidAPI
APIs can be found all over the internet. A great consolidator of many APIs is RapidAPI. In this blog we will use a site to consolidates API stats. Learning a few lines of code and you can start extracting lots of data from the internet via APIs.
"""
Requests is a HTTP library for the Python programming language.
The goal of the project is to make HTTP requests simpler and more human-friendly.
"""
import requests
url = "https://corona-virus-world-and-india-data.p.rapidapi.com/api"
headers = {
"X-RapidAPI-Key": "e819277188msh68a6f7af77fad4dp172d06jsnc2bc1f8ab152",
"X-RapidAPI-Host": "corona-virus-world-and-india-data.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers)
# print(response.text)
# This code looks for "world data"
print("World Totals")
world = response.json().get('world_total') # turn response to json() so we can extract "world_total"
for key, value in world.items(): # this finds key, value pairs in country
print(key, value)
print()
# This code looks for USA in "countries_stats"
print("Country Totals")
countries = response.json().get('countries_stat')
for country in countries: # countries is a list
if country["country_name"] == "USA": # this filters for USA
for key, value in country.items(): # this finds key, value pairs in country
print(key, value)
# RapidAPI page https://rapidapi.com/Coinranking/api/coinranking1/
# Begin Rapid API Code
import requests
url = "https://coinranking1.p.rapidapi.com/coins"
querystring = {"referenceCurrencyUuid":"yhjMzLPhuIDl","timePeriod":"24h","tiers[0]":"1","orderBy":"marketCap","orderDirection":"desc","limit":"50","offset":"0"}
headers = {
"X-RapidAPI-Key": "jcmbea0fa2ff5msh7f14bf69be38ca6p175482jsn6c4988114560", # place your key here
"X-RapidAPI-Host": "coinranking1.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers, params=querystring)
print(response.text)
# End Rapid API Code
json = response.json() # convert response to python json object
# Observe data from an API. This is how data transports over the internet in a "JSON" text form
# - The JSON "text" is formed in dictionary {} and list [] divisions
# - To read the result, Data Scientist of Developer converts JSON into human readable form
# - Review the first line, look for the keys -- "status" and "data"
"""
This cell is dependent on valid run of API above.
- try and except code is making sure "json" was properly run above
- inside second try is code that is used to process Coin API data
Note. Run this cell repeatedly to format data without re-activating API
"""
try:
print("JSON data is Python type: " + str(type(json)))
try:
# Extracting Coins JSON status, if the API worked
status = json.get('status')
print("API status: " + status)
print()
# Extracting Coins JSON data, data about the coins
data = json.get('data')
# Procedural abstraction of Print code for coins
def print_coin(c):
print(c["symbol"], c["price"])
print("Icon Url: " + c["iconUrl"])
print("Rank Url: " + c["coinrankingUrl"])
# Coins data was observed to be a list
for coin in data['coins']:
print_coin(coin)
print()
except:
print("Did you insert a valid key in X-RapidAPI-Key of API cell above?")
print(json)
except:
print("This cell is dependent on running API call in cell above!")
import requests
import json
url = "https://shazam.p.rapidapi.com/search"
querystring = {"term":"kiss the rain","locale":"en-US","offset":"0","limit":"5"}
headers = {
"X-RapidAPI-Key": "e819277188msh68a6f7af77fad4dp172d06jsnc2bc1f8ab152",
"X-RapidAPI-Host": "shazam.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers, params=querystring)
# print(response.text)
print("Track_Name")
tracks = response.json().get('tracks')
hits = tracks.get('hits')
for track in hits:
if track ["track"]['key'] == '40099833':
json_formatted_str = json.dumps(track ["track"], indent=4)
print (json_formatted_str)