-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
170 lines (129 loc) · 4.6 KB
/
Copy pathapp.py
File metadata and controls
170 lines (129 loc) · 4.6 KB
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
from flask import Flask, jsonify
from flask_cors import CORS
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func, inspect
import time
import string
engine = create_engine('sqlite:///Forum.sqlite');
# engine = create_engine('postgres://forum:forum@localhost/forum')
Base = automap_base()
Base.prepare(engine, reflect=True)
ForumStats = Base.classes.ForumStats;
HourlyStats = Base.classes.HourlyStats;
Users = Base.classes.Users;
session = Session(engine);
app = Flask(__name__);
#ALLOWS CORS Access
CORS(app);
#Create simple front page to describe routes
@app.route("/")
def guidance():
return (
f"<h1 style='text-align: center;'>Here are the available routes:</h1>"
f"<ul style='text-align: center;'><li style='display: inline-block;'>/total</li></br>"
f"<li style='display: inline-block;'>/daily/:date:</li></br><li style='display: inline-block;'>/hourly/:date:</li></br>"
f"<li style='display: inline-block;'>specific/total/:forum name:</li></br><li style='display: inline-block;'>specific/daily/:date:/:forum name:</li></br>"
f"<li style='display: inline-block;'>specific/hourly/:date:/:forum name:</li></br></ul>"
f"<p style='text-align: center;'>Please enter dates formatted as YYYY-MM-DD</p>"
f"<p style='text-align: center;'>Strings accepted for search queries.</p>"
)
@app.route("/total+<string:username>+<string:password>")
def total(username,password):
if session.query(Users).filter(Users.username == username).filter(Users.password == password).all():
dates = (time.strftime("%Y/%m/%d"))
totals = session.query(ForumStats).filter(ForumStats.Date == dates).all()
#ttd is short for total to date
ttd = [];
for row in totals:
totaldict = {};
totaldict['name'] = row.name;
totaldict['Messages'] = row.Messages;
totaldict['Threads']= row.Threads;
totaldict['Members'] = row.Members;
ttd.append(totaldict);
return jsonify(ttd);
else:
return (
f"<p>Nothing to see here.</p>"
)
@app.route("/daily/<string:date>")
def daily(date):
properdate = date.replace('-', '/');
daily = session.query(ForumStats).filter(ForumStats.Date == properdate).all()
#dtd is short for daily to date
dtd = [];
for row in daily:
totaldaily = {};
totaldaily['name'] = row.name;
totaldaily['Messages'] = row.Messages;
totaldaily['Threads'] = row.Threads;
totaldaily['Members'] = row.Members;
dtd.append(totaldaily);
return jsonify(dtd);
@app.route("/hourly/<string:date>")
def hourly(date):
properdate = date.replace('-', '/');
hourly = session.query(HourlyStats).filter(HourlyStats.Date == properdate).all()
#hod is short for hourly on date
hod = [];
for row in hourly:
totalhourly = {};
totalhourly['name'] = row.name;
totalhourly['Messages'] = row.Messages;
totalhourly['Threads'] = row.Threads;
totalhourly['Members'] = row.Members;
hod.append(totalhourly);
return jsonify(hod);
@app.route("/specific/total/<string:forum>")
def spectotal(forum):
dates = (time.strftime("%Y/%m/%d"))
totals = session.query(ForumStats).filter(ForumStats.Date == dates).filter(ForumStats.name == forum).all()
#ttd is short for total to date
ttd = [];
for row in totals:
totaldict = {};
totaldict['name'] = row.name;
totaldict['Messages'] = row.Messages;
totaldict['Threads']= row.Threads;
totaldict['Members'] = row.Members;
ttd.append(totaldict);
return jsonify(ttd);
@app.route("/specific/daily/<string:date>/<string:forum>")
def specdaily(date, forum):
properdate = date.replace('-', '/');
print(date);
print(forum);
daily = session.query(ForumStats).filter(ForumStats.Date == properdate).filter(ForumStats.name == forum).all()
print(daily);
#dtd is short for daily to date
dtd = [];
for row in daily:
totaldaily = {};
totaldaily['name'] = row.name;
totaldaily['Messages'] = row.Messages;
totaldaily['Threads'] = row.Threads;
totaldaily['Members'] = row.Members;
dtd.append(totaldaily);
return jsonify(dtd);
@app.route("/specific/hourly/<string:date>/<string:forum>")
def spechourly(forum, date):
properdate = date.replace('-', '/');
print(date);
print(forum);
print(properdate);
hourly = session.query(HourlyStats).filter(HourlyStats.Date == properdate).filter(HourlyStats.name == forum).all()
print(daily);
#hod is short for hourly on date
hod = [];
for row in hourly:
totalhourly = {};
totalhourly['name'] = row.name;
totalhourly['Messages'] = row.Messages;
totalhourly['Threads'] = row.Threads;
totalhourly['Members'] = row.Members;
hod.append(totalhourly);
return jsonify(hod);
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, port=3134)