From 3e7d6cca9c6c454dc2943bb69a92949bf47394fc Mon Sep 17 00:00:00 2001 From: Killian Davitt Date: Sat, 23 Jul 2016 20:23:40 +0100 Subject: [PATCH 1/4] Provided a form to which the user is redirected to in order for them to create the secrets.json file via a web form. In addition, errors with authentication are now displayed in the web page proper, instead of via a werkzeug debug error. Debug mode can now be turned off by the standard user as it is not neccessary to display error messages. Templates are now seperated into a base template which provides basic styling and the actual content for different pages is placed in their own html file which extends the base.html --- api.py | 1 + app.py | 42 +++++++++++++++++++++----- templates/base.html | 27 +++++++++++++++++ templates/create_secrets.html | 23 ++++++++++++++ templates/{main.html => profiles.html} | 29 ++++++------------ 5 files changed, 96 insertions(+), 26 deletions(-) create mode 100644 templates/base.html create mode 100644 templates/create_secrets.html rename templates/{main.html => profiles.html} (71%) diff --git a/api.py b/api.py index 3fc4902..d8c9254 100644 --- a/api.py +++ b/api.py @@ -43,6 +43,7 @@ def __init__(self, facebook_auth_filename=SECRETS_FILENAME): self.authed = False self.profiles = None self.friends = set() + def _load_fb_auth(self): diff --git a/app.py b/app.py index 4e31ba5..27ecae3 100644 --- a/app.py +++ b/app.py @@ -1,18 +1,46 @@ - - -from flask import Flask, render_template +from flask import Flask, render_template, request import api app = Flask('tinder-detective') -stalker = api.NSASimulator() +stalker = "" @app.route('/') def index(): - profiles = stalker.get_profiles() - return render_template("main.html", profiles=profiles) + global stalker + + if stalker == "": + try: + stalker = api.NSASimulator() + except api.AuthenticationError: + return render_template("create_secrets.html") + + error = None + profiles = None + try: + profiles = stalker.get_profiles() + except api.AuthenticationError: + error = "There was a problem with authentication" + return render_template("profiles.html", profiles=profiles, error=error) + + +@app.route('/create_secret', methods=['POST']) +def create_secret(): + data = request.form + + user_id = data['user_id'] + user_secret = data['user_secret'] + + with open('SECRETS.json', 'w') as f: + f.write("{\n") + f.write(" \"facebook_id\": \"" + user_id + "\",") + f.write(" \"facebook_secret\": \"" + user_secret + "\"") + f.write("\n") + f.write("}") + + return "Successfully wrote to file, please reload the main page" if __name__ == '__main__': - app.run(debug=True) + app.run(debug=True, port=8080) diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..2c1eb62 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,27 @@ + + + + + + + + + + +
+
+

Tinder detective I guess

+ +

"It's important to note Tinder's not a secret considering 70% of users download Tinder because their friends recommend it."

+
+
+ + {% block content %} + + {% endblock %} + + + + + + diff --git a/templates/create_secrets.html b/templates/create_secrets.html new file mode 100644 index 0000000..c437662 --- /dev/null +++ b/templates/create_secrets.html @@ -0,0 +1,23 @@ +{% extends 'base.html' %} + +{% block content %} + +
+

Please create the SECRETS.json file

+ +
+ + Facebook user id:
+ + +

+ Facebook Tinder secret:
+ +
+ + + +
+ +
+{% endblock %} diff --git a/templates/main.html b/templates/profiles.html similarity index 71% rename from templates/main.html rename to templates/profiles.html index 929545a..3799529 100644 --- a/templates/main.html +++ b/templates/profiles.html @@ -1,22 +1,15 @@ - - - - +{% extends 'base.html' %} - +{% block content %} +
- - - - + {% endif %} -
+ {% if profiles %} {% for profile in profiles %}
@@ -57,10 +50,8 @@

{{ profile.name }}

{% endfor %} + {% endif %}
+ {% endblock %} - - - - From 4d0bbef4f4ecc0605e82d954bc081a6f4b772079 Mon Sep 17 00:00:00 2001 From: Killian Davitt Date: Sat, 23 Jul 2016 20:25:08 +0100 Subject: [PATCH 2/4] Turned off debug mode --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index 27ecae3..5aa11d1 100644 --- a/app.py +++ b/app.py @@ -43,4 +43,4 @@ def create_secret(): return "Successfully wrote to file, please reload the main page" if __name__ == '__main__': - app.run(debug=True, port=8080) + app.run(debug=False, port=8080) From a45314479556aed1de6f804b7256aa817ecb7e9b Mon Sep 17 00:00:00 2001 From: Killian Davitt Date: Sat, 23 Jul 2016 22:31:19 +0100 Subject: [PATCH 3/4] Fixed mistake in the SECRETS.json writing --- app.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 5aa11d1..2d52d79 100644 --- a/app.py +++ b/app.py @@ -35,8 +35,8 @@ def create_secret(): with open('SECRETS.json', 'w') as f: f.write("{\n") - f.write(" \"facebook_id\": \"" + user_id + "\",") - f.write(" \"facebook_secret\": \"" + user_secret + "\"") + f.write(" \"facebook_id\": \"" + user_id + "\",\n") + f.write(" \"facebook_token\": \"" + user_secret + "\"") f.write("\n") f.write("}") From 2cd37a2299b67167587a764c446c8efe538e9c31 Mon Sep 17 00:00:00 2001 From: Killian Davitt Date: Mon, 25 Jul 2016 12:08:39 +0100 Subject: [PATCH 4/4] Changed 'stalker' object to use the flask g var instead of python global variables --- app.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index 5aa11d1..dc56c5f 100644 --- a/app.py +++ b/app.py @@ -1,27 +1,29 @@ -from flask import Flask, render_template, request +from flask import Flask, render_template, request, g import api app = Flask('tinder-detective') -stalker = "" - @app.route('/') def index(): - global stalker + + stalker = getattr(g, 'stalker', None) - if stalker == "": + if stalker is None: try: stalker = api.NSASimulator() except api.AuthenticationError: return render_template("create_secrets.html") + g.stalker = stalker + error = None profiles = None try: profiles = stalker.get_profiles() except api.AuthenticationError: + print("Auth error") error = "There was a problem with authentication" return render_template("profiles.html", profiles=profiles, error=error) @@ -35,7 +37,7 @@ def create_secret(): with open('SECRETS.json', 'w') as f: f.write("{\n") - f.write(" \"facebook_id\": \"" + user_id + "\",") + f.write(" \"facebook_id\": \"" + user_id + "\",\n") f.write(" \"facebook_secret\": \"" + user_secret + "\"") f.write("\n") f.write("}")