-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
executable file
·395 lines (352 loc) · 13.7 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
from __future__ import print_function
from flask import Flask, render_template, request, json, jsonify, Response
from flask.ext.cors import CORS, cross_origin
from datetime import datetime
from yahoo_finance import Share
import unirest
import pytz
from misc.settings import ACCESS_TOKEN, MONGODBPipeline
import misc.stock_processing as ms
import pymongo
from stop_words import get_stop_words
from collections import Counter, OrderedDict
# App config
app = Flask(__name__)
app.config.from_object(__name__)
# Cors Config
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
# Create Database Stock
db = MONGODBPipeline()
# Stopwords
stopW = set(get_stop_words('english'))
stopW.update(['will', 'can', 'just', 'www', 'com'])
# -------- Route to Manipulate Collection Infos --------
# API CIC(Collection Infos Create)
@app.route('/cic')
def createInfos():
if db.infos.count() == 0:
print("Creating Infos!!")
with open('static/sp100.json', 'rb') as f:
ls = json.load(f)
for i in ls:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
symbol = Share(i['name'])
item = {
'name': i['name'],
'price': symbol.get_price(),
'time': timestamp,
'prev_close': symbol.get_prev_close(),
'open': symbol.get_open(),
'volume': symbol.get_volume(),
'pe': symbol.get_price_earnings_ratio(),
'eps': symbol.get_earnings_share(),
'price_sales': symbol.get_price_sales(),
'ebitda': symbol.get_ebitda(),
'hotness': ms.hotness_function(i['name']),
'BS': ms.bs_function(i['name'])}
db.infos.insert_one({
"name": i['name'],
"sector": i['sector'],
"data": [item]
})
print('Collection Infos Created.')
return Response('Collection Infos Created.')
# createInfos()
# API CIU(Collection Iinfo Update)
@app.route('/ciu')
def updateInfos():
print("Updating Infos!")
with open('static/sp100.json', 'rb') as f:
ls = json.load(f)
for i in ls:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print (i['name'])
symbol = Share(i['name'])
item = {
'name': i['name'],
'price': symbol.get_price(),
'time': timestamp,
'prev_close': symbol.get_prev_close(),
'open': symbol.get_open(),
'volume': symbol.get_volume(),
'pe': symbol.get_price_earnings_ratio(),
'eps': symbol.get_earnings_share(),
'price_sales': symbol.get_price_sales(),
'ebitda': symbol.get_ebitda(),
'hotness': ms.hotness_function(i['name']),
'BS': ms.bs_function(i['name'])}
db.infos.update(
{"name": i['name']},
{
"$push": {"data": item}
}
)
print('Collection Infos Updated.')
return Response('Collection Infos Updated.')
# API CID(Collection Infos Delete)
@app.route("/cid")
def deleteInfos():
date = datetime.now()
start = date.replace(hour=9, minute=0).strftime("%Y-%m-%d %H:%M:%S")
with open('static/sp100.json', 'rb') as f:
ls = json.load(f)
for i in ls:
data = filter(lambda x: x['time'] > start, [
j for i in db.infos.find({'name': i['name']}) for j in i['data']])
db.infos.update_one({'name': i['name']}, {
'$set': {
'data': data
}
}, upsert=False)
return Response('Collection Infos Deleted.')
# --------- Route to Manipulate Collection Twits ----------
# API CTC(Collection Twits Create)
@app.route('/ctc')
def createTwits():
def bs(record):
if record is None:
return 0
else:
return 1 if record['basic'] == "Bullish" else -1
if db.twits.count() == 0:
with open('static/sp100.json', 'rb') as f:
ls = json.load(f)
url = "https://api.stocktwits.com/api/2/streams/symbol/{}.json?access_token={}"
for i in ls:
unirest.timeout(200)
response = unirest.get(url.format(
i['name'], ACCESS_TOKEN))
data = response.body
msgs = data['messages']
# print("Creating", i['name'], '....')
# print(db.twits.count())
for msg in msgs:
time = datetime.strptime(
msg['created_at'], "%Y-%m-%dT%H:%M:%SZ")
utc = pytz.utc
item = {
'name': msg['user']['username'],
'body': msg['body'],
'id': msg['id'],
'time': utc.localize(time).astimezone(pytz.timezone('US/Eastern')).strftime('%Y-%m-%d %H:%M:%S'),
'symbols': [i['symbol'] for i in msg['symbols']],
'reshares': msg['reshares']['reshared_count'],
'bs': bs(msg['entities']['sentiment'])}
try:
db.twits.replace_one(item, item, True)
except pymongo.errors.DuplicateKeyError:
pass
print('Collection Twits Created.')
return Response('Collection Twits Created.')
return Response('Error Occurs.')
# createTwits()
# API CTU(Collection Twits Update)
@app.route('/ctu')
def updateTwits():
def bs(record):
if record is None:
return 0
else:
return 1 if record['basic'] == "Bullish" else -1
print("Updating Twits!!")
with open('static/sp100.json', 'rb') as f:
ls = json.load(f)
url = "https://api.stocktwits.com/api/2/streams/symbol/{}.json?access_token={}"
for i in ls:
unirest.timeout(200)
response = unirest.get(url.format(
i['name'], ACCESS_TOKEN))
data = response.body
msgs = data['messages']
# print("Updating", i['name'])
# print(db.twits.count())
for msg in msgs:
time = datetime.strptime(
msg['created_at'], "%Y-%m-%dT%H:%M:%SZ")
utc = pytz.utc
item = {
'name': msg['user']['username'],
'body': msg['body'],
'id': msg['id'],
'time': utc.localize(time).astimezone(pytz.timezone('US/Eastern')).strftime('%Y-%m-%d %H:%M:%S'),
'symbols': [i['symbol'] for i in msg['symbols']],
'reshares': msg['reshares']['reshared_count'],
'bs': bs(msg['entities']['sentiment'])}
try:
db.twits.replace_one(item, item, True)
except pymongo.errors.DuplicateKeyError:
pass
print('Collection Twits Update.')
return Response('Collection Twits Updated.')
# API CTD(Collection Twits Delete)
@app.route("/ctd")
def deleteTwits():
date = datetime.now()
start = date.replace(hour=0, minute=0).strftime("%Y-%m-%d %H:%M:%S")
db.twits.remove({"time":
{"$lt": start}
})
print('Collection Twits Deleted.')
return Response('Collection Twits Deleted.')
# API CKC(Collection Keywords Create)
@app.route("/ckc")
def createKeywords():
if db.keywords.count() == 0:
print("Creating Keywords!!")
with open('static/sp100.json', 'rb') as f:
ls = json.load(f)
for i in ls:
if i['name'] == "BRKB":
symbol = "BRK.B"
else:
symbol = i['name']
twits = [ms.regex(i['body']) for i in db.twits.find(
{"symbols": {"$elemMatch": {"$eq": symbol}}}, projection={"_id": 0, "id": 0, "reshares": 0})]
words = reduce(lambda x, y: x + y, twits)
clean_words = Counter()
for key, value in words.iteritems():
if not key.lower() in stopW and key.isalpha() and len(key) >= 2:
clean_words[key[0].upper() + key[1:]] = value
item = {'name': symbol, 'data': OrderedDict(
clean_words.most_common(25))}
try:
db.keywords.replace_one(item, item, True)
except pymongo.errors.DuplicateKeyError:
pass
print('Collection Keywords Created.')
return Response('Collection Keywords Created.')
# API CKU(Collection Keywords Update)
@app.route("/cku")
def updateKeywords():
print("Updating Keywords!!")
with open("static/sp100.json", "rb") as f:
ls = json.load(f)
for i in ls:
if i['name'] == "BRKB":
symbol = "BRK.B"
else:
symbol = i['name']
twits = [ms.regex(i['body']) for i in db.twits.find(
{"symbols": {"$elemMatch": {"$eq": symbol}}}, projection={"_id": 0, "id": 0, "reshares": 0})]
words = reduce(lambda x, y: x + y, twits)
clean_words = Counter()
for key, value in words.iteritems():
if not key.lower() in stopW and key.isalpha() and len(key) >= 2:
clean_words[key[0].upper() + key[1:]] = value
item = {'name': symbol, 'data': OrderedDict(
clean_words.most_common(25))}
try:
db.keywords.replace_one(item, item, True)
except pymongo.errors.DuplicateKeyError:
pass
print("Collection Keywords Updated.")
return Response("Collection Keywords Updated.")
# API CKD(Collection Keywords Delete)
@app.route("/ckd")
def deleteKeywords():
db.keywords.delete_many({})
print('Collection Keywords Deleted.')
return Response('Collection Keywords Deleted.')
# ===============Route for API GET===============
# Route for homepage
@app.route("/")
def home():
return render_template("home.html", name="home")
# Route for getting symbol within sector
@app.route("/sectors", methods=["GET"])
@cross_origin()
def sectionAPI():
if not('sector' in request.args) or (request.args['sector'] == 'all'):
sector = "S&P 100 Index Symbols"
data = [i['data'][len(i['data']) - 1]
for i in db.infos.find()]
return jsonify({sector: data})
else:
sector = request.args['sector'][
0].upper() + request.args['sector'][1:] + " Sector Symbols"
data = [i['data'][len(i['data']) - 1]
for i in db.infos.find({'sector': request.args['sector']})]
return jsonify({sector: data})
# Route for searching specific symbol and it's general information
@app.route("/search", methods=["GET"])
@cross_origin()
def searchAPI():
name = request.args['symbol']
data = [j
for i in db.infos.find({'name': name}) for j in i['data'][-30:]]
return jsonify({'data': data})
# Route for searching specific symbol and return it's first 30 twits
@app.route("/twits", methods=["GET"])
@cross_origin()
def twitsAPI():
if not('symbol' in request.args) or (request.args['symbol'] == 'all'):
data = [i for i in db.twits.find(
{}, projection={"_id": 0, "id": 0, "reshares": 0}).sort('time', -1).limit(50)]
return jsonify({'data': data})
else:
name = request.args['symbol']
data = [i for i in db.twits.find(
{"symbols": {"$elemMatch": {"$eq": name}}},
projection={"_id": 0, "id": 0, "reshares": 0}).sort('time', -1).limit(50)]
return jsonify({'data': data})
# Route for searching specific symbol and return it's most common 25 keywords.
@app.route('/keywords', methods=["GET"])
@cross_origin()
def keywordsAPI():
name = request.args['symbol']
data = [i['data'] for i in db.keywords.find({'name': name})]
return jsonify({'data': data})
# Route for realted stocks
@app.route('/related', methods=["GET"])
@cross_origin()
def related():
symbol = request.args['symbol']
data = [i for i in db.twits.find({'symbols': {"elemMatch": {"$eq": symbol}}})]
return jsonify({'data': data})
# Route for listing the price of each stocks in the past 24 hours.
@app.route('/sparkline', methods=["GET"])
@cross_origin()
def sparkline():
name = request.args['symbol']
price = [j['price']
for i in db.infos.find({'name': name}) for j in i['data']]
bs = [j['BS']
for i in db.infos.find({'name': name}) for j in i['data']]
time = [j['time']
for i in db.infos.find({'name': name}) for j in i['data']]
return jsonify({'price': price,
'BS': bs,
'time': time,
'start': time[0][11:13] + ':00',
'end': time[-1][11:13] + ':00'})
# Error Handler
@app.errorhandler(404)
def not_found(error=None):
message = {
'status': 404,
'message': 'Not Found: ' + request.url,
}
return jsonify(message)
# # # Testing
# # : Route for testing hotness function
#
#
# @app.route("/hot", methods=['GET'])
# @cross_origin()
# def hot():
# symbol = request.args['symbol']
# hotness = ms.hotness_function(symbol)
# return jsonify({('{} hotness'.format(symbol)): hotness})
# # Route for testing bs function
#
#
# @app.route("/bs", methods=['GET'])
# @cross_origin()
# def bs():
# symbol = request.args['symbol']
# bs = ms.bs_function(symbol)
# return jsonify({('{} bs'.format(symbol)): bs})
# Route for getting price for specific symbol.
if __name__ == "__main__":
app.run(debug=True)