-
Notifications
You must be signed in to change notification settings - Fork 1
/
atlas_api_helper.py
174 lines (143 loc) · 6.37 KB
/
atlas_api_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import requests
import crud
import model
from flask_sqlalchemy import SQLAlchemy
def search_board_game_atlas(search_terms):
"""Makes call to Board Game Atlas API using search terms"""
payload = {'name': search_terms,
'fields': 'id,name,min_players,max_players,publishers,image_url',
'limit': '10',
'fuzzy_match': 'True',
'pretty': 'True',
'client_id': 'ZRKfcqdFcV'}
response = requests.get('https://api.boardgameatlas.com/api/search', params=payload)
api_results = response.json()
# Get the list of games returned
games = api_results["games"]
search_results = []
for game in games:
try:
publisher = game["publishers"][0]
except:
publisher = ""
search_results.append(
{
"key": game["id"],
"name": game["name"],
"min_players": game["min_players"],
"max_players": game["max_players"],
"publisher": publisher,
"image_url": game["image_url"]
}
)
return search_results
def add_game_to_database(add_type, name, user_id, atlas_id=None):
"""Adds game to either UserGame or WantedGame tables"""
if atlas_id:
#Check if there's record of this game in db with matching atlas id
existing_game = crud.get_game_by_atlas_id(atlas_id)
else:
existing_game = crud.get_game_by_name(name)
if existing_game:
game_id = existing_game.id
else:
# If not in db, get object from Atlas API and save details in
# Game, GameMechanic, Category, GameCategory, Publisher, GamePublisher,
#Designer, and GameDesigner tables
atlas_id_array = atlas_id + ","
payload = {'ids': atlas_id_array,
'client_id': 'ZRKfcqdFcV'}
response = requests.get('https://api.boardgameatlas.com/api/search', params=payload)
print(response)
new_game_response = response.json()
new_game_data = new_game_response["games"][0]
name = new_game_data["name"]
description = new_game_data["description"]
publish_year = new_game_data["year_published"]
min_age = new_game_data["min_age"]
min_players = new_game_data["min_players"]
max_players = new_game_data["max_players"]
min_playtime = new_game_data["min_playtime"]
max_playtime = new_game_data["max_playtime"]
image_url = new_game_data["image_url"]
msrp = float(new_game_data["msrp"])
atlas_id = new_game_data["id"]
game = crud.create_game(name, description, publish_year, min_age,
min_players, max_players, min_playtime, max_playtime,
image_url, msrp, atlas_id)
game_id = game.id
# Create GameMechanic objects for game's mechanics
# Get a list of dictionaries containing mechanic atlas_id's
mechanics_list = new_game_data["mechanics"]
# Iterate through the list, as long as it's not empty
if len(mechanics_list) > 0:
for mechanic in mechanics_list:
atlas_id = mechanic["id"]
# Try to match the atlas_id with what's in db to get mech id.
# If can't find a match, skip it.
try:
mech = model.db.session.query(model.Mechanic).filter_by(
atlas_id = atlas_id).first()
crud.create_game_mechanic(game_id, mech.id)
except:
continue
# Create GameCategory objects for game's categories
# Get a list of dictionaries containing category atlas_id's
categories_list = new_game_data["categories"]
# Iterate through the list, as long as it's not empty
if len(categories_list) > 0:
for category in categories_list:
atlas_id = category["id"]
# Try to match the atlas_id with what's in db to get cat id.
# If can't find a match, skip it.
try:
cat = model.db.session.query(model.Category).filter_by(
atlas_id = atlas_id).first()
crud.create_game_category(game_id, cat.id)
except:
continue
# Create Publisher objects for game's publishers
# Get a list of publishers
publishers_list = new_game_data["publishers"]
# Iterate through the list, as long as it's not empty
if len(publishers_list) > 0:
for publisher in publishers_list:
publisher = publisher.strip()
# See if publisher already exists in db
existing_pub = model.Publisher.query.filter_by(
name = publisher).first()
if existing_pub:
crud.create_game_publisher(game_id, existing_pub.id)
else:
new_pub = crud.create_publisher(publisher)
crud.create_game_publisher(game_id, new_pub.id)
# Create Designers objects for game's designers
# Get a list of designers
designers_list = new_game_data["designers"]
# Iterate through the list, as long as it's not empty
if len(designers_list) > 0:
for designer in designers_list:
designer = designer.strip()
# See if designer already exists in db
existing_designer = model.Designer.query.filter_by(
name = designer).first()
if existing_designer:
crud.create_game_designer(game_id,
existing_designer.id)
else:
new_designer = crud.create_designer(designer)
crud.create_game_designer(game_id, new_designer.id)
#After assigning game_id above, can add game to UserGame or WantedGame
added_game = False
if add_type == "own":
# Will need to replace 1 with username
added_game = crud.create_user_game(user_id, game_id)
elif add_type == "wishlist":
added_game = crud.create_wanted_game(user_id, game_id)
if added_game:
return "Game was successfully added"
else:
return "A problem has occurred"
if __name__ == '__main__':
from server import app
model.connect_to_db(app)