-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
221 lines (180 loc) · 5.49 KB
/
app.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
from flask import Flask, render_template, request, escape
from flask import redirect, url_for, make_response
from discord_text_parser import DiscordParser, ParseException
from clients.base_client import BaseClient, BaseCreds, TokenException, Clients
import logging
import datetime
from functools import wraps
app = Flask(__name__)
parser = DiscordParser()
TODAY = datetime.date.today()
CREDS = None
IS_DEV = False
CURRENT_CLIENT = None
logging.basicConfig(
filename=f"logs/mainapp_{TODAY}.log",
encoding="utf-8",
level=logging.INFO,
)
def check_credentials(f):
@wraps(f)
def wrapper(*args, **kwargs):
if CREDS or IS_DEV:
return f(*args, **kwargs)
else:
return render_template(
"creds.html", response="Please login.", client_list=list(Clients)
)
return wrapper
@app.route("/creds", methods=["POST"])
def _check_creds():
try:
if request.method == "POST":
account_id = int(request.form.get("account_id"))
consumer_key = request.form.get("consumer_key")
refresh_token = request.form.get("refresh_token")
set_client()
try:
cred_object = BaseCreds.get_creds_object(CURRENT_CLIENT)
creds = cred_object(account_id, consumer_key, refresh_token)
global CREDS
CREDS = creds
url = url_for("index")
except TokenException as e:
url = url_for("index", response=f"{e}")
except Exception as e:
url = url_for("index", response=f"Invalid entry.")
return redirect(url)
@app.route("/dev_login")
def _dev_login():
set_client()
if _is_dev():
set_dev()
return render_template(_get_index())
url = url_for("index", response="U R Not Dev")
return redirect(url)
@app.route("/")
def index():
if CREDS:
return render_template(_get_index())
else:
response = request.args.get("response", "")
return render_template(
"creds.html", response=response, client_list=list(Clients)
)
@app.route("/logout")
def logout():
global CREDS
global IS_DEV
global CURRENT_CLIENT
if CREDS:
CREDS = None
if IS_DEV:
IS_DEV = False
if CURRENT_CLIENT:
CURRENT_CLIENT = None
return render_template(
"creds.html", response="Logged Out.", client_list=list(Clients)
)
@app.route("/results")
@check_credentials
def return_results():
discord_text = request.args.get("discord_text")
response = request.args.get("response")
parsed_text = request.args.get("parsed_text")
return make_response(
render_template(
_get_index(),
input_text=discord_text,
parsed_text=parsed_text,
response=response,
),
200,
)
@app.route("/invalid")
@check_credentials
def invalid_results():
discord_text = request.args.get("discord_text")
response = request.args.get("response")
return make_response(
render_template(
_get_index(),
input_text=discord_text,
response=response,
),
400,
)
@app.route("/submit")
@check_credentials
def get_discord_text():
discord_text = str(escape(request.args.get("discord_text", "")))
try:
parsed_text, amount = _get_parsed_text_and_amount(discord_text)
except ParseException as e:
return redirect(
url_for(
"invalid_results",
discord_text=discord_text,
response=f"{e}",
)
)
url = _return_url(parsed_text, amount, discord_text)
return redirect(url)
def _return_url(parsed_text: str, amount: int, discord_text):
if parsed_text:
try:
response = _place_order(parsed_text, amount)
url = url_for(
"return_results",
discord_text=discord_text,
parsed_text=parsed_text,
response=response,
)
except TokenException:
url = url_for(
"invalid_results",
discord_text=discord_text,
response="Unable to connect to broker, please check credentials.",
)
except Exception as e:
url = url_for(
"invalid_results",
discord_text=discord_text,
response=f"{e}",
)
else:
url = url_for(
"invalid_results",
discord_text=discord_text,
response="Invalid Symbol",
)
return url
def _is_dev():
try:
client = BaseClient.get_client(CURRENT_CLIENT)
client()
return True
except TokenException:
return False
def set_client():
global CURRENT_CLIENT
CURRENT_CLIENT = request.form.get("client") or Clients.TD.value
def set_dev():
global IS_DEV
IS_DEV = True
def _get_index():
global IS_DEV
return "dev_index.html" if IS_DEV else "index.html"
def _get_parsed_text_and_amount(discord_text: str):
parsed_text, amount = (
parser.parse(discord_text) if discord_text.strip() else (None, None)
)
return parsed_text, amount
def _place_order(parsed_text: str, amount: int):
client = BaseClient.get_client(CURRENT_CLIENT)
client = client(CREDS)
response = client.place_order(parsed_text, amount)
return response
if __name__ == "__main__":
# app.run(host="127.0.0.1", port=8080, debug=True)
app.run(host="0.0.0.0", debug=True)