-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
564 lines (488 loc) · 20.2 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
'''
app.py
Server file.
'''
import os
import json
import random
from datetime import datetime, timedelta
from flask import Flask, send_from_directory, request
from flask_socketio import SocketIO
from flask_cors import CORS
from dotenv import load_dotenv, find_dotenv
from stock import Stock
from database import DB
from stock_utils import parse_api_data
APP = Flask(__name__, static_folder='./build/static')
load_dotenv(find_dotenv())
APP.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URL')
# Gets rid of a warning
APP.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
DB.init_app(APP)
import models
# Datetime object to store the last time the database was updated
# Server-wide variable compared to each incoming client-side request
LAST_UPDATED_TIME = None
USER_LIST = [] #{socket_id: id, name: name}
#{"socket_id": "abc123", "name":"user1"}, {"socket_id": "def123", "name":"user2"}
COR_S = CORS(APP, resources={r"/*": {"origins": "*"}})
SOCKET_IO = SocketIO(APP,
cors_allowed_origins="*",
json=json,
manage_session=False)
@APP.route('/', defaults={"filename": "index.html"})
@APP.route('/<path:filename>')
def index(filename):
""" index """
return send_from_directory('./build', filename)
# When a client connects from this Socket connection, this function is run
@SOCKET_IO.on('connect')
def on_connect():
""" Checks if user connected and sends socket_id """
print("User connected! " + request.sid)
@SOCKET_IO.on('disconnect')
def on_disconnect():
"""Checks if user is disconnected and removes them from user list"""
global USER_LIST
for user in USER_LIST:
if user['socket_id'] == request.sid:
USER_LIST.remove(user)
print('User disconnected!')
display_list = [user['name'] for user in USER_LIST]
SOCKET_IO.emit('disconnect',
display_list,
broadcast=True,
include_self=True)
@SOCKET_IO.on('login')
def on_login(data): #{socket_id: socket_id, username: email}
""" Sends updated list of active users to client """
global USER_LIST
try: #Checks if socket is in list
if not any(d['socket_id'] == data['socket_id'] for d in USER_LIST):
user = {'socket_id': data['socket_id'], 'name': data['username']}
USER_LIST.append(user)
except KeyError:
print("Key Error!")
display_list = []
for user in USER_LIST:
if user['name'] not in display_list:
display_list.append(user['name'])
print("list " + str(display_list))
SOCKET_IO.emit('login', display_list, broadcast=True, include_self=True)
# @SOCKET_IO.on('logout')
# def on_logout(data):
# """ Removes user from list after logging out"""
# global USER_LIST
# for user in USER_LIST:
# if user['socket_id'] == request.sid:
# USER_LIST.remove(user)
# print('User disconnected!')
# display_list = [user['name'] for user in USER_LIST]
# SOCKET_IO.emit('disconnect', display_list, broadcast=True, include_self=True)
# @SOCKET_IO.on('like')
# def on_like(data): #{symbol: symbol, like : bool}
# """Takes stock info with new like number, changes it in the db, and emits to others"""
# symbol = data['symbol']
# stock = models.Stocks.query.filter_by(symbols=symbol).first()
# stock = DB.session.query(models.Stocks).filter(models.Stocks.symbols == symbol).first()
# if data['like'] is True:
# stock.likes += 1
# else:
# stock.likes -= 1
# DB.session.commit()
# #Dont know exactly how what I want to emit: The whole list of stocks from the
# #home page or just the one stock
# #SOCKET_IO.emit('like', )
@APP.route('/stocks', methods=['GET'])
def stocks():
""" Home screen stock lists that shows new stock info every 15 minutes"""
global USER_LIST
# With the actual data, we would query an SQLAlchemy model
# for all records and convert the result to JSON. Haven't looked much
# into converting SQLAlchemy query results to JSON, but it should be
# straight forward
stock = Stock()
weekdays = [0, 1, 2, 3, 4]
curr_day = datetime.today().weekday()
now = datetime.now()
curr_date = curr_date = now.strftime("%Y-%m-%d")
open_time = datetime.strptime('{} 9:30AM'.format(curr_date),
'%Y-%m-%d %I:%M%p')
close_time = datetime.strptime('{} 4:00PM'.format(curr_date),
'%Y-%m-%d %I:%M%p')
if (curr_day in weekdays and open_time <= now <= close_time):
# Get the last updated time for the server
global LAST_UPDATED_TIME
if (LAST_UPDATED_TIME is None
or now > LAST_UPDATED_TIME + timedelta(minutes=15)):
# Call the API to update records in the database
api_data = stock.default()
LAST_UPDATED_TIME = now
stocks_data = parse_api_data(api_data)
# Add data into db and commit data
add_stocks_db(stocks_data)
else:
# 15 hasn't passed since last database update
# Read from database only
# Gather 4 random stocks from each category
stocks_data = get_random_stocks_db()
else:
if LAST_UPDATED_TIME is None:
# Just in case it's not during market hours
# and the database could be empty
api_data = stock.default()
LAST_UPDATED_TIME = now
stocks_data = parse_api_data(api_data)
# Add data into db and commit data
add_stocks_db(stocks_data)
else:
print("After hour stock data")
stocks_data = get_random_stocks_db()
display_list = []
for user in USER_LIST:
if user['name'] not in display_list:
display_list.append(user['name'])
return {"stocks_data": stocks_data, "display_list": display_list}
@APP.route('/stock_page', methods=['POST'])
def stock_page():
""" Function that sends individual stock info to stock page """
# We're assuming that the requested stock will exist in the database
# when the stock page link is accessed. So we simply query the database
# for the symbol request sent by the client
# stock = Stock()
# weekdays = [0,1,2,3,4]
# curr_day = datetime.datetime.today().weekday()
# if curr_day in weekdays:
# #Include line where stock info is stored into db between api call times
news_data = []
page_data = {}
stock_data = {}
comment_data = []
stock_id = None
# Get the stock id sent from the client side
content = request.get_json(force=True)
user_symbol = content.get('stock_symbol').upper()
stock_obj = Stock()
# Check if the request stock exists in the database
stock_record = models.Stocks.query.filter_by(symbols=user_symbol).first()
if stock_record is None:
# Query the API for user requested stock
api_data = stock_obj.search([user_symbol], None)
if 'Not Found' in api_data.values():
stock_data.update({'Error': 'No Results Found'})
else:
stock_data = parse_api_data(api_data)
add_stocks_db(stock_data)
else:
# To get page data, we would search the database's Comments table
# to get all of the comment records matching the stock_symbol passed in via the client side
# Convert this info. to JSON format and send it back to the client
with open('test_stock_page.json', 'r') as json_file:
page_data = json.loads(json_file.read())
stock_id = stock_record.id
# Get the stock info from the DB
stock_data = {}
stock_data.update({
'Symbol': stock_record.symbols,
'Company': stock_record.stocks_name,
'High': stock_record.high_stocks,
'Low': stock_record.low_stocks,
'Price': stock_record.current_price,
'Category': stock_record.category,
'Likes': stock_record.likes
})
try:
news_data = stock_obj.news(stock_data['Company'])
except KeyError:
news_data.append({'Error': 'Couldn\'t Retrieve News Data'})
if stock_id is not None:
comments = models.Comments.query.filter_by(stocks_column=stock_id).all()
for comment in comments:
comment_data.append({
"username": comment.username,
"message": comment.comment
})
#for comment in comments:
#page_data.append(comment.comment)
#print(l)
#print(page_data)
page_data['comments'] = comment_data
return {
'stock_data': stock_data,
"page_data": page_data,
"news_data": news_data
}
def add_stocks_db(data):
''' Insert stock data into the DB '''
all_stocks = data['allStocks']
with APP.app_context():
for i in all_stocks:
if len(i) == 3:
symbol = i['Symbol']
current = i['Price']
categories = i['Category']
crypto_record = models.Crypto.query.filter_by(
symbols=symbol).first()
if crypto_record is not None:
crypto_record.current_price = current
else:
new_crypto = models.Crypto(symbols=symbol,
current_price=current,
likes=0,
category=categories)
DB.session.add(new_crypto)
DB.session.commit()
else:
stockname = i['Company']
symbol = i['Symbol']
high = i['High']
low = i['Low']
current = i['Price']
categories = i['Category']
# Check if the stock record already exists
stock_record = models.Stocks.query.filter_by(
symbols=symbol).first()
if stock_record is not None:
# Update the stock's pricing info in the DB
stock_record.high_stocks = high
stock_record.low_stocks = low
stock_record.current_price = current
else:
# Add the new stock record to the DB
new_stock = models.Stocks(stocks_name=stockname,
symbols=symbol,
high_stocks=high,
low_stocks=low,
current_price=current,
likes=0,
category=categories)
DB.session.add(new_stock)
DB.session.commit()
def get_random_stocks_db():
''' Get 4 random stocks from each of the predefined categories '''
stocks_data = {'allStocks': []}
for category in ['Mega', 'Finance', 'Energy', 'Utilities', 'Tech']:
category_stocks = models.Stocks.query.filter_by(
category=category).all()
random_stocks = random.sample(category_stocks,
len(category_stocks))[:4]
for stock in random_stocks:
stocks_data['allStocks'].append({
'Symbol': stock.symbols,
'Company': stock.stocks_name,
'High': stock.high_stocks,
'Low': stock.low_stocks,
'Price': stock.current_price,
'Category': stock.category,
'Likes': stock.likes
})
#For crypto
crypto_cat = 'Cryptocurrency'
category_crypto = models.Crypto.query.filter_by(category=crypto_cat).all()
random_crypto = random.sample(category_crypto, len(category_crypto))[:4]
for crypto in random_crypto:
stocks_data['allStocks'].append({
'Symbol': crypto.symbols,
'Price': crypto.current_price,
'Category': crypto.category
})
return stocks_data
@APP.route('/like_stock', methods=['POST'])
def like_stock():
''' Insert or delete a record from a user's liked stocks '''
content = request.get_json(force=True)
user_symbol = content.get('stock_symbol').upper()
email = content.get('email')
print('Email ' + email + ' clicked stock: ' + user_symbol)
### Proposed DB Logic ####
# if a record in the Likes table with matching email & symbol exists:
# it's a dislike click so remove that record from the table
# else:
# create a record with client's email / stock symbol in the table
#check if the like tables has matching email& SYMBOL EXIST
stock_list = []
stock_record = models.Person.query.filter_by(username=email).first()
for i in stock_record.all_stocks:
s_p = i.stocks
stock_list.append(s_p)
if user_symbol not in stock_list:
with APP.app_context():
check_symbol = models.Stocks.query.filter_by(
symbols=user_symbol).first()
if check_symbol is None:
increment_like = models.Crypto.query.filter_by(
symbols=user_symbol).first()
increment_like.likes = increment_like.likes + 1
else:
increment_like = models.Stocks.query.filter_by(
symbols=user_symbol).first()
increment_like.likes = increment_like.likes + 1
# Add the symbol to the Like table
new_user = models.Liketable(person=stock_record.id,
stocks=user_symbol)
DB.session.add(new_user)
DB.session.commit()
else:
# Delete the User symbol from the database
with APP.app_context():
check_symbol = models.Stocks.query.filter_by(
symbols=user_symbol).first()
if check_symbol is None:
decrement_like = models.Crypto.query.filter_by(
symbols=user_symbol).first()
decrement_like.likes = decrement_like.likes - 1
else:
decrement_like = models.Stocks.query.filter_by(
symbols=user_symbol).first()
decrement_like.likes = decrement_like.likes - 1
# Deleting the record from DB
ndel = models.Liketable.query.filter_by(stocks=user_symbol).first()
DB.session.delete(ndel)
DB.session.commit()
return {}
@APP.route('/login', methods=['POST'])
def login():
''' Insert a user record into DB if needed '''
content = request.get_json(force=True)
email = content.get('email')
print('User with email ' + email + ' logged in')
stock_record = models.Person.query.filter_by(username=email).first()
with APP.app_context():
if stock_record is None:
new_user = models.Person(username=email, bio='')
DB.session.add(new_user)
DB.session.commit()
return {}
@APP.route('/get_liked_stocks', methods=['POST'])
def get_liked_stocks():
''' Get a user's liked stocks '''
content = request.get_json(force=True)
email = content.get('email')
print('Get liked stocks for ' + email)
### Proposed DB Logic ####
# Get all records from Likes table matching email
# Return records in JSON format like in
# test_stock_data.json
# else:
# Return {'myLikedStocks': []}
test_data = {'myLikedStocks': []}
stock_record = models.Person.query.filter_by(username=email).first()
if stock_record is not None:
for i in stock_record.all_stocks:
s_p = i.stocks
search_stocks = models.Stocks.query.filter_by(symbols=s_p).first()
crypto_search = models.Crypto.query.filter_by(symbols=s_p).first()
if search_stocks is None:
if s_p in crypto_search.symbols is not None:
test_data['myLikedStocks'].append({
'Symbol':
crypto_search.symbols,
'Price':
crypto_search.current_price,
'Category':
crypto_search.category
})
else:
if s_p in search_stocks.symbols is not None:
test_data['myLikedStocks'].append({
'Symbol':
search_stocks.symbols,
'Company':
search_stocks.stocks_name,
'High':
search_stocks.high_stocks,
'Low':
search_stocks.low_stocks,
'Price':
search_stocks.current_price,
'Category':
search_stocks.category,
'Likes':
search_stocks.likes
})
liked_stocks = {}
# Open a file to write the like stocks
with open('test_liked_stocks.json', 'w') as json_file:
json.dump(test_data, json_file, indent=4)
# Read the file from the JSON file
with open('test_liked_stocks.json', 'r') as json_file:
liked_stocks = json.loads(json_file.read())
return liked_stocks
@APP.route('/submit_comment', methods=['POST'])
def submit_comment():
''' Insert a user's comment into the DB '''
content = request.get_json(force=True)
email = content.get('email')
stock_symbol = content.get('stock_symbol')
comment = content.get('comment')
print('Email ' + email + ' commented on stock ' + stock_symbol +
'\nmessage: ' + comment)
stock = models.Stocks.query.filter_by(symbols=stock_symbol).first()
#print(stock.stocks_name)
print(stock.id)
stock_id = stock.id
with APP.app_context():
new_comment = models.Comments(username=email,
comment=comment,
owner=stock.id)
DB.session.add(new_comment)
DB.session.commit()
#DB.session.close()
comment_data = []
if stock_id is not None:
comments = models.Comments.query.filter_by(stocks_column=stock_id).all()
for comment in comments:
comment_data.append({
"username": comment.username,
"message": comment.comment
})
#print(comment_data)
comment_data = {'message': comment, 'username': email}
#page_data = {'comment' : comment_data}
SOCKET_IO.emit('new_comment', comment_data, broadcast=True, include_self=True)
return {}
@APP.route('/delete_comment', methods=['POST'])
def delete_comment():
''' Delete a comment from database'''
content = request.get_json(force=True)
email = content.get('email')
stock_symbol = content.get('stock_symbol')
comment = content.get('comment')
stock = models.Stocks.query.filter_by(symbols=stock_symbol).first()
user_comments = models.Comments.query.filter_by(
comment=comment, username=email, stocks_column=stock.id).first()
DB.session.delete(user_comments)
DB.session.commit()
@APP.route('/get_user_profile', methods=['POST'])
def get_user_profile():
''' Get the user's profile details from database'''
content = request.get_json(force=True)
email = content.get('email')
print('Get user profile for ' + email)
# Getting bio from databse
my_profile = {'Bio': 'Bio not set'}
profile_query = models.Person.query.filter_by(username=email).first()
my_profile['Bio'] = profile_query.bio
return my_profile
@APP.route('/update_bio', methods=['POST'])
def update_bio():
''' Update user's bio on the database '''
content = request.get_json(force=True)
email = content.get('email')
new_bio = content.get('newBio')
# Inserting new bio into database
print("Updating " + email + "'s bio to " + new_bio)
user = models.Person.query.filter_by(username=email).first()
user.bio = new_bio
DB.session.commit()
return {}
if __name__ == "__main__":
# Note that we don't call APP.run anymore. We call SOCKET_IO.run with APP arg
# stocks()
with APP.app_context():
DB.create_all()
SOCKET_IO.run(
APP,
host=os.getenv('IP', '0.0.0.0'),
port=8081 if os.getenv('C9_PORT') else int(os.getenv('PORT', 8081)),
)