-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.py
114 lines (100 loc) · 2.58 KB
/
server.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
#
# twitterbot2
#
# edoardottt
# edoardoottavianelli.it
# https://github.com/edoardottt/twitterbot2
#
# This repository is under GPL-3 License.
#
#
# This file contains the api logic for the web service.
#
from flask import Flask
from flask import request
from flask.templating import render_template
import datetime
import db
app = Flask(__name__, template_folder="templates")
app.config["SECRET_KEY"] = "ILVYilvbthLQETHeteggrgwi2r389"
@app.route("/")
def hello():
conn = db.conn_db()
users = db.all_users(conn)
return render_template("index.html", len=len(users), users=users)
def user_ok(user):
"""
This function checks if a username is correct.
"""
if user is None:
return False
if len(user) == 0:
return False
conn = db.conn_db()
users = db.all_users(conn)
for param in users:
if user == param[0]:
return True
return False
@app.route("/dashboard")
def user_dashboard():
user = request.args.get("user")
if not user_ok(user):
return render_template("error.html", errormsg="Invalid username.")
# if user ok ->
conn = db.conn_db()
values = db.today_stats(conn, user)
today_show = 1
if values is None:
today_show = 0
else:
(
username,
today,
tweet_count,
likes_count,
retweet_count,
followers_count,
) = values
values = db.user_stats(conn, user)
if values is None:
return render_template("error.html", errormsg="No data for this user.")
# render last 50 logs
logs_line = 50
logs = []
render_logs = 1
try:
with open("twitterbot2.log", "r") as f:
text = f.read().split("\n")
if len(text) > logs_line:
start = len(text) - logs_line - 1
else:
start = 0
for line in text[start:]:
if line.strip() != "":
logs.append(line)
except Exception:
render_logs = 0
if not today_show:
(
tweet_count,
likes_count,
retweet_count,
followers_count,
) = ("", "", "", "")
return render_template(
"dashboard.html",
user=user,
update=datetime.datetime.now().strftime("%d %b %Y %H:%M:%S"),
tweets=tweet_count,
likes=likes_count,
retweets=retweet_count,
followers=followers_count,
values=values,
today_show=today_show,
len=len(logs),
logs=logs,
render_logs=render_logs,
)
if __name__ == "__main__":
app.run()