forked from MShawon/YouTube-Viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
website.py
135 lines (106 loc) · 3.65 KB
/
website.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
import calendar
import sqlite3
from contextlib import closing
from datetime import date, datetime, timedelta
from flask import Flask, jsonify, render_template, request
MONTHS = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December']
global console
console = []
database = 'database.db'
def create_graph_data(dropdown_text):
now = datetime.now()
day = now.day
month = now.month
year = now.year
today = now.date()
days = False
number = False
try:
number = [int(s) for s in dropdown_text.split() if s.isdigit()][0]
except:
pass
if number:
if dropdown_text.startswith('Last'):
days = number
else:
month = MONTHS.index(dropdown_text[:-5]) + 1
year = number
else:
month = MONTHS.index(dropdown_text) + 1
query_days = []
if days:
for i in range(days):
day = today - timedelta(days=i)
query_days.append(str(day))
query_days.reverse()
else:
num_days = calendar.monthrange(year, month)[1]
for day in range(1, num_days+1):
query_days.append(str(date(year, month, day)))
first_date = query_days[0]
last_date = query_days[-1]
graph_data = [['Date', 'Views']]
total = 0
try:
with closing(sqlite3.connect(database, timeout=30)) as connection:
with closing(connection.cursor()) as cursor:
for i in query_days:
view = cursor.execute(
"SELECT view FROM statistics WHERE date = ?", (i,),).fetchall()
if view:
graph_data.append([i[-2:], view[0][0]])
total += view[0][0]
else:
graph_data.append([i[-2:], 0])
except:
pass
return graph_data, total, first_date, last_date
def create_dropdown_data():
dropdown = ['Last 7 days', 'Last 28 days', 'Last 90 days']
now = datetime.now()
current_year = now.year
dropdown.append(now.strftime("%B"))
for _ in range(0, 12):
now = now.replace(day=1) - timedelta(days=1)
if current_year == now.year:
dropdown.append(now.strftime("%B"))
else:
dropdown.append(now.strftime("%B %Y"))
return dropdown
def shutdown_server():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
def start_server(host, port):
app = Flask(__name__,
static_url_path='',
static_folder='web/static',
template_folder='web/templates')
@app.route('/')
def home():
dropdown = create_dropdown_data()
return render_template('homepage.html', dropdownitems=dropdown)
@app.route('/update', methods=['POST'])
def update():
return jsonify({'result': 'success', 'console': console[-20:]})
@app.route('/graph', methods=['GET', 'POST'])
def graph():
query = None
if request.method == 'POST':
query = request.json['query']
graph_data, total, first_date, last_date = create_graph_data(query)
return jsonify({
'graph_data': graph_data,
'total': total,
'first': first_date,
'last': last_date
})
@app.route('/shutdown', methods=['POST'])
def shutdown():
shutdown_server()
return 'Server shutting down...'
app.run(host=host, port=port)
if __name__ == '__main__':
start_server(host='0.0.0.0', port=5000)