import requests
base_URL = "https://swapi.co/api/"

def getJSONfromAPI(category, searchTerm):
  response = requests.get(base_URL + category + "/?search=" + searchTerm)
  response.content.decode("utf-8")
  data = response.json()['results']
  print('Number of results found: ' + str(len(data)))
  return data
  
def get_person(person):
  data = getJSONfromAPI('people',person)
  for person in data:
    print(person['name'] + ' is a ' + person['gender'] + ' character, born on ' + person['birth_year'])

def get_spaceship(spaceship):
  data = getJSONfromAPI('starships',spaceship)
  for spaceship in data:
    print(spaceship['name'] + ' holds up to ' + spaceship['passengers'] + ' passengers. It has a cargo capacity of ' + spaceship['cargo_capacity'] + ' and costs ' + spaceship['cost_in_credits'] + ' credits. '+spaceship['name']  + ' has a hyperdrive rating of ' + spaceship['hyperdrive_rating'])
  
def get_planet(planet):
  data = getJSONfromAPI('planets',planet)
  for planet in data:
      print(planet['name'] + '\'s climate is ' + planet['climate'] + ' and has a population of ' + planet['population'] + '. It\'s mostly made of: ' + planet['terrain'] + '. ' + planet['name'] + ' has been featured in ' + str(len(planet['films'])) + ' films.')
      
def get_film(film):
  data = getJSONfromAPI('films',film)
  for film in data:
      print(film['title'] + ' has been directed by ' + film['director'] + '. It is the episode number ' + str(film['episode_id']) + '. The opening crawl is as follows:\n' + film['opening_crawl'])
  
def get_species(species):
  data = getJSONfromAPI('species',species)
  for species in data:
      print(species['name'] + 's are classified as ' + species['classification'] + 's. They are ' + species['designation'] + ' with an average height of '+species['average_height'] + ' and average lifespan of ' + species['average_lifespan'] + ' years. They speak in ' + species['language'])
  
def get_vehicle(vehicle):
  data = getJSONfromAPI('vehicles',vehicle)
  for vehicle in data:
      print(vehicle['name'] + ' holds up to ' + vehicle['passengers'] + ' passengers. It has a cargo capacity of ' + vehicle['cargo_capacity'] + ' and costs ' + vehicle['cost_in_credits'] + ' credits. '+vehicle['name']  + ' is classified as ' + vehicle['vehicle_class'] + ' vehicles.')

end = False
while (not end):
  category = input("Welcome to Star Wars information center. We are using an API hosted at swapi.co to perform searches for you. \n What would you like to search for?\n1: Person\n2: Spaceship\n3: Planet\n4: Film\n5: Species\n6: Vehicle\nPlease enter a number: ")
  
  if category == "1":
    person = input("Which person?:")
    get_person(person)
  
  elif category == "2":
      spaceship = input("Which spaceship?:")
      get_spaceship(spaceship)
  
  elif category == "3":
      planet = input("Which planet?:")
      get_planet(planet)
  
  elif category == "4":
      film = input("Which film?:")
      get_film(film)
  
  elif category == "5":
      species = input("Which species?:")
      get_species(species)
  
  elif category == "6":
      vehicle = input("What vehicle?:")
      get_vehicle(vehicle)
  else:
    print("Please enter a number from 1 to 6")
