Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import hashlib
|
3 |
+
import time
|
4 |
+
import csv
|
5 |
+
from flask import Flask, render_template, request, jsonify
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
current_text = ""
|
9 |
+
log_file = 'text_log.csv'
|
10 |
+
|
11 |
+
def get_ip_hash(ip):
|
12 |
+
return hashlib.sha256(ip.encode()).hexdigest()[:8]
|
13 |
+
|
14 |
+
def log_text(ip, text):
|
15 |
+
timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
|
16 |
+
ip_hash = get_ip_hash(ip)
|
17 |
+
with open(log_file, 'a', newline='') as csvfile:
|
18 |
+
writer = csv.writer(csvfile)
|
19 |
+
writer.writerow([timestamp, ip_hash, text])
|
20 |
+
|
21 |
+
@app.route('/')
|
22 |
+
def index():
|
23 |
+
return render_template('index.html')
|
24 |
+
|
25 |
+
@app.route('/ViewPort')
|
26 |
+
def viewport():
|
27 |
+
return render_template('viewport.html')
|
28 |
+
|
29 |
+
@app.route('/Ctrl', methods=['GET', 'POST'])
|
30 |
+
def ctrl():
|
31 |
+
global current_text
|
32 |
+
if request.method == 'POST':
|
33 |
+
new_text = request.form['text']
|
34 |
+
if new_text != current_text: # Check for duplicate content
|
35 |
+
current_text = new_text
|
36 |
+
log_text(request.remote_addr, current_text)
|
37 |
+
return '', 204
|
38 |
+
return render_template('ctrl.html')
|
39 |
+
|
40 |
+
@app.route('/get_text')
|
41 |
+
def get_text():
|
42 |
+
global current_text
|
43 |
+
return jsonify({'text': current_text})
|
44 |
+
|
45 |
+
@app.route('/log')
|
46 |
+
def log():
|
47 |
+
log_data = []
|
48 |
+
with open(log_file, newline='') as csvfile:
|
49 |
+
reader = csv.reader(csvfile)
|
50 |
+
for row in reader:
|
51 |
+
log_data.append(row)
|
52 |
+
return render_template('log.html', log_data=log_data)
|
53 |
+
|
54 |
+
if __name__ == '__main__':
|
55 |
+
# Create log file with headers if it doesn't exist
|
56 |
+
with open(log_file, 'a', newline='') as csvfile:
|
57 |
+
writer = csv.writer(csvfile)
|
58 |
+
writer.writerow(['Timestamp', 'IP Hash', 'Text'])
|
59 |
+
app.run(debug=True, host="0.0.0.0", )
|