-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
185 lines (177 loc) · 5.38 KB
/
main.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
from flask import Flask, render_template, jsonify, request
from flask_cors import CORS
import json
import os
from crawler import *
from predictions import *
import numpy as np
app = Flask('Website Timer')
CORS(app)
d = {}
websites = {}
times = {}
predict = [0]
#get -> return data to extension
#post -> get data from extension
@app.route('/test', methods = ['GET','POST'])
def testfunc():
print("\ntestfunc")
message = {'flask': 'Hello World'}
print('sending hello world to extension')
data = request.get_json()
if data == {}:
return jsonify({'flask': 'nothing received'})
return jsonify(message)
@app.route('/saveTime', methods=['POST'])
#@cross_origin()
def saveTime():
print('\nsaving time')
data = request.get_json()
print(data)
if data == {}:
return jsonify({'flask': 'nothing received'})
name = data['username']
time = data['time']
website = data['website']
print(name, time, website)
print(d)
print(times)
#save_file()
#store stats of website into server
if website not in websites.keys():
websites[website] = webscrape(website)
try:
times[name].append([time, websites[website][1],websites[website][3],websites[website][4]])
except:
times[name] = [[time,websites[website][1],websites[website][3],websites[website][4]]]
#store user data into server
if name in d.keys():
if website in d[name].keys():
d[name][website].append(time)
else:
d[name][website] = [time]
else:
d[name] = {}
d[name][website] = [time]
with open("data.json", "w") as f:
json.dump(d, f)
print(d)
print(times)
with open("time.json", "w") as f:
json.dump(times,f)
try:
temp = sorted(d[name][website])[:10]
except KeyError:
return jsonify({'flask': 'nothing received'})
return jsonify({"time": temp})
@app.route('/showPrediction', methods=['GET', 'POST'])
def showPrediction():
data = request.get_json()
if data == {}:
return jsonify({'flask': 'nothing received'})
name = data['username']
website = data['website']
#store stats of website into server
if website not in websites.keys():
websites[website] = webscrape(website)
try:
test = [websites[website][1],websites[website][3],websites[website][4]]
temp2 = websites[website]
temp2[0] = temp2[0][:10]
predict[0] = (predictTimes(times[name], test))
temp2[2] = round(int(predict[0]),2)
except:
temp2[2] = 0
return jsonify({"webstats": temp2})
return jsonify({"webstats": temp2})
@app.route('/delTime', methods=['POST'])
#@cross_origin()
def deleteTime():
print('\ndeleting time')
data = request.get_json()
print(data)
if data == {}:
return jsonify({'flask': 'nothing received'})
name = data['username']
website = data['website']
print(name, website)
print(d)
#store stats of website into server
if website not in websites.keys():
websites[website] = webscrape(website)
if name in d.keys():
if website in d[name].keys():
del d[name][website]
#save_file()
with open("data.json", "w") as f:
json.dump(d, f)
message = {'flask': 'Time Deleted!'}
print(d)
return jsonify(message)
@app.route('/showTime', methods=['GET', 'POST'])
#@cross_origin()
def showTime():
print("\nsending Times")
data = request.get_json()
print(data)
if data == {}:
return jsonify({'flask': 'nothing received'})
name = data['username']
website = data['website']
print(name, website)
print(d)
#store stats of website into server
if website not in websites.keys():
websites[website] = webscrape(website)
try:
test = [websites[website][1],websites[website][3],websites[website][4]]
print("predicts", predictTimes(times[name],test))
predict[0] = (predictTimes(times[name], test))
except:
print("no prediction")
predict[0] = 0
try:
temp = sorted(d[name][website])[:10]
except KeyError:
return jsonify({'flask': 'nothing received'})
return jsonify({"time": temp})
@app.route('/showWords', methods=['GET', 'POST'])
#@cross_origin()
def showWords():
print("\nshowWords")
dat = request.get_json()
print(dat)
if dat == {}:
return jsonify({'flask': 'nothing received'})
website = dat['website']
print("website", website)
print(d)
#store stats of website into server
if website not in websites.keys():
websites[website] = webscrape(website)
temp2 = websites[website]
temp2[0] = temp2[0][:10]
try:
temp2[2] = round(int(predict[0]),2)
except:
print("Prediction not found")
return jsonify({"webstats": temp2})
return jsonify({"webstats": temp2})
if __name__ == '__main__':
#open file
if os.stat('data.json').st_size != 0:#check if file exists and is not empty
try:
with open('data.json') as f:
d = json.load(f)
except FileNotFoundError:
pass
print(d)
if os.stat('time.json').st_size != 0:#check if file exists and is not empty
try:
with open('time.json') as f:
times = json.load(f)
except FileNotFoundError:
pass
print(times)
#app.run(debug = True)
app.run(host='0.0.0.0', port=8080, debug = False, threaded = True)