git.haldean.org apex / c48c31a
dump games to pandas-friendly json Haldean Brown 2 years ago
2 changed file(s) with 19 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
6363 flask.flash("User \"{}\" added".format(username))
6464 return flask.redirect(flask.url_for("adduser"))
6565
66 @app.route("/allgames/csv")
67 def allgames_csv():
68 user = users.session_user()
69 if user is None:
70 return flask.redirect(flask.url_for("index"))
71 csvdat = io.StringIO()
72 data.dump_games_to_csv(user, csvdat)
73 csvdat.seek(0)
74 return csvdat.read(), 200, {"Content-type": "text/plain"}
66 @app.route("/api/games/<username>")
67 def allgames_json(username):
68 return data.dump_games_to_json(username), 200, {"Content-type": "text/plain"}
7569
7670 @app.route("/logout")
7771 def logout():
4444 def get_user_games(username):
4545 return r.smembers("apex:games:{}".format(username))
4646
47 def dump_games_to_csv(username, file):
48 gamekeys = list(get_user_games(username))
49 gamewriter = csv.writer(file)
50 gamekeys.sort()
51 gamewriter.writerow(dump_fields)
52 for gamekey in gamekeys:
53 gamedata = json.loads(r.get(gamekey).decode("utf-8"))
54 gamewriter.writerow(gamedata.get(k) for k in dump_fields)
47 def get_game(gamekey):
48 return json.loads(r.get(gamekey).decode("utf-8"))
5549
50 def dump_games_to_json(username):
51 gamekeys = get_user_games(username)
52 rows = []
53 cols = sorted(list(valid_fields))
54 d = []
55 for key in gamekeys:
56 gamedata = get_game(key)
57 rows.append(gamedata.get("time"))
58 d.append([gamedata.get(k) for k in cols])
59 return json.dumps({
60 "rows": rows,
61 "cols": cols,
62 "data": d,
63 })