-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
433 lines (323 loc) · 11.6 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
from flask import Flask, render_template, redirect, request, flash, jsonify
from flask import session, send_from_directory
from flask_session import Session
from config import Config
from forms import UserNameForm
from forms import postUrlForm
from forms import TrailForm
from forms import DelegateForm
from markupsafe import escape
import BlurtChain as BC
import threading
import re
app = Flask(__name__)
app.config.from_object(Config)
Session(app)
@app.errorhandler(404)
# This handles 404 error
def page_not_found(e):
return render_template('404.html')
@app.route('/robots.txt/')
def robots():
return send_from_directory('static', 'robots.txt')
@app.route('/sitemap.xml/')
def sitemap():
return send_from_directory('static', 'sitemap.xml')
@app.route('/', methods=['GET', 'POST'])
def blurt():
form = UserNameForm(request.form)
# get ip address
ip_address = request.environ.get(
'HTTP_X_FORWARDED_FOR', request.remote_addr)
print('BLURT IP_ADDRESS:', ip_address)
if request.method == 'POST':
if form.validate():
username = request.form['username'].strip().lower()
return redirect(f'/{username}/')
# flash('Under Maintenance')
else:
flash('Username is Required')
return render_template('blurt/profile.html', form=form)
@app.route('/<username>')
@app.route('/<username>/')
def blurt_profile_data(username=None):
data = {}
ips = ['20.25.1.112']
# get ip address
ip_address = request.environ.get(
'HTTP_X_FORWARDED_FOR', request.remote_addr)
print('PROFILE IP_ADDRESS:', ip_address)
if ip_address in ips:
print(f'BAD_IP:', ip_address)
return render_template('404.html')
pattern = '^[a-zA-Z]+[a-zA-Z0-9-.]+$'
if not bool(re.match(pattern, username)):
print(f'INVALID_USERNAME:', username)
return render_template('404.html')
username = str(escape(username).lower())
if 3 <= len(username) <= 16:
blurt = BC.BlurtChain(username)
if blurt.account:
# check session profile_data
profile_data = username + '_profile_data'
if session.get(profile_data):
data = session[profile_data]
else:
data = blurt.get_account_info()
session[profile_data] = data
# threading processes in the background
# 1, 7, 30 day rewards
# transfer, upvote, comment history
# incoming delegation
threading_processes(username, blurt)
return render_template(
'blurt/profile_data.html',
username=blurt.username, data=data)
else:
return render_template('404.html')
@app.route('/blurt/upvote', methods=['GET', 'POST'])
@app.route('/blurt/upvote/', methods=['GET', 'POST'])
def upvote():
form = postUrlForm(request.form)
data = {
'banner': 'publish0x'
}
if request.method == 'POST':
if form.validate():
url = request.form['url'].strip().lower()
# boost = 'boost' in request.form
blurt = BC.BlurtChain(username=None)
forwarded_for = request.headers.getlist("X-Forwarded-For")
blurt.client_ip = ''
if forwarded_for:
blurt.client_ip = forwarded_for[0]
result = blurt.process_upvote(url)
flash(result['message'])
else:
# check empty url
flash('Error: URL is required')
return render_template('blurt/upvote.html', form=form, data=data)
@app.route('/blurt/trail', methods=['GET', 'POST'])
@app.route('/blurt/trail/', methods=['GET', 'POST'])
def trail():
# usernameform = UserNameForm(request.form)
form = TrailForm(request.form)
if request.method == 'POST':
if form.validate():
username = request.form['username'].strip().lower()
blurt = BC.BlurtChain(username=username)
posting = request.form['posting']
weight = request.form['weight']
# join or leave
if 'join' in request.form:
result = blurt.join_trail(username, posting, weight)
flash(result)
elif 'leave' in request.form:
result = blurt.leave_trail(username)
flash(result)
else:
flash('Username, Posting Key, Weight Required')
return render_template('blurt/trail.html', form=form)
@app.route('/blurt/delegate', methods=['GET', 'POST'])
@app.route('/blurt/delegate/', methods=['GET', 'POST'])
def delegate():
data = {}
form = DelegateForm(request.form)
if request.method == 'POST':
if form.validate():
BLT = BC.BlurtChain(username=None)
data['username'] = request.form['username']
data['wif'] = request.form['wif']
# Convert BP string into vests
try:
data['BP'] = int(request.form['amount'])
data['amount'] = data['BP']
vests = BLT.blurt.bp_to_vests(data['amount'])
vests = f'{vests:.6f}'
data['amount'] = vests
except Exception as err:
print('Exception', err)
data['amount'] = '0.000000'
return render_template('blurt/delegation.html', form=form, data=data)
@app.route('/blurt/leaderboard')
@app.route('/blurt/leaderboard/')
def leaderboard(username=None):
data = {}
blurt = BC.BlurtChain(username)
data = blurt.get_leaderboard()
return render_template('blurt/leaderboard.html',
data=data)
@app.route('/blurt/delegators')
@app.route('/blurt/delegators/')
def delegators(username=None):
data = {}
blurt = BC.BlurtChain(username)
data = blurt.get_delegators()
return render_template('blurt/delegators.html',
data=data)
@app.route('/blurt/exchange')
@app.route('/blurt/exchange/')
def exchange():
return render_template('blurt/exchange.html')
@app.route('/blurt/maintenance')
@app.route('/blurt/maintenance/')
def maintenance():
return render_template('blurt/maintenance.html')
def threading_processes(username, blurt):
if username is None:
return
# blurt = BC.BlurtChain(username)
# this thread runs in the background
# result is saved in db
durations = [30, 7, 1]
for duration in durations:
t = threading.Thread(
target=blurt.get_rewards, args=[duration])
t.start()
# Check incoming delegations and store in fb
threading.Thread(
target=blurt.get_incoming_delegation,
args=[username]).start()
# BLURT API
@app.route('/api/blurt/follower/<username>')
@app.route('/api/blurt/follower/<username>/')
def blurt_follower(username=None):
data = {}
if username:
blurt = BC.BlurtChain(username)
data = blurt.get_follower()
return jsonify(data)
@app.route('/api/blurt/following/<username>')
@app.route('/api/blurt/following/<username>/')
def blurt_following(username=None):
data = {}
if username:
blurt = BC.BlurtChain(username)
data = blurt.get_following()
return jsonify(data)
@app.route('/api/blurt/votes/<username>')
@app.route('/api/blurt/votes/<username>/')
def blurt_votes(username=None):
data = {}
if username:
blurt = BC.BlurtChain(username)
data = blurt.get_vote_history()
return jsonify(data)
@app.route('/api/blurt/mute/<username>')
@app.route('/api/blurt/mute/<username>/')
def blurt_mute(username=None):
data = {}
if username:
blurt = BC.BlurtChain(username)
data = blurt.get_mute()
return jsonify(data)
@app.route('/api/blurt/operations/<username>')
@app.route('/api/blurt/operations/<username>/')
def blurt_operations(username=None):
data = {}
if username:
blurt = BC.BlurtChain(username)
data = blurt.get_operations()
return jsonify(data)
@app.route('/api/blurt/delegation/<username>/<option>')
@app.route('/api/blurt/delegation/<username>/<option>/')
def blurt_delegation(username=None, option=None):
delegation_type = ["in", "out", "exp"]
data = {}
if username and option in delegation_type:
# check session delegation_data
delegation_data = username + '_delegation_' + option
if session.get(delegation_data):
data = session[delegation_data]
else:
blurt = BC.BlurtChain(username)
data = blurt.get_delegation_new(option)
session[delegation_data] = data
return jsonify(data)
@app.route('/api/blurt/reward/<username>/<int:duration>')
@app.route('/api/blurt/reward/<username>/<int:duration>/')
@app.route('/api/blurt/reward/<username>/<int:duration>/<option>')
@app.route('/api/blurt/reward/<username>/<int:duration>/<option>/')
def blurt_reward(username=None, duration=1, option=None):
data = {}
if username:
blurt = BC.BlurtChain(username)
# check session reward_data
reward_data = username + '_reward_' + str(duration)
if session.get(reward_data):
data = session[reward_data]
else:
data = blurt.get_reward_summary_fb(reward_data)
if not data:
data = blurt.get_rewards(duration)
session[reward_data] = data
return jsonify(data)
@app.route('/api/blurt/leaderboard')
@app.route('/api/blurt/leaderboard/')
def blurt_leaderboard(username=None):
data = {}
blurt = BC.BlurtChain(username)
data = blurt.get_leaderboard()
return jsonify(data)
@app.route('/api/blurt/ranking/<username>')
@app.route('/api/blurt/ranking/<username>/')
def blurt_ranking(username=None):
data = {}
if username:
blurt = BC.BlurtChain(username)
data = blurt.get_ranking(username)
return jsonify(data)
@app.route('/api/blurt/history/<username>/<string:option>')
@app.route('/api/blurt/history/<username>/<string:option>/')
def blurt_history(username=None, option=None):
data = dict()
if username and option:
blurt = BC.BlurtChain(username)
# check session history_data
history_data = username + '_history_' + str(option)
if session.get(history_data):
data = session[history_data]
else:
data = blurt.get_account_history_fb(history_data)
if not data:
data = blurt.get_history(username, option)
session[history_data] = data
blurt.remove_account_history_fb(history_data)
return jsonify(data)
@app.route('/api/blurt/votedata/<username>')
@app.route('/api/blurt/votedata/<username>/')
def blurt_votedata(username=None):
data = {}
if username:
blurt = BC.BlurtChain(username)
# check session data
vote_data = username + '_votedata'
if session.get(vote_data):
data = session[vote_data]
else:
data = blurt.get_upvote_data(username)
session[vote_data] = data
return jsonify(data)
@app.route('/api/blurt/trail-count')
@app.route('/api/blurt/trail-count/')
def curation_trail_count(username=None):
data = {}
blurt = BC.BlurtChain(username)
data = blurt.get_trail_count()
return jsonify(data)
@app.route('/api/blurt/msg-token', methods=['POST'])
def blurt_msg_token():
data = {}
token = ''
api_key = ''
if request.method == 'POST':
request_data = request.get_json()
if 'token' in request_data:
token = request_data['token']
if 'api_key' in request_data:
api_key = request_data['api_key']
blurt = BC.BlurtChain(username=None)
data = blurt.get_msg_token(api_key, token)
return jsonify(data)
if __name__ == "__main__":
app.run()