From 0c92b0b9cd8ab8da8e91e583e872c346f7b1fdf6 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sun, 19 Mar 2017 13:46:50 -0400 Subject: [PATCH] Initial super-trivial server --- playwithfriends/intersections.py | 3 ++- playwithfriends/server.py | 34 +++++++++++++++++++++++++++++++ playwithfriends/static/index.html | 13 ++++++++++++ playwithfriends/templates.py | 15 ++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 playwithfriends/server.py create mode 100644 playwithfriends/static/index.html create mode 100644 playwithfriends/templates.py diff --git a/playwithfriends/intersections.py b/playwithfriends/intersections.py index 0d6e63a..3975667 100644 --- a/playwithfriends/intersections.py +++ b/playwithfriends/intersections.py @@ -6,6 +6,7 @@ def intersecting_games(*args): api = steam.SteamAPI() games_list = [api.get_games(steamid) for steamid in args] - ids_mutual = reduce(lambda l, r: set(l.keys()).intersection(r.keys()), games_list) + game_ids = [g.keys() for g in games_list] + ids_mutual = reduce(lambda l, r: set(l).intersection(r), game_ids) return {game_id: games_list[0][game_id] for game_id in ids_mutual} diff --git a/playwithfriends/server.py b/playwithfriends/server.py new file mode 100644 index 0000000..60846f1 --- /dev/null +++ b/playwithfriends/server.py @@ -0,0 +1,34 @@ +from bottle import route, run, request + +from playwithfriends.templates import render_template +from playwithfriends.steam import SteamAPI +from playwithfriends.intersections import intersecting_games + + +@route('/') +def index(): + return render_template('index.html') + + +@route('/get_friends') +def get_friends(): + api = SteamAPI() + steam64_id = api.get_steam64_from_profile(request.params['profile_url']) + friends_ids = api.get_friends_ids(steam64_id) + + return api.get_player_names(friends_ids) + + +@route('/get_games') +def get_games(): + player_ids = request.params['player_ids'].split(',') + inter_games = intersecting_games(*player_ids) + + return inter_games + + +def main(): + run(host='localhost', port=8000) + +if __name__ == '__main__': + main() diff --git a/playwithfriends/static/index.html b/playwithfriends/static/index.html new file mode 100644 index 0000000..3f8889d --- /dev/null +++ b/playwithfriends/static/index.html @@ -0,0 +1,13 @@ + + + + + + + Document + + +

The Server is Working!

+ + \ No newline at end of file diff --git a/playwithfriends/templates.py b/playwithfriends/templates.py new file mode 100644 index 0000000..60b00df --- /dev/null +++ b/playwithfriends/templates.py @@ -0,0 +1,15 @@ +from os import path +from bottle import SimpleTemplate + + +def render_template(template_name, params=None): + if params is None: + params = {} + + package_dir = path.abspath(path.dirname(__file__)) + static_dir = path.join(package_dir, 'static') + + with open(path.join(static_dir, template_name), 'r') as handle: + template = SimpleTemplate(handle.read()) + + return template.render(**params)