Exploring the Star Wars API. This is an open and free-to-use API that provides all information you could possibly need about Star Wars.
You can read about the project on the page
and access the technical documentation for the API on the page
https://swapi.dev/documentation
Using an API requires that you get the relevant information from its documentation. This API has great documentation, so be sure to check it carefully. The documentation contains all the details you need to answer the questions below.
You can access information about 10 planets in Star Wars by sending a get request (without any parameters) to
1.1) A for loop to print out the names of all 10 planets
# Answer to Example 1.1
import requests, json
url = "http://swapi.dev/api/planets/"
response = requests.get(url)
data = json.loads(response.text)
for i in range(10):
print(data['results'][i]['name'])
1.2) A function called get_planet_population that takes as an argument a string called 'planet_name'.
# Answer to Example 1.2
def get_planet_population(planet_name):
import requests, json
url = "http://swapi.dev/api/planets/"
response = requests.get(url)
data = json.loads(response.text)
for i in range(10):
if planet_name == data['results'][i]['name']:
if data['results'][i]['population'] == 'unknown':
return None
return data['results'][i]['population']
return "unknown planet"
#Tests for Example 1.2
print(get_planet_population("Tatooine")) ## vanilla exmaple planet
print(get_planet_population("Hoth")) ## planet with unknown population
print(get_planet_population("XYZ")) ## planet that doesn't exit
1.3) Print the names of all planets, from among the 10 planets returned by a call to http://swapi.dev/api/planets/, that have a population less than or equal to 30000000 and whose climate description includes the word 'temperate'.
# Answer to Example 1.3
import requests, json
url = "http://swapi.dev/api/planets/"
response = requests.get(url)
data = json.loads(response.text)
for i in range(10):
if data['results'][i]['population'] != 'unknown' and int(data['results'][i]['population']) <= 30000000 and \
'temperate' in data['results'][i]['climate']:
print(data['results'][i]['name'])
In this exmaple, I will use a while loop to issue requests for information about all starships in Star Wars. The API to use is located at
http://swapi.dev/api/starships/
Note that the data you get back is a dictionary that contains a key called 'next'. The value for that key is the URL to which you should send the next request using requests.get() to fetch the additional batch of information about the following 10 starships.
2.1) Retrieve information about all starships available via this API and store it in a list called 'starships'.
The typical way to fetch all results from an API is to use a while loop that will retrieve a batch of 10 results, add them to a list (or similar data structure) and then send another request for more results if the value for the key 'next' in the dictionary in the previous response contained a URL. When you retrieve the final batch of results and no more results are available, the server will send you a dictionary that will probably still contain results you need to add to the list but the value for key 'next' will be None (rather than a URL). Therefore, one common strategy is to have your while loop end when the value for key 'next' == None. Notice that None is a special value in Python (like True or False) and is not surrounded in quotes!
# Answer to Example 2.1
starships = []
import requests, json
url = "http://swapi.dev/api/starships/"
number = 1
parameters = {'page': number}
response = requests.get(url, parameters)
data = json.loads(response.text)
while (data['next']!= None):
response = requests.get(url, parameters)
data = json.loads(response.text)
length = len(data['results'])
for i in range(length):
starships.append(data['results'][i]['name'])
number += 1
parameters = {'page': number}
#Test Example 2.1
print(starships)
2.2) Print out the name of the fastest starship Star Wars. As indicated in the API documentation, speed is given by the MGLT (Maximum number of Megalights) attribute of a starship.
# Answer to Example 2.2
fastest_starship = ''
max_speed = 0
import requests, json
url = "http://swapi.dev/api/starships/"
number = 1
parameters = {'page': number}
response = requests.get(url, parameters)
data = json.loads(response.text)
while (data['next']!= None):
response = requests.get(url, parameters)
data = json.loads(response.text)
length = len(data['results'])
for i in range(length):
if data['results'][i]['MGLT'] != 'unknown' and int(data['results'][i]['MGLT']) > max_speed:
max_speed = int(data['results'][i]['MGLT'])
fastest_starship = data['results'][i]['name']
number += 1
parameters = {'page': number}
# Test
print("The fastest starship is %s and its MGLT(speed) is %d" %(fastest_starship, max_speed))
# print(fastest_starship)
# print(max_speed)