-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
276 lines (240 loc) · 10.1 KB
/
main.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import flask
import pandas as pd
import requests
import datetime
from zoneinfo import ZoneInfo
import firebase_admin
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from google.cloud import bigquery
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
from firebase_admin import credentials
from firebase_admin import firestore
from flask import request
import sys
from urllib.parse import urlparse
DEBUG = len(sys.argv) > 1 and sys.argv[1] == 'dev'
if DEBUG:
FIRESTORE_COLLECTION = u'deck-ids-dev'
else:
FIRESTORE_COLLECTION = u'deck-ids'
class SubmitForm(FlaskForm):
url = StringField('url', validators=[DataRequired()])
submit = SubmitField('Submit')
class UpdateForm(FlaskForm):
submit = SubmitField('Update Prices')
# Use the application default credentials
cred = credentials.ApplicationDefault()
firebase_admin.initialize_app(cred, {
'projectId': "nifty-beast-realm",
})
db = firestore.client()
def remove_nan(x):
if pd.isnull(x):
return 'NEW CARD'
else:
return x
def calculate_price_archidekt(data,url):
commander = 'Commander not found'
for card in data['cards']:
if 'Commander' in card['categories'] and ('Planeswalker' in card['card']['oracleCard']['types'] or 'Creature' in card['card']['oracleCard']['types']):
commander = card['card']['oracleCard']['name']
if data['deckFormat'] == 14:
deckFormat = 'oathbreaker'
elif data['deckFormat'] == 3:
deckFormat = 'edh'
else:
deckFormat = 'other'
cards = []
if len(data['cards']) > 0:
for card in data['cards']:
if 'Basic' not in card['card']['oracleCard']['superTypes'] and 'Maybeboard' not in card['categories'] and 'Sideboard' not in card['categories']:
cards.append(card['card']['oracleCard']['name'])
where_in_statement = '['
for card in cards:
where_in_statement += "\"{}\", ".format(card)
where_in_statement = where_in_statement[:-2] + ']'
card_prices = []
for card in cards:
doc_ref = db.collection(u'card-prices').document(card.replace('//','---'))
doc = doc_ref.get()
if doc.exists:
card_prices.append(doc.to_dict())
else:
flask.flash('Missing {}, contact the admin 💀'.format(card))
print(card,'MISSING!!')
historical = pd.DataFrame(card_prices)
price_list = historical[['name','price_season','price_season_new','price_season_combined']].sort_values(
by='price_season_combined', ascending=False)
price_list['price_season'] = price_list['price_season'].astype(float)
price_list['price_season'] = price_list['price_season'].round(2)
price_list['price_season_new'] = price_list['price_season_new'].round(2)
price_list = price_list.round(2).values.tolist()
flat_price_list = []
for value in price_list:
flat_price_list.append(remove_nan(value[0]))
flat_price_list.append(remove_nan(value[1]))
flat_price_list.append(remove_nan(value[2]))
res = {'name': data['name'],
'owner': data['owner']['username'],
'url': url.replace('/api', ''),
'commander': commander,
'cards': len(historical),
'commander_price' : round(historical['price_season_combined'][historical.name == commander].sum(), 2),
'free_cards': int((historical['price_season_combined'] == 0).sum()),
'id': data['id'],
'modified': str(datetime.datetime.today()),
'price_list': flat_price_list,
'deckFormat': deckFormat,
'deck_price_season': round(historical['price_season_combined'].sum(), 2),
'deck_price_season_new': round(historical['price_season_new'].sum(), 2),
'deck_price': round(historical['price_season_combined'].sum(), 2),
'deck_price_change': 0
}
return res
else:
return {'name': data['name'],
'owner': data['owner']['username'],
'url': url.replace('/api', ''),
'commander': commander,
'cards': 0,
'commander_price': 0,
'free_cards': 0,
'id': data['id'],
'modified': str(datetime.datetime.today()),
'price_list' : [],
'deckFormat' : 'n/a',
'deck_price_season': 0.0000000000000001,
'deck_price_season_new': 0.0000000000000001,
}
# def price_archidekt(url):
# data = requests.get(url=url).json()
# return calculate_price_archidekt(data,url)
app = flask.Flask(__name__)
bootstrap = Bootstrap(app)
app.config['SECRET_KEY'] = 'you-will-never-guess'
bigquery_client = bigquery.Client(project='nifty-beast-realm')
@app.route('/update_deck_id', methods=['POST'])
def update_deck_id():
data = update_deck(flask.request.form['id'])
return flask.jsonify(data)
def update_deck(archidekt_id):
url = 'https://archidekt.com/api/decks/{}/'.format(archidekt_id)
deck_request = requests.get(url)
if deck_request.status_code != 200:
db.collection(FIRESTORE_COLLECTION).document(archidekt_id).delete()
return False
else:
doc_ref = db.collection(FIRESTORE_COLLECTION).document(archidekt_id)
result = calculate_price_archidekt(deck_request.json(),url)
doc_ref.set(result)
return result
def parse_url(url):
o = urlparse(url)
split = o.path.split('/')
if len(split) < 3:
return False
archidekt_id = split[2]
if not archidekt_id.isdigit():
return False
return archidekt_id
def main_page(deckFormat,budget,experimental=False,request=None):
form = SubmitForm()
owners = []
if form.validate_on_submit():
archidekt_id = parse_url(form.url.data)
if archidekt_id:
if update_deck(archidekt_id):
return flask.redirect(flask.url_for(deckFormat))
else:
flask.flash('Failed to update deck URL: {}'.format(form.url.data))
else:
flask.flash('Bad archidekt URL: {}'.format(form.url.data))
return flask.redirect(flask.url_for(deckFormat))
else:
deck_ids_ref = db.collection(FIRESTORE_COLLECTION).order_by('modified', direction=firestore.Query.DESCENDING)
res = []
average_price = 0
owner_filter=None
if request:
if 'owner' in request.args:
owner_filter = request.args['owner']
for doc in deck_ids_ref.stream():
doc = doc.to_dict()
owners.append(doc['owner'])
if owner_filter and doc['owner'] != owner_filter:
continue
if doc['deckFormat'] != deckFormat:
continue
if "[P]" not in doc['name'] and not experimental:
continue
if not experimental:
doc['name'] = doc['name'].replace("[P]", "")
# Fix timezone to UK
doc['modified'] = pd.to_datetime(doc['modified'],utc=True).tz_convert('Europe/London').strftime('%Y-%m-%d %H:%M')
average_price += doc['deck_price_season']
res.append(doc)
if 'deck_price_season' not in doc:
doc['deck_price_season'] = 0.00
if len(res) > 0:
average_price = average_price/float(len(res))
else:
average_price = 0
return flask.render_template("index.html",
title=deckFormat,
experimental=experimental,
budget=budget,
average_price=round(average_price,2),
results=res,
form=form,
owners=list(set(owners)),
update_form=UpdateForm())
@app.route("/", methods=['GET', 'POST'])
def main():
return main_page('edh', budget=60, experimental=False,request=request)
@app.route("/beta", methods=['GET', 'POST'])
def beta():
return flask.render_template("beta.html",
title='edhbudget',
form=SubmitForm())
@app.route("/faq", methods=['GET', 'POST'])
def faq():
return flask.render_template("faq.html",
title='faq')
@app.route("/oathbreaker", methods=['GET', 'POST'])
def oathbreaker():
return main_page('oathbreaker',budget=35, experimental=True)
@app.route("/edh", methods=['GET', 'POST'])
def edh():
return main_page('edh', budget=60, experimental=False,request=request)
@app.route("/edh-experimental", methods=['GET', 'POST'])
def edh_experimental():
return main_page('edh', budget=60,experimental=True,request=request)
@app.route("/deck", methods=['GET', 'POST'])
def deck():
form = SubmitForm()
if form.validate_on_submit():
if not update_deck(form.url.data.split('/')[-1].split('#')[0]):
flask.flash('Bad archidekt URL! {}'.format(form.url.data))
return flask.redirect(flask.url_for('main'))
else:
doc_ref = db.collection(FIRESTORE_COLLECTION).document(request.args['archidekt_id'])
data = doc_ref.get().to_dict()
price_list = data['price_list']
res = []
for i in range(0, len(price_list), 3):
res.append([price_list[i],
price_list[i+1],
price_list[i+2]])
return flask.render_template("deck.html",
title='edhbudget',
results=res,
deck_name=data['name'],
form=form,
update_form=UpdateForm())
@app.route("/robots.txt")
def robots_dot_txt():
return "User-agent: *\nDisallow: /"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080, debug=DEBUG)