def get_ship_data(epiosde): # get the film from the episode number film = swapi.get_film(epiosde) # get the starships in the film starships = film.get_starships() film_data = {} starships_data = {} # get all the pilots for each starship for ship in starships.iter(): pilots = ship.get_pilots() pilots_data = [] # get the pilot names for pilot in pilots.iter(): pilots_data.append(pilot.name) # set up the starship data with a list of pilots for each starship starships_data[ship.name] = {"pilots": pilots_data} # add the starship data to the film data film_data = {film.title: {"starships": starships_data}} # format as json return json.dumps(film_data)
def species_count_by_film(num): #if num >= 1 and num <= 7: film_num = swapi.get_film(num) species = film_num.get_species() list_species_items = species.items dedup_list_species_items = dedup_list(list_species_items) #print("不同的species有幾個:", len(li_2)) return len(dedup_list_species_items)
def get(attribute, value): if attribute == "films": return swapi.get_film(value).title if attribute == "species": return swapi.get_species(value).name if attribute == "starships": return swapi.get_starship(value).name if attribute == "vehicles": return swapi.get_vehicle(value).name
def test_repr_(self): starship = swapi.get_starship(3) self.assertEquals('<Starship - Star Destroyer>', starship.__repr__()) vehicle = swapi.get_vehicle(4) self.assertEquals('<Vehicle - Sand Crawler>', vehicle.__repr__()) film = swapi.get_film(1) self.assertEquals('<Film - A New Hope>', film.__repr__()) planet = swapi.get_planet(1) self.assertEquals('<Planet - Tatooine>', planet.__repr__()) species = swapi.get_species(1) self.assertEquals('<Species - Human>', species.__repr__())
def gen_dict_from_movie(id): movie = swapi.get_film(id) dict = { 'title': movie.title, 'director': movie.director, 'producer': movie.producer, 'release': movie.release_date, 'crawl': movie.opening_crawl } return dict
def gen_dict_info_from_movie(id): movie = swapi.get_film(id) characters = [] planets = [] species = [] spaceships = [] vehicles = [] """ get all characters from a movie and store them """ for person in movie.characters: person_id = person.split('/')[5] characters.append((swapi.get_person(person_id)).name) #print (characters) #print(len(characters)) """ get all planets from a movie and store them """ for planet in movie.planets: planet_id = planet.split('/')[5] planets.append((swapi.get_planet(planet_id)).name) #print (planets) """ get all species from a movie and store them """ for specie in movie.species: specie_id = specie.split('/')[5] species.append((swapi.get_species(specie_id)).name) """ get all starships from a movie and store them """ for spaceship in movie.starships: ship_id = spaceship.split('/')[5] spaceships.append((swapi.get_starship(ship_id)).name) """ get all vehicles from a movie and store them """ for vehicle in movie.vehicles: vehicle_id = vehicle.split('/')[5] vehicles.append((swapi.get_vehicle(vehicle_id)).name) dict = { 'title': movie.title, 'characters': characters, 'planets': planets, 'species': species, 'spaceships': spaceships, 'vehicles': vehicles } return dict
def swapi_to_action(text): try: if get_match(text, 'swapi')[0]: index = get_match(text, 'swapi')[1] if index == 0: i = swapi.get_planet(randint(1, 61)) print i if index == 1: i = swapi.get_film(randint(1, 7)) print i if index == 2: i = swapi.get_vehicle(randint(1, 39)) print i if index == 3: i = swapi.get_starship(randint(1, 37)) print i if index == 4: i = wapi.get_specie(randint(1, 37)) print i if index == 5: i = swapi.get_person(randint(1, 87)) print i except: pass
def test_film_get_characters(self): film = swapi.get_film(1) self.assertEquals(type(film.get_characters()), PeopleQuerySet)
def film(): swapi.get_film(randint(1, 7))
def test_film_get_species(self): film = swapi.get_film(1) self.assertEquals(type(film.get_species()), SpeciesQuerySet) self.assertIn('<SpeciesQuerySet - ', film.get_species().__repr__())
def test_film_gen_crawl(self): film = swapi.get_film(1) for line in film.gen_opening_crawl(): self.assertIn(line, film.opening_crawl)
def test_film_get_vehicles(self): film = swapi.get_film(1) self.assertEquals(type(film.get_vehicles()), VehicleQuerySet)
def test_film_get_planets(self): film = swapi.get_film(1) self.assertEquals(type(film.get_planets()), PlanetQuerySet) self.assertIn('<PlanetQuerySet - ', film.get_planets().__repr__())
import json import pprint import swapi """ result = requests.get("https://swapi.dev/api/people/3/") data = json.loads(result.text) data["name"] = "Vadim" pprint.pprint(data) print("*" * 40) homeworld = requests.get(data["homeworld"]) home_data = json.loads(homeworld.text) pprint.pprint(home_data) with open("starwar.json", "w") as file: json.dump(data, file, indent=4) """ result = requests.get("https://swapi.dev/api/starships/9/") data = json.loads(result.text) pprint.pprint(data) print("*" * 40) result = requests.get("https://swapi.dev/api/planets/2/") data = json.loads(result.text) pprint.pprint(data) film = swapi.get_film(1) #people = swapi.get_all('people') #luke = swapi.get_person(1) #tatooine = swapi.get_planet(1) #print(luke)
import xkcd import tweepy import swapi from secrets import * from random import randint auth = tweepy.OAuthHandler(C_KEY, C_SECRET) auth.set_access_token(A_TOKEN, A_TOKEN_SECRET) api = tweepy.API(auth) opc = randint(1, 6) if (opc == 1): #films try: film = swapi.get_film(randint(1, 7)) except: film = swapi.get_film(1) tweet = "#StarWars\nFilm\nTitle: " + film.title + "\nDirector: " + film.director + "\nRelease Date: " + film.release_date + "\nOpening Crawl:" if (len(tweet) < 276): ocL = len(film.opening_crawl) tL = len(tweet) tot = (ocL if ocL + tL <= 276 else 276 - tL) tweet += film.opening_crawl[:tot] + "..." elif (opc == 2): #people try: person = swapi.get_person(randint(1, 87)) except: person = swapi.get_person(1) home = person.get_homeworld()
def test_film_get_starships(self): film = swapi.get_film(1) self.assertEquals(type(film.get_starships()), StarshipQuerySet) self.assertIn('<StarshipQuerySet - ', film.get_starships().__repr__())
import swapi pm = swapi.get_film(4) jj = swapi.get_person(36) for c in pm.get_characters().iter(): if c.name == jj.name: print("Why George, why.")
import swapi empire = swapi.get_film(2) empirePlanets = empire.get_planets() for planet in empirePlanets.order_by("population"): print(planet.name, planet.population)