def test_year_api_2(self): y = Year(1997, 2000, "FPS", 9, 100) y2 = Year(1990, 200, "Platformer", 10, 50) db.session.add(y) db.session.add(y2) db.session.commit() self.assertTrue(y2 in db.session) db.session.remove()
def test_year_api_3(self): y = Year(2007, 5000, "FPS", 9.6, 1000) y2 = Year(2008, 2000, "FPS", 9.5, 500) y3 = Year(1999, 1000, "Fighting", 8.0, 400) db.session.add(y) db.session.add(y2) db.session.add(y3) q = Year.query.all() self.assertEqual(q[0].year_id, y3.year_id) db.session.remove()
def test_games_3(self): y = Year(2008) db.session.add(y) g = Game(1,"Fallout 3", rating = 87, release_year = 2008) y2 = Year(2004) db.session.add(y2) g2 = Game(2,"TES IV: Oblivion", rating = 100, release_year = 2004) db.session.add(g) db.session.add(g2) db.session.commit() q = Game.query.all() self.assertEqual(len(q), 2) db.session.remove()
def test_year_api_1(self): y = Year(1997, 2000, "FPS", 9, 100) db.session.add(y) db.session.commit() q = Year.query.all() self.assertEqual(y, q[0]) db.session.remove()
def test_games_2(self): y = Year(2008) db.session.add(y) g = Game(1, "Fallout 3", rating = 87, release_year = 2008) db.session.add(g) db.session.commit() self.assertTrue(g in db.session) # https://pythonhosted.org/Flask-Testing/ db.session.remove()
def test_company_2(self): y = Year(1986) db.session.add(y) c = Company(1, "Bethesda Softworks", 11, None, 66, 8.9, 1986) y2 = Year(1994) db.session.add(y2) c2 = Company(2, "Blizzard Entertainment", 20, None, 40, 9, 1994) y3 = Year(1980) db.session.add(y3) c3 = Company(3, "Activision", 10, None, 1000, 6, 1980) db.session.add(c) db.session.add(c2) db.session.add(c3) db.session.commit() q = Company.query.all() self.assertEqual(len(q), 3) db.session.remove()
def test_company_1(self): y = Year(1986) db.session.add(y) c = Company(1,"Bethesda Softworks", 11, None, 66, 8.9, 1986) db.session.add(c) db.session.commit() self.assertTrue(c in db.session) db.session.remove()
def test_games_1(self): y = Year(2008) db.session.add(y) g = Game(1, "Fallout 3", rating = 87, release_year = 2008) db.session.add(g) db.session.commit() q = Game.query.all() self.assertEqual(len(q), 1) db.session.remove()
def test_company_3(self): y = Year(1986) db.session.add(y) c = Company(1, "Bethesda Softworks", 11, None, 66, 8.9, 1986) c2 = Company(2, "Bethesda Softworks", 11, None, 66, 8.9, 1986) db.session.add(c) self.assertTrue(c in db.session) self.assertFalse(c2 in db.session) db.session.remove()
def add_companies(companies, game): global company_cache global year_cache for company in companies: c={} company_to_update = None company_id = company["id"] if company_id in company_cache: company_to_update = Company.query.filter_by(company_id = company_id).first() if company["developer"]: company_to_update.num_developed += 1 if company["publisher"]: company_to_update.num_published += 1 db.session.commit() else: company_cache.add(company_id) c["company_id"] = company_id c["name"] = company["name"] c["num_developed"] = 1 if company["developer"] else 0 c["num_published"] = 1 if company["publisher"] else 0 url_specific_company = "https://www.igdb.com/api/v1/companies/" + str(company_id) r = requests.get(url_specific_company, params = header) company_info = r.json()["company"] if "founded_year" in company_info: year = company_info["founded_year"] c["year_founded"] = year if(year not in year_cache): y = Year(year) db.session.add(y) year_cache+= [year] if "average_rating" in company_info: c["avg_rating"] = company_info["average_rating"] else: c["avg_rating"] = 0.0 if "company_logo" in company_info: c["image_url"] = "https:" + company_info["company_logo"]["url"] company_to_update = Company(**c) db.session.add(company_to_update) game.associated_companies.append(company_to_update)
def db_import(): global year_cache global header db.drop_all() db.configure_mappers() db.create_all() game_cache = [g.game_id for g in Game.query.all()] platform_cache = [] # GAME DATA PULLING # data: id, name, release_date url = "https://www.igdb.com/api/v1/games" r = requests.get(url, params = header) j = r.json() # while len(j["games"]) > 0: while header["offset"] < 100: # pp = pprint.PrettyPrinter(indent = 4) # pp.pprint(j) games = j["games"] # loop through games for game in games: game_id = game["id"] if(game_id not in game_cache): game_cache += [game_id] name = game["name"] release_year = int((re.split("-", game["release_date"]))[0]) #check year cache before adding a new year if(release_year not in year_cache): y = Year(release_year) db.session.add(y) year_cache += [release_year] url_specific_game = "https://www.igdb.com/api/v1/games/" + str(game_id) r = requests.get(url_specific_game, params = header) #get specific game information game_info = r.json()["game"] #image image_url = None if("cover" in game_info and "url" in game_info["cover"]): image_url = "https:" + game_info["cover"]["url"] #rating rating = 0.0 if("rating" in game_info): rating = game_info["rating"] g = Game(id=game_id, name=name, image_url=image_url, rating=rating, release_year=release_year) #loop through platforms for v in game_info["release_dates"]: c = None platform = v["platform_name"] if platform not in platform_cache: platform_cache += [platform] c = Platform(platform) db.session.add(c) else: c = Platform.query.filter_by(platform_name = platform).first() g.associated_platforms.append(c) if "genres" not in game_info: continue add_genres(game_info["genres"], g) add_companies(game_info["companies"], g) #add game db.session.add(g) db.session.commit() r = requests.get(url, params = header) j = r.json() header["offset"] += 25