OzoneAsai commited on
Commit
b9bca66
1 Parent(s): a106c71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -39
app.py CHANGED
@@ -1,46 +1,55 @@
1
- import hashlib
2
- import time
3
- from flask import Flask, render_template, request, jsonify
4
 
5
  app = Flask(__name__)
6
- current_text = ""
7
- logs = []
8
 
9
- def get_ip_hash(ip):
10
- return hashlib.sha256(ip.encode()).hexdigest()[:8]
11
-
12
- def log_text(ip, text):
13
- timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
14
- ip_hash = get_ip_hash(ip)
15
- logs.append([timestamp, ip_hash, text])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  @app.route('/')
18
- def index():
19
- return render_template('index.html')
20
-
21
- @app.route('/ViewPort')
22
- def viewport():
23
- return render_template('viewport.html')
24
-
25
- @app.route('/Ctrl', methods=['GET', 'POST'])
26
- def ctrl():
27
- global current_text
28
- if request.method == 'POST':
29
- new_text = request.form['text']
30
- if new_text != current_text: # Check for duplicate content
31
- current_text = new_text
32
- log_text(request.remote_addr, current_text)
33
- return '', 204
34
- return render_template('ctrl.html')
35
-
36
- @app.route('/get_text')
37
- def get_text():
38
- global current_text
39
- return jsonify({'text': current_text})
40
-
41
- @app.route('/log')
42
- def log():
43
- return render_template('log.html', log_data=logs)
44
 
45
  if __name__ == '__main__':
46
- app.run(debug=True, host="0.0.0.0", port=7860)
 
1
+ from flask import Flask, request, jsonify, render_template_string
2
+ from datetime import datetime
 
3
 
4
  app = Flask(__name__)
 
 
5
 
6
+ # テキストを記録するためのリスト
7
+ texts = []
8
+
9
+ @app.route('/post', methods=['POST'])
10
+ def post_text():
11
+ # POSTリクエストからテキストを取得
12
+ text = request.form.get('text')
13
+ if text:
14
+ # 現在の日時を取得
15
+ timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
16
+ # テキストとタイムスタンプをリストに追加
17
+ texts.append({'text': text, 'timestamp': timestamp})
18
+ return jsonify({'message': 'Text received and recorded', 'text': text, 'timestamp': timestamp}), 201
19
+ else:
20
+ return jsonify({'message': 'No text provided'}), 400
21
+
22
+ @app.route('/getText/latest', methods=['GET'])
23
+ def get_latest_text():
24
+ if texts:
25
+ # リストの最後の要素(最新のテキスト)を取得
26
+ latest_text = texts[-1]
27
+ return jsonify(latest_text), 200
28
+ else:
29
+ return jsonify({'message': 'No texts recorded yet'}), 200
30
 
31
  @app.route('/')
32
+ def display_texts():
33
+ # 記録されたテキストをHTMLで表示
34
+ html_content = '''
35
+ <!DOCTYPE html>
36
+ <html lang="ja">
37
+ <head>
38
+ <meta charset="UTF-8">
39
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
40
+ <title>Recorded Texts</title>
41
+ </head>
42
+ <body>
43
+ <h1>Recorded Texts</h1>
44
+ <ul>
45
+ {% for entry in texts %}
46
+ <li><strong>{{ entry.timestamp }}:</strong> {{ entry.text }}</li>
47
+ {% endfor %}
48
+ </ul>
49
+ </body>
50
+ </html>
51
+ '''
52
+ return render_template_string(html_content, texts=texts)
 
 
 
 
 
53
 
54
  if __name__ == '__main__':
55
+ app.run(debug=True, host='0.0.0.0', port=7860)